wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Conditional Statements

Total questions: 8

Worksheet time: 4mins

Name
Class
Date
1.

In a Python program, a control structure:

a)

Directs the order of execution of the statements in the program

b)

Manages the input and output of control characters

c)

Defines program-specific data structures

d)

Dictates what happens before the program starts and after it terminates

2.

Which one of the following if statements will not execute successfully:

a)

if (1, 2):

print('foo')

b)

if (1, 2):


print('foo')

c)

if (1, 2):

print('foo')

d)

if (1, 2): print('foo')

3.

What signifies the end of a statement block or suite in Python?

a)

end

b)

}

c)

A line that is indented less than the previous line

d)

A comment

4.

What is the output of the following code snippet:

if 'bar' in {'foo': 1, 'bar': 2, 'baz': 3}:

print(1)

print(2)

if 'a' in 'qux':

print(3)

print(4)

a)

4

b)

1

2

3

4

c)

It doesn’t generate any output.

d)

1

2

4

5.

The following if/elif/else statement will raise a KeyError exception:

1 d = {'a': 0, 'b': 1, 'c': 0}

2

3 if d['a'] > 0:

4 print('ok')

5 elif d['b'] > 0:

6 print('ok')

7 elif d['c'] > 0:

8 print('ok')

9 elif d['d'] > 0:

10 print('ok')

11 else:

12 print('not ok')

a)

False

b)

True

6.

Which of the following are valid if/else statements in Python, assuming x and y are defined appropriately:

a)

if x < y: print('foo'); print('bar'); print('baz')

b)

if x < y: print('foo')

elif y < x: print('bar')

else: print('baz')

c)

if x < y: print('foo') else: print('bar')

d)

if x < y: if x > 10: print('foo')

7.

What is value of this expression:


'a' + 'x' if '123'.isdigit() else 'y' + 'b'

a)

'axb'

b)

'ax'

c)

'axyb'

d)

'ab'

8.

Suppose you have two variables x and y defined. Write a stub if statement to evaluate whether x is less than y. The statement should not do anything, even if the condition is true.

a)

if x < y:

pass

b)

if x < y:

break

c)

pass

d)

None of the Above