Informatics Practices
What will be the output?
x = ['ab', 'cd']
for i in x:
i.upper()
print (x)
- ['ab', 'cd']
- ['AB', 'CD']
- [None, None]
- None of these
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'].
Related Questions
What does the following code print to console?
if True: print (101) else: print (202)- 101
- 202
- 303
- 102
Which of the following is not a loop statement in Python?
- do-while
- while
- for
- All of these
Which statement is used to iterate itself over a range of values or a sequence?
- if
- while
- do-while
- for
Find the output of the following Python program:
for x in range(1, 20, 3): print(x, end =',')- 1,20,3,
- 1,4,7,10,13,16,19,
- 13,6,9,12,15,18,
- 20,40,60,80,100,