Computer Science
What will be the output of the following code ?
num = [3, 5, 7, 9]
def process():
num.append(11)
num[0] = num[2] + 1
print(num[0], end = '#')
process()
num[2] = 14
print(num[2], end = '%')
Python Functions
2 Likes
Answer
8#14%
Reason
num = [3, 5, 7, 9]— Initializes a listnumwith four elements.def process():— Defines a function namedprocess.num.append(11)— Adds 11 to the end of the listnum. Now,numbecomes[3, 5, 7, 9, 11].num[0] = num[2] + 1— The element at index 0 of the listnumis updated using the expressionnum[2] + 1. Initially,num[2]holds the value 7, so adding 1 to it results in 8. Now,numbecomes[8, 5, 7, 9, 11].print(num[0], end = '#')— Prints the first element of the list (num[0]), which is now 8, followed by a#(since theend='#'argument specifies that the line should end with#). Output at this point:8#.num[2] = 14— The value at index 2 (which isnum[2], 7) is updated to 14. Now,numbecomes[8, 5, 14, 9, 11].print(num[2], end = '%')— Prints the value ofnum[2], which has been updated to 14, followed by a%(since theend='%'argument specifies that the line should end with%). Thus, the final output is8#14%.
Answered By
3 Likes