Computer Science, asked by saileinthirababu, 5 months ago

Write a function Swap(num, n) in Python, which accepts a list num of numbers and n is the

number of elements. The function will interchange every alternate value.
E.g

If the list num contain: [11, 21, 31, 41, 51, 61]

Output, [21, 11, 41, 31, 61, 51]​

Answers

Answered by dreamrob
9

Program:

def Swap(num, n):

   for i in range(0, n, 2):

       temp = num[i]

       num[i] = num[i+1]

       num[i+1] = temp

   return num

num = []

n = int(input("Enter the number of elements : "))

print("Enter the elements : ")

for i in range(0, n):

   x = int(input())

   num.append(x)

print("Original list : ", num)

print("Swapped list : ", Swap(num, n))

Output:

Enter the number of elements : 6

Enter the elements :  

11

21

31

41

51

61

Original list :  [11, 21, 31, 41, 51, 61]

Swapped list :  [21, 11, 41, 31, 61, 51]

Similar questions