CBSE 11TH CLASS CS PROGRAM 13

 13. Input a list of numbers and swap elements at the even location with the elements at the odd location.

Python

numbers = [1, 2, 3, 4, 5]

 

for i in range(0, len(numbers), 2):

    if i + 1 < len(numbers):

        numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]

 

print("Swapped list:", numbers)

No comments:

Post a Comment