Skip to content

Question 11

What is the output of the following code?

1
2
3
4
5
6
7
8
9
n, s = '0', 'me'
if n and s:
    n, s = s, n
elif not n:
    if not s:
        s = n + s
if s:
    s *= 2
print(s)
Solution

Given n = '0' and s = 'me',

  • if n and s is True; this is because while 0 is indicative of one of the False-like values, '0' is not an empty string given there are quotation marks surrounding it. 0 is not seen the same way as '0' here. Hence, n, s = s, n swaps the values of n and s, making n = 'me' and s = '0'. Ignore elif not n block from here.
  • if s is True, since s = '0' is not an empty string. This leads to s *= 2 which makes s = '00'.
  • Print s: prints '00'
Answer
'00'