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))
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.
Related Questions
Find the errors:
L1 = [3, 4, 5] L2 = L1 * 3 print(L1 * 3.0) print(L2)
Find the errors:
L1 = [3, 3, 8, 1, 3, 0, '1', '0', '2', 'e', 'w', 'e', 'r'] print(L1[: :-1]) print(L1[-1:-2:-3]) print(L1[-1:-2:-3:-4])
Complete the code to create a list of every integer between 0 and 100, inclusive, named nums1 using Python, sorted in increasing order.
Let nums2 and nums3 be two non-empty lists. Write a Python command that will append the last element of nums3 to the end of nums2.