Computer Science

What will be the output of following code?

x = ['3', '2', '5']
y = ''
while x:
    y = y + x[-1]
    x = x[:len(x) - 1]
print(y)
print(x)
print(type(x), type(y))

Python

Python List Manipulation

28 Likes

Answer

523
[]
<class 'list'> <class 'str'>

Working

The loop while x will continue executing as long as the length of list x is greater than 0. y is initially an empty string. Inside the loop, we are adding the last element of x to y and after that we are removing the last element of x from x. So, at the end of the loop y becomes 523 and x becomes empty. Type of x and y are list and str respectively.

Answered By

9 Likes


Related Questions