Font size
WorksheetsPython Conditional Statements
Total questions: 8
Worksheet time: 4mins
In a Python program, a control structure:
Directs the order of execution of the statements in the program
Manages the input and output of control characters
Defines program-specific data structures
Dictates what happens before the program starts and after it terminates
Which one of the following if statements will not execute successfully:
if (1, 2):
print('foo')
if (1, 2):
print('foo')
if (1, 2):
print('foo')
if (1, 2): print('foo')
What signifies the end of a statement block or suite in Python?
end
}
A line that is indented less than the previous line
A comment
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)
4
1
2
3
4
It doesn’t generate any output.
1
2
4
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')
False
True
Which of the following are valid if/else statements in Python, assuming x and y are defined appropriately:
if x < y: print('foo'); print('bar'); print('baz')
if x < y: print('foo')
elif y < x: print('bar')
else: print('baz')
if x < y: print('foo') else: print('bar')
if x < y: if x > 10: print('foo')
What is value of this expression:
'a' + 'x' if '123'.isdigit() else 'y' + 'b'
'axb'
'ax'
'axyb'
'ab'
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.
if x < y:
pass
if x < y:
break
pass
None of the Above
