NEW
Font size
WorksheetsPython If Statements
Total questions: 18
Worksheet time: 9mins
What is the correct syntax for an if statement in Python?
if (condition) {code block}
if condition: code block
if condition then code block
if [condition] {code block}
Which of the following operators is used to check if two values are not equal in Python?
!=
==
<=
>
How do you write an if statement to check if `x` is greater than 10?
if x > 10:
if x is > 10:
if x >> 10:
if x gt 10:
What is the correct way to write a nested if statement in Python?
if condition1: if condition2: code block
if condition1: then if condition2: code block
if condition1 & if condition2: code block
if condition1 > if condition2: code block
Which logical operator is used in Python to combine two boolean conditions and returns True if both conditions are true?
OR
AND
NOT
XOR
What is the output of the following code if `x = 5`? ```python if x > 2: print("Hello") elif x > 4: print("World") else: print("!") ```
Hello
World
!
Hello World
Which comparison operator is used to check if a value is less than or equal to another value in Python?
<
<=
==
!=
In Python, what is the correct way to write an if statement that checks if `x` is either 10 or 20?
if x == 10 or x == 20:
if x = 10 || x = 20:
if x == 10 | x == 20:
if x == (10 or 20):
What is the correct way to write a chained if statement in Python to check multiple conditions sequentially?
if condition1: elif condition2: else:
if condition1 > elif condition2 > else:
if condition1 then condition2 else:
if condition1 and then condition2 else:
Which logical operator would you use to write an if statement that checks if either `x` is greater than 10 or `y` is less than 5?
AND
OR
NOT
XOR
What is the correct syntax to write an if statement in Python that executes a code block only if a condition is false?
if not condition: code block
if condition != True: code block
if condition == False: code block
All of the above
Which of the following is a valid comparison operator in Python for checking greater than or equal to?
>=
=>
>+
=<
In Python, what does the `!=` operator do?
Checks if two values are equal
Checks if two values are not equal
Assigns a value not equal to a variable
Compares two values to see if the left is not equal to the right
What is the output of the following code? ```python x = 10 if x > 5: print("Greater") else: print("Lesser") ```
Greater
Lesser
Error
No output
Which of the following is the correct way to write an if statement to check if `x` is not equal to 10?
if x != 10:
if x <> 10:
if x not 10:
if x ~= 10:
What is the correct way to use logical operators for combining conditions in Python?
if condition1 && condition2:
if condition1 and condition2:
if condition1 & condition2:
if condition1 + condition2:
Which of the following is a valid syntax for an if-else statement in Python?
if condition: code block else: code block
if condition: code block; else: code block
if condition: code block else code block
if condition: code block else: code block
In Python, how do you write an if statement to check if `x` is between 5 and 10, inclusive?
if x > 5 and x < 10:
if 5 <= x <= 10:
if x >= 5 or x <= 10:
if x == 5 and x == 10:
