The Wayback Machine - https://web.archive.org/web/20240204103649/https://www.geeksforgeeks.org/output-python-programs-set-22-loops/
Open In App
Related Articles

Output of Python Programs | Set 22 (Loops)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Prerequisite: Loops
Note: Output of all these programs is tested on Python3

1. What is the output of the following?




mylist = ['geeks', 'forgeeks']
for i in mylist:
    i.upper()
print(mylist)


  1. [‘GEEKS’, ‘FORGEEKS’].
  2. [‘geeks’, ‘forgeeks’].
  3. [None, None].
  4. Unexpected

Output:

2. [‘geeks’, ‘forgeeks’]

Explanation: The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.

2. What is the output of the following?




mylist = ['geeks', 'forgeeks']
for i in mylist:
    mylist.append(i.upper())
print(mylist)


  1. [‘GEEKS’, ‘FORGEEKS’].
  2. [‘geeks’, ‘forgeeks’, ‘GEEKS’, ‘FORGEEKS’].
  3. [None, None].
  4. None of these

Output:

4. None of these

Explanation:The loop does not terminate as new elements are being added to the list in each iteration.

3. What is the output of the following?




i = 1
while True:
    if i % 0O7 == 0:
        break
    print(i)
    i += 1


  1. 1 2 3 4 5 6.
  2. 1 2 3 4 5 6 7.
  3. error.
  4. None of these

Output:

1. 1 2 3 4 5 6

Explanation: The loop will terminate when i will be equal to 7.

4. What is the output of the following?




True = False
while True:
    print(True)
    break


  1. False.
  2. True.
  3. Error.
  4. None of these

Output:

3. Error

Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.

5. What is the output of the following?




i = 1
while True:
    if i % 3 == 0:
        break
    print(i)
    i + = 1


  1. 1 2 3.
  2. 1 2.
  3. Syntax Error.
  4. None of these

Output:

3. Syntax Error

Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 14 Dec, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials