wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python If Statements

Total questions: 18

Worksheet time: 9mins

Name
Class
Date
1.

What is the correct syntax for an if statement in Python?

a)

if (condition) {code block}

b)

if condition: code block

c)

if condition then code block

d)

if [condition] {code block}

2.

Which of the following operators is used to check if two values are not equal in Python?

a)

!=

b)

==

c)

<=

d)

>

3.

How do you write an if statement to check if `x` is greater than 10?

a)

if x > 10:

b)

if x is > 10:

c)

if x >> 10:

d)

if x gt 10:

4.

What is the correct way to write a nested if statement in Python?

a)

if condition1: if condition2: code block

b)

if condition1: then if condition2: code block

c)

if condition1 & if condition2: code block

d)

if condition1 > if condition2: code block

5.

Which logical operator is used in Python to combine two boolean conditions and returns True if both conditions are true?

a)

OR

b)

AND

c)

NOT

d)

XOR

6.

What is the output of the following code if `x = 5`? ```python if x > 2: print("Hello") elif x > 4: print("World") else: print("!") ```

a)

Hello

b)

World

c)

!

d)

Hello World

7.

Which comparison operator is used to check if a value is less than or equal to another value in Python?

a)

<

b)

<=

c)

==

d)

!=

8.

In Python, what is the correct way to write an if statement that checks if `x` is either 10 or 20?

a)

if x == 10 or x == 20:

b)

if x = 10 || x = 20:

c)

if x == 10 | x == 20:

d)

if x == (10 or 20):

9.

What is the correct way to write a chained if statement in Python to check multiple conditions sequentially?

a)

if condition1: elif condition2: else:

b)

if condition1 > elif condition2 > else:

c)

if condition1 then condition2 else:

d)

if condition1 and then condition2 else:

10.

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?

a)

AND

b)

OR

c)

NOT

d)

XOR

11.

What is the correct syntax to write an if statement in Python that executes a code block only if a condition is false?

a)

if not condition: code block

b)

if condition != True: code block

c)

if condition == False: code block

d)

All of the above

12.

Which of the following is a valid comparison operator in Python for checking greater than or equal to?

a)

>=

b)

=>

c)

>+

d)

=<

13.

In Python, what does the `!=` operator do?

a)

Checks if two values are equal

b)

Checks if two values are not equal

c)

Assigns a value not equal to a variable

d)

Compares two values to see if the left is not equal to the right

14.

What is the output of the following code? ```python x = 10 if x > 5: print("Greater") else: print("Lesser") ```

a)

Greater

b)

Lesser

c)

Error

d)

No output

15.

Which of the following is the correct way to write an if statement to check if `x` is not equal to 10?

a)

if x != 10:

b)

if x <> 10:

c)

if x not 10:

d)

if x ~= 10:

16.

What is the correct way to use logical operators for combining conditions in Python?

a)

if condition1 && condition2:

b)

if condition1 and condition2:

c)

if condition1 & condition2:

d)

if condition1 + condition2:

17.

Which of the following is a valid syntax for an if-else statement in Python?

a)

if condition: code block else: code block

b)

if condition: code block; else: code block

c)

if condition: code block else code block

d)

if condition: code block else: code block

18.

In Python, how do you write an if statement to check if `x` is between 5 and 10, inclusive?

a)

if x > 5 and x < 10:

b)

if 5 <= x <= 10:

c)

if x >= 5 or x <= 10:

d)

if x == 5 and x == 10: