Output of Python program | Set 15 (Loops)

22 Aug, 2019
Prerequisite – Loops in PythonPredict the output of the following Python programs.
1) What is the output of the following program?x = ['ab', 'cd']for i in x:    i.upper()print(x)Output:['ab', 'cd']
Explanation:The function upper() does not modify a string in place, but it returns a new string which here isn’t being stored anywhere. So we will get our original list as output.
x = ['ab', 'cd']for i in x:    i.upper()print(x)
Output:
['ab', 'cd']

Explanation:The function upper() does not modify a string in place, but it returns a new string which here isn’t being stored anywhere. So we will get our original list as output.
2) What is the output of the following program?x = ['ab', 'cd']for i in x:    x.append(i.upper())print(x)Output:No Output
Explanation:The loop does not terminate as new elements are being added to the list in each iteration. So our program will stuck in infinite loop
x = ['ab', 'cd']for i in x:    x.append(i.upper())print(x)
Output:
No Output

Explanation:The loop does not terminate as new elements are being added to the list in each iteration. So our program will stuck in infinite loop
3) What is the output of the following program?i = 1while True:    if i%3 == 0:        break    print(i)    i + = 1Output:No Output
Explanation:The program will give no output as there is an error in the code. In python while using expression there shouldn’t be a space between + and = in +=.
i = 1while True:    if i%3 == 0:        break    print(i)    i + = 1
Output:
No Output

Explanation:The program will give no output as there is an error in the code. In python while using expression there shouldn’t be a space between + and = in +=.
4) What is the output of the following program?x = 123for i in x:    print(i)Output:Error!
Explanation:Objects of type int are not iterable instead a list, dictionary or a tuple should be used.
x = 123for i in x:    print(i)
Output:
Error!

Explanation:Objects of type int are not iterable instead a list, dictionary or a tuple should be used.
5) What is the output of the following program?for i in [1, 2, 3, 4][::-1]:    print (i)Output:4
3
2
1
Explanation:Adding [::-1] beside your list reverses the list. So output will be the elements of original list but in reverse order.
for i in [1, 2, 3, 4][::-1]:    print (i)
Output:
4
3
2
1

Explanation:Adding [::-1] beside your list reverses the list. So output will be the elements of original list but in reverse order.
This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Akanksha_Rai
Python-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Runtime Errors
Different ways to copy a string in C/C++
Output of C++ Program | Set 1
Output of Java Program | Set 3
Output of Java Programs | Set 12
Output of C++ programs | Set 47 (Pointers)
Output of Java Program | Set 7
Output of C Programs | Set 3
Output of C programs | Set 59 (Loops and Control Statements)
unsigned specifier (%u) in C with Examples