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

Class 11 Computer Science with Python CBSE Syllabus Practical Term 2. Input a list of numbers and swap elements at the even location with the elements at the odd location. These programs consists of the series of 5 questions that has been recommended by CBSE written on the syllabus of class 11 Term-2. The Python Programme for CBSE class 11 practical file is given below:-

Exercise 2 : Input a list of numbers and swap elements at the even location with the elements at the odd location.

Code (Python):

val=eval(input("Enter a list "))
print("Original List is:",val)
s=len(val)
if s%2!=0:
    s=s-1
for i in range(0,s,2):
    val[i],val[i+1]=val[i+1],val[i]
print("List after swapping :",val)

Explanation of code

  • Use of eval function : eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.
  • A list is directly added. The list is continuous .
  • The length of list is directly calculated by len() function.
  • Then the conditional operator checks whether the total length is even or odd and if it is odd then s variable contain the length of the list inputted is decreased by 1.
  • Then with the help of for loop the values are interchanged and we get the desired result.

Output of Programme

Enter a list [1,2,3,4]
Original List is: [1,2,3,4]
List after swapping : [2,1,4,3]

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

Thankyou for learning the topic. Python is the most powerful language present and being used in writing artificial intelligence and data analysis. With huge number of keywords and library this language is a jewel for your career. This program is provided as exercise 2 by CBSE for practical file. Practical file has more than the 5 Exercise of such recommendation For more programs from practical file of Term 2 link is provided below. This site contain enough material for the preparation and learning for the class 11 and 12 and beginner level projects, practical questions and answer.

Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study

Leave a Comment

Your email address will not be published. Required fields are marked *