Home > Python > Quizzes > Python Programming Basics Practice Test: Python While & For Loops
Python Programming Basics Practice Test: Python While & For Loops
Fast practice, instant feedback. Timer auto-submits when time’s up.
Avg score: 10% Most missed: “What will be the output of the following Python code?”

Python has two types of loop statements:
For loop:
Used when the number of iterations is known before the loop starts.
While loop: Repeats as long as a certain condition is true.

Here are some examples of when you might use each loop:

For loop:
Iterate over a list of items
Perform a set number of iterations

While loop:
Iterate until a condition is met
Perform an unknown number of iterations

You can also use nested loops, which are loops that are placed inside of other loops. Nested loops can be useful for performing complex tasks.

Python Programming Basics Practice Test: Python While & For Loops
Time left 00:00
25 Questions

1. What will be the output of the following Python code snippet?
x = 2
for i in range(x):
x -= 2
print (x)
2. What will be the output of the following Python code?
d = {0, 1, 2}
for x in d:
print(d.add(x))
3. What will be the output of the following Python code snippet?
string = "my name is x"
for i in ' '.join(string.split()):
print (i, end=", ")
4. What will be the output of the following Python code?
x = (i for i in range(3))
for i in x:
print(i)
5. What will be the output of the following Python code snippet?
for i in 'abcd'[::-1]:
print (i)
6. What will be the output of the following Python code snippet?
a = [0, 1, 2, 3]
for a[0] in a:
print(a[0])
7. What will be the output of the following Python code?
i = 1
while True:
if i%0O7 == 0:
break
print(i)
i += 1
8. What will be the output of the following Python code?
for i in range(int(float('inf'))):
print (i)
9. What will be the output of the following Python code?
d = {0, 1, 2}
for x in d:
print(x)
10. What will be the output of the following Python code?
i = 1
while True:
if i%2 == 0:
break
print(i)
i += 2
11. What will be the output of the following Python code?
x = ['ab', 'cd']
for i in x:
i.upper()
print(x)
12. What will be the output of the following Python code?
x = "abcdef"
i = "a"
while i in x:
x = x[1:]
print(i, end = " ")
13. What will be the output of the following Python code snippet?
for i in '':
print (i)
14. What will be the output of the following Python code?
d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
print(i)
15. What will be the output of the following Python code?
for i in range(2.0):
print(i)
16. What will be the output of the following Python code?
for i in range(float('inf')):
print (i)
17. What will be the output of the following Python code?
string = "my name is x"
for i in string:
print (i, end=", ")
18. What will be the output of the following Python code?
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
19. What will be the output of the following Python code?
x = 123
for i in x:
print(i)
20. What will be the output of the following Python code snippet?
a = [0, 1, 2, 3]
i = -2
for i not in a:
print(i)
i += 1
21. What will be the output of the following Python code?
x = "abcdef"
i = "a"
while i in x:
print('i', end = " ")
22. What will be the output of the following Python code?
x = "abcdef"
i = "a"
while i in x:
x = x[:-1]
print(i, end = " ")
23. What will be the output of the following Python code?
True = False
while True:
print(True)
break
24. What will be the output of the following Python code?
x = "abcdef"
i = "a"
while i in x:
print(i, end = " ")
25. What will be the output of the following Python code?
x = 'abcd'
for i in range(len(x)):
print(i.upper())