Wednesday 14 February 2024

Class 11 CS Practical - 37

 37 - Write a program in Python to Input a list of numbers and swap elements at the even location with the elements at the odd location. 

L=[]

n=int(input("Enter the size of the list: "))

for i in range(0,n):

    a=int(input("Enter element of List: "))

    L.append(a)

print("List is: ", L)

if n%2!=0:

    n=n-1

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

    L[i],L[i+1]=L[i+1],L[i]

print("Updated list is: ", L)




Output:

Enter the size of the list: 5

Enter element of List: 2

Enter element of List: 8

Enter element of List: 6

Enter element of List: 4

Enter element of List: 7

List is: [2, 8, 6, 4, 7]

Updated list is: [8, 2, 4, 6, 7]


Enter the size of the list: 6

Enter element of List: 2

Enter element of List: 8

Enter element of List: 6

Enter element of List: 4

Enter element of List: 7

Enter element of List: 9

List is: [2, 8, 6, 4, 7, 9]

Updated list is: [8, 2, 4, 6, 9, 7]






No comments:

Post a Comment