WorksheetsPython Quiz day 3 : Conditional Statements and Loops
Total questions: 10
Worksheet time: 5mins
What is the syntax for an if-else statement in Python?
if condition: # code to be executed if the condition is false else: # code to be executed if the condition is true
if condition: # code to be executed if the condition is true else: # code to be executed if the condition is false
if condition: # code to be executed if the condition is true else: # code to be executed if the condition is true
if condition: # code to be executed if the condition is true else: # code to be executed if the condition is false
How do you write a nested if statement in Python?
By adding a for loop within the if statement block
By using a switch statement instead
By including another if statement within the block of code for the first if statement.
By removing the first if statement
What is the purpose of the 'elif' keyword in Python?
To end the 'if' statement
To create a loop
To define a function
To add another condition to an 'if' statement
What is the output of the following code? x = 5 if x > 3: print('x is greater than 3') else: print('x is less than or equal to 3')
x is equal to 3
x is greater than or equal to 3
x is greater than 3
x is less than 3
What is the output of the following code? for i in range(5): print(i)
15
10
5
0 1 2 3 4
What is the purpose of the 'break' keyword in a loop?
To double the value of the loop variable
To print a message to the console
To continue to the next iteration of the loop
To exit the loop immediately
What is the purpose of the 'continue' keyword in a loop?
To end the loop immediately
To print the current iteration value
To restart the loop from the beginning
To skip the current iteration of the loop and continue with the next iteration.
What is the output of the following code? for i in range(1, 6, 2): print(i)
2
1 3
6
4
How do you write a nested for loop in Python?
Use the syntax: for x in range(3): for y in range(3): print(x, y)
Nested for loops are not supported in Python
You can write a nested for loop in Python by using the syntax: for x in range(3): for y in range(3): print(x, y)
You can't write a nested for loop in Python
What is the output of the following code? x = 2 if x > 5: print('x is greater than 5') else: print('x is less than or equal to 5')
x is equal to 5
x is greater than 5
x is less than 5
x is less than or equal to 5
