Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 89 of 102
Marks: +1, -0
Write a function EOReplace() in Python, which accepts a list L of numbers. Thereafter, it increments all even numbers by 1 and decrements all odd numbers by 1.
Example :
If Sample Input data of the list is :
L=[10,20,30,40,35,55]
Output will be :
L=[11,21,31,41,34,54]
[CBSE 2023 Set 4]
Solution:  
def EOReplace(L):
for i in range(len(L)):
if L[i] % 2 == 0:
L[i] = L[i] + 1
else:
L[i] = L[i] - 1
print("L=", L)
L = [10, 20, 30, 40, 35, 55]
EOReplace(L)
Explanation: The loop runs over the indices of the list. L[i] % 2 == 0 checks for an even number, which is increased by 1; an odd number is decreased by 1. The list is changed in place, giving [11, 21, 31, 41, 34, 54].
© examsnet.com
Advertisements
Go to Question: