If Statements

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

If Statements

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does the if statement check?
A Whether a condition is True
B How many times to loop
2. What goes at the end of an if line?
A A semicolon ;
B A colon :
3. How does Python know what's inside an if block?
A Indentation (spaces)
B Curly braces {}
4. if 10 > 5:\n print('Yes')\nWhat prints?
A Nothing
B Yes
5. if 3 > 7:\n print('Yes')\nWhat prints?
A Nothing
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if x > 5 print('big') AOk: Always runs
2if x > 5: print('big') BError: Missing colon after if
3if age >= 18: print('Adult') CError: Body must be indented
4if True: print('yes') DOutput: big
5x = 10 if x > 5: print('big') EOutput: (nothing)
6x = 3 if x > 5: print('big') FValid: if with colon + indent

Definitions

if

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
__ 5 > 3: print('Yes')
2.
if age >= 13_ print('Teen')
3.
if x == 10: _____('Match')
4.
if score __ 50: print('Pass')
5.
if name __ 'Alice': print('Hi')

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A if 5 > 3: print('Yes')
B if 5 > 3 print('Yes')
C if 10 > 8: print('Big')
D if x > 0: print('Positive')
Question 2
A if 5 > 3: print('Yes')
B if x > 0: print('Positive')
C if done: print('Done')
D if ok: print('OK')
Question 3
A if 5 > 3: print('Yes')
B if ready: print('Go')
C If 10 > 5: print('Big')
D if name: print('Hi')
Question 4
A if done: print('Done') print('OK')
B if ok: print('A') print('B')
C if ready: print('Go') print('Start')
D if 5 > 3: print('Yes') print('Done')
Question 5
A if score == 10: print('Match')
B if x = 5: print('Five')
C if age == 18: print('Adult')
D if n == 0: print('Zero')

Answer Key

If Statements
Section A: Quick Questions
Q1
A — Whether a condition is True
Q2
B — A colon :
Q3
A — Indentation (spaces)
Q4
B — Yes
Q5
A — Nothing
Section B: Match the Pairs
Q1
if x > 5 print('big') → Error: Missing colon after if
Q2
if x > 5: print('big') → Error: Body must be indented
Q3
if age >= 18: print('Adult') → Valid: if with colon + indent
Q4
if True: print('yes') → Ok: Always runs
Q5
x = 10 if x > 5: print('big') → Output: big
Q6
x = 3 if x > 5: print('big') → Output: (nothing)
Section C: Definitions
Q1
if — Runs the indented code only when the condition is True.~~Condition
Section D: Fill in the Blanks
Q1
if 5 > 3: print('Yes')
Q2
if age >= 13: print('Teen')
Q3
if x == 10: print('Match')
Q4
if score >= 50: print('Pass')
Q5
if name == 'Alice': print('Hi')
Section E: Spot the Error
Q1
B — Missing colon at the end of the if line
Q2
A — The print line must be indented (4 spaces) inside the if block
Q3
C — if must be lowercase — If is not valid Python
Q4
D — Inconsistent indentation — both lines must use the same number of spaces
Q5
B — Use == to compare, not = (which assigns a value)