NEW
Font size
WorksheetsMastering Python Control Flow
Total questions: 15
Worksheet time: 8mins
What is the output of the following code: if 5 > 3: print('A') else: print('B')?
A
5
B
C
How do you correctly use an else if statement in Python?
Use 'elseif' to chain multiple conditions together.
Use 'if else' to handle multiple scenarios in one line.
Use 'elif' to add additional conditions after an 'if' statement.
Use 'else' to create a new block without conditions.
What will be the output of the following code: for i in range(3): print(i)?
1.5
0 1 2
3
-1
Explain the difference between a for loop and a while loop in Python.
A for loop is used for conditional execution, while a while loop is for fixed iterations.
A for loop is used for iterating over a sequence, while a while loop continues until a condition is false.
A for loop runs indefinitely, while a while loop stops after a fixed number of iterations.
A for loop can only iterate over numbers, while a while loop can iterate over any data type.
What is the purpose of the 'continue' statement in a loop?
To end the loop immediately and exit.
To skip the current iteration of a loop and continue with the next one.
To repeat the current iteration of a loop.
To pause the loop for a specified time.
How can you manipulate a loop to skip the current iteration?
Add a return statement inside the loop.
Remove the loop entirely.
Use the 'break' statement in the loop.
Use the 'continue' statement in the loop.
What will be the output of the following code: my_list = [1, 2, 3]; for item in my_list: if item == 2: break; print(item)?
1
1, 2
3
2
How do you iterate over a dictionary in Python?
for key in my_dict.keys():
for key in my_dict: # to iterate over keys for value in my_dict.values(): # to iterate over values for key, value in my_dict.items(): # to iterate over key-value pairs
for value in my_dict.items():
for item in my_dict:
What is the result of using 'pass' in a loop?
It raises an error during execution.
It skips the entire loop and moves to the next one.
It results in a no-operation for that iteration of the loop.
It causes the loop to terminate immediately.
Write a Python code snippet that uses a while loop to count from 1 to 5.
count = 1 while count <= 5: print(count) count += 1
count = 1 for count in range(1, 6): print(count)
count = 0 while count < 5: print(count) count += 1
count = 1 while count < 5: print(count) count += 2
What is the output of the following code: for i in range(5): if i == 2: continue; print(i)?
5
0 1 3 4
2
-1
How can you use an else block with a for loop? Provide an example.
for i in range(5): print(i) else: print('Loop interrupted.');
for i in range(5): print(i) break;
Example: for i in range(5): print(i) else: print('Loop completed without break.') # Output: # 0 # 1 # 2 # 3 # 4 # Loop completed without break.
for i in range(5): print(i) if i == 3: break; else: print('Done.');
What will be the output of the following code: my_string = 'Hello'; for char in my_string: if char == 'e': break; print(char)?
e
l
o
H
Describe how to use a for loop to iterate through a list of numbers and print only the even ones.
for number in numbers: if number % 2 == 0: print(number)
for number in numbers: print(number)
for number in numbers: if number % 2 != 0: print(number)
for number in range(len(numbers)): print(numbers[i])
What is the significance of the range() function in a for loop?
The range() function generates random numbers for iteration.
The range() function provides a sequence of numbers for iteration in a for loop.
The range() function is used to create a list of strings for a for loop.
The range() function limits the number of iterations in a while loop.
