Functions that return True or False (Python)

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

Functions that return True or False (Python)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the cleaner way to use a Boolean function as an if condition?
A if is_even(n):
B if is_even(n) == True:
2. Which function correctly returns a comparison result?
A def is_adult(age): return age >= 18 == "True"
B def is_adult(age): return age >= 18
3. is_even(7) returns False. What does if not is_even(7): do?
A Runs the indented code, because not False is True
B Skips the indented code, because the function returned False
4. Which line correctly checks that BOTH conditions are true using Boolean functions?
A if is_even(n) and is_positive(n):
B if is_even(n) and is_positive:
5. Which while loop keeps going as long as the number is NOT even?
A while is_even(n) not:
B while not is_even(n):

Match the Pairs

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

Left columnRight column
1def is_even(n): return n % 2 == 0 AThe function prints the result instead of returning it, so the call gives None and an if cannot use it. Use return instead of print.
2if is_even(n): Bdef is_adult(age): return age >= 18
3A Boolean function can return the result of a comparison directly, with no if needed CCalls a function that returns True or False and uses the result directly as the if condition.
4Boolean functions can be combined in one condition with not, and, or DComparing the result to True is unnecessary. The function already returns a Boolean, so write if is_even(n): instead.
5if is_even(n) == True: print("Even") Eif is_even(n) and not is_negative(n):
6def is_even(n): print(n % 2 == 0) FDefines a Boolean function that returns True when n is even and False when it is odd.

Definitions

if is_even(n):

Fill in the Blanks

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

1.
def adult(age): return age __ 18 print(adult(20))
2.
def even(n): return n % 2 == 0 if ____(8): print('Even number')
3.
def even(n): return n % 2 == 0 if ___ even(7): print('Odd')
4.
def adult(age): return age >= 18 ticket = True if adult(20) ___ ticket: print('Enter')
5.
def even(n): return n % 2 == 0 n = 7 _____ not even(n): n += 1

Spot the Error

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

Question 1
A def is_even(n):
B return n % 2 = 0
C if is_even(4):
D print('Even')
Question 2
A def is_adult(age):
B return age >= 18
C age = 20
D if is_adult(age)
E print('Adult')
Question 3
A def is_even(n):
B return n % 2 == 0
C if !is_even(7):
D print('Odd')
Question 4
A def is_pass(mark):
B print(mark >= 50)
C if is_pass(65):
D print('Pass')
Question 5
A def is_vowel(ch):
B return ch in 'aeiou'
C word = 'apple'
D if is_vowel[word[0]]:
E print('Vowel')

Answer Key

Functions that return True or False (Python)
Section A: Quick Questions
Q1
A — if is_even(n):
Q2
B — def is_adult(age): return age >= 18
Q3
A — Runs the indented code, because not False is True
Q4
A — if is_even(n) and is_positive(n):
Q5
B — while not is_even(n):
Section B: Match the Pairs
Q1
def is_even(n): return n % 2 == 0 → Defines a Boolean function that returns True when n is even and False when it is odd.
Q2
if is_even(n): → Calls a function that returns True or False and uses the result directly as the if condition.
Q3
A Boolean function can return the result of a comparison directly, with no if needed → def is_adult(age): return age >= 18
Q4
Boolean functions can be combined in one condition with not, and, or → if is_even(n) and not is_negative(n):
Q5
if is_even(n) == True: print("Even") → Comparing the result to True is unnecessary. The function already returns a Boolean, so write if is_even(n): instead.
Q6
def is_even(n): print(n % 2 == 0) → The function prints the result instead of returning it, so the call gives None and an if cannot use it. Use return instead of print.
Section C: Definitions
Q1
if is_even(n): — Calls a function that returns True or False and uses that result directly as the if condition. You do not need to write == True. It can also be combined with not, and, or.
Section D: Fill in the Blanks
Q1
def adult(age): return age >= 18 print(adult(20))
Q2
def even(n): return n % 2 == 0 if even(8): print('Even number')
Q3
def even(n): return n % 2 == 0 if not even(7): print('Odd')
Q4
def adult(age): return age >= 18 ticket = True if adult(20) and ticket: print('Enter')
Q5
def even(n): return n % 2 == 0 n = 7 while not even(n): n += 1
Section E: Spot the Error
Q1
B — A single = assigns a value and is not allowed inside a return comparison. Use == to compare: return n % 2 == 0.
Q2
D — An if statement must end with a colon, even when its condition is a function call that returns True or False. It should be if is_adult(age):
Q3
C — Python does not use ! for negation. Put the word not in front of the Boolean function call: if not is_even(7):
Q4
B — The function prints the comparison instead of returning it, so it gives back None and the if never sees True. It should be return mark >= 50.
Q5
D — A function is called with round brackets, not square brackets. It should be if is_vowel(word[0]):