Informatics Practices

What will be the output?

x = ['ab', 'cd']
for i in x:
   i.upper()
print (x)
  1. ['ab', 'cd']
  2. ['AB', 'CD']
  3. [None, None]
  4. None of these

Python Control Flow

2 Likes

Answer

['ab', 'cd']

Reason — In the given code, the for loop iterates over the list x containing strings 'ab' and 'cd'. Inside the loop, i.upper() is called, which returns a new string in uppercase but doesn't modify i itself because strings in Python are immutable. Therefore, i.upper() doesn't change i or x in place. After the loop finishes, x remains unchanged, so the output is ['ab', 'cd'].

Answered By

1 Like


Related Questions