WorksheetsFor Loop in Python
Total questions: 10
Worksheet time: 5mins
What is the syntax of a for loop in Python?
for item in iterable: # code to be executed
for i in range(n): # code to be executed
for element in list: # code to be executed
for item in iterable: # code to be executed
How do you define the range of a for loop in Python?
By using the range() function with start, stop, and step parameters.
By using the len() function with the iterable as the parameter.
By using the start, stop, and step parameters directly in the for loop.
By using the range() function with only the stop parameter.
What is the purpose of the else statement in a for loop?
To specify a block of code to be executed before the loop starts
To specify a block of code to be executed if the loop completes normally
To specify a block of code to be executed if the loop does not complete normally
To specify a block of code to be executed after each iteration of the loop
How many times does a for loop iterate if the range is (1, 5)?
4
2
1
3
What is the output of the following code?
0 1 2 Loop completed
0 1 2
3
0
1
2
0 1 2
3
Loop completed
What is the output of the following code? for i in range(1, 6, 2): print(i)
1 2 3 4 5
1 2 3 4
1 3 5
1 3 5 7
What is the output of the following code? for i in range(5, 0, -1): print(i)
1
2
3
4
5
1
2
3
4
5
4
3
2
1
4
3
2
1
What is the output of the following code?
0 1
0
1
Loop End
0
1
2
0
1
2
Loop End
What is the output of the following code?
0
3
0
1
1
2
0
2
What is the output of the following code?
0 2 Loop completed
0 1 Loop completed
0 1 2 Loop completed
0
2
