KnowledgeBoat Logo
|

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

  1. num = [3, 5, 7, 9] — Initializes a list num with four elements.

  2. def process(): — Defines a function named process.

  3. num.append(11) — Adds 11 to the end of the list num. Now, num becomes [3, 5, 7, 9, 11].

  4. num[0] = num[2] + 1 — The element at index 0 of the list num is updated using the expression num[2] + 1. Initially, num[2] holds the value 7, so adding 1 to it results in 8. Now, num becomes [8, 5, 7, 9, 11].

  5. print(num[0], end = '#') — Prints the first element of the list (num[0]), which is now 8, followed by a # (since the end='#' argument specifies that the line should end with #). Output at this point: 8#.

  6. num[2] = 14 — The value at index 2 (which is num[2], 7) is updated to 14. Now, num becomes [8, 5, 14, 9, 11].

  7. print(num[2], end = '%') — Prints the value of num[2], which has been updated to 14, followed by a % (since the end='%' argument specifies that the line should end with %). Thus, the final output is 8#14%.

Answered By

3 Likes


Related Questions