Page 431 - Computer Science Class 11 With Functions
P. 431
else:
newString += char
return newString
string = input("Enter a string: ")
newString = swapCase(string)
print("The string after reversing the Case is : ", newString)
Program 14
Write a program that takes a user-provided list as input and then generates a new list comprising swapped values at
odd indices with those at even indices.
Ans. def swappOddEvenIndex(myList):
'''
Objective : To swap values at odd indices with those at even indices
Input Parameter : myList - list
Return Value : myList - list with swapped values
'''
for x in range(0,len(myList)-1,2):
myList[x], myList[x+1] = myList[x+1],myList[x]
return myList
myList = []
num = int(input("Enter the number of elements that you want of add to the list
: "))
for x in range(num):
num1 = int(input("Enter number : "))
myList.append(num1)
print("The list formed is : ", myList)
resList = swappOddEvenIndex(myList)
print("The list after swapping odd indices with those at even indices is: ",
resList)
Program 15
Write a function search that takes a list myList and a value key to be searched in myList as arguments. When
the key is present in myList, the function search returns True and the index where the search key is found.
If search reaches the end of the list without finding the key, search returns False with None as the index.
Invoke search to search for a key in a list provided by the user.
Ans. def search(myList, key):
'''
Objective: To search the key in myList
Input Parameters:
myList: list
key : element to be searched
Practical 429

