Python Programming · Middle Stage (6–8) · 2 courses, 26 worked problems, 52 practice questions
A program often has to choose. A heater with a thermostat turns on when the room is cold and off when it is warm. In Python, the if statement runs a block of code only when a condition is True. You add elif to test another condition and else for every other case. The code inside each branch is indented. Conditions use comparisons such as > and ==, and you can join them with and and or. With these, a program can give a grade from a mark or decide whether a number is even.
if, elif and else statement with indented blocksand, or and notChoose an answer, then check it. Each one shows a full explanation.
An if statement has another if inside it, and the first test compares two strings.
word = "Loop"
if word < "loop":
if len(word) > 3:
print("A")
else:
print("B")
elif word == "loop":
print("C")
else:
print("D")
What does the program print?
A program checks a basket, which is a list, before it adds a fruit. basket = ["apple", "pear"] fruit = "plum" if fruit in basket: print("have it") elif len(basket) < 3: basket.append(fruit) print("added", len(basket)) else: print("full") What does the program print?
A function picks a word for a temperature with if, elif and a final return.
def label(t):
if t > 30:
return "hot"
elif t > 20:
return "warm"
elif t > 10:
return "mild"
return "cold"
print(label(30), label(10), label(21))
What does the program print?
Each one has a hint, the key idea and a full worked solution.
A function sorts values by whether they count as true or false in an if test.
def describe(x):
if x:
return "full"
elif x == 0:
return "zero"
else:
return "empty"
print(describe([]), describe(0.0), describe(" "), describe(None))
What does the program print?
A program reads an operator and a number from the keyboard, and passes both to a function. The user types + on the first line and 3 on the second.
def apply(op, a, b):
if op == "+":
return a + b
elif op == "*":
return a * b
return "?"
op = input()
x = input()
print(apply(op, x, x), apply("-", 1, 2))
What does the program print?
Each number of a list should give one letter: A, B or C. The test in the if line is left out, and ____ stands in its place.
for n in [-2, 0, 3]:
if ____:
print("A", end="")
elif n:
print("B", end="")
else:
print("C", end="")
print()
Which condition fills the gap so that the output is exactly BCA?
A function decides with not, or and and in one condition, and has no brackets to show the order.
def pick(x, y):
if not x or y and x > y:
return "first"
elif x == y or not y:
return "second"
return "third"
print(pick(0, 5), pick(4, 2), pick(3, 3), pick(2, 9))
What does the program print?
A program uses or and and on values that are not Boolean.
a = 0
b = ""
c = "go"
print(a or b or c, c and 5, a and c)
What does the program print?
A function prints its argument and then answers a question about it. def check(n): print(n, end=" ") return n > 1 if check(1) and check(2) or check(3): print("yes") What does the program print?
A program uses chained comparisons and a mixed condition. x = 5 print(1 < x < 10, 10 > x > 7, x == 5 != 4, not x > 3 or x) What does the program print?