Page 137 - Trackpad_V1_Book 8_Flipbook
P. 137
6. Write the output for the following codes in Python:
Critical Thinking
a. test_str = "Good Morning"
print("The original string is : " + str(test_str))
hlf_idx = len(test_str)
res = ' '
for idx in range(len(test_str)):
if idx >= hlf_idx:
res+= test_str[idx].upper()
else:
res+= test_str[idx].upper()
print("The resultant string : " + str(res))
____________________________________________________________________________
b. def countX(lst, X):
count = 0
for ele in lst:
if(ele==X):
count = count + 1
return count
lst = [5.24, 89, 5, 14, 5]
X = 5
print("{} has occurred {} times".format(X, countX(lst, X)))
____________________________________________________________________________
c. def swapList(newList):
size = len(newList)
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
newList = [12, 45, 9, 32, 24]
print(swapList(newList))
____________________________________________________________________________
d. l1 = ['O', 'r', 'a', 'n', 'g', 'e']
l1.extend(['E', 'd', 'u', 'c', 'a', 't', 'i', 'o', 'n'])
print(l1)
print(l1[-3])
____________________________________________________________________________
Functions, String and List in Python 135

