What this topic tests
- Trace variables line by line instead of pattern-matching the code.
- Notice when a function or method mutates an object in place.
- Check spaces, newlines, quotes, and print separators before answering.
Common mistakes
- Reading the final expression but ignoring a mutation on an earlier line.
- Assuming print adds a space where sep or end changed the output.
- Answering with the value you expected rather than the characters Python actually writes.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming writes the line below. What will it print?
print(f"Milk tea {3+5} yuan")Show answer
Answer
Milk tea 8 yuanWhy: The braces hold an expression rather than text: Python evaluates 3+5 to 8 before formatting, so the label reads Milk tea 8 yuan.
-
2. What does `decode('zmlyhgzxovrhlugvmzhgvkkrmthglmv')` return?
from string import ascii_lowercase BLOCK_SIZE = 5 trtbl = str.maketrans(ascii_lowercase, ascii_lowercase[::-1]) def base_trans(text): return ''.join([character for character in text if character.isalnum()]).lower().translate(trtbl) def encode(plain): cipher = base_trans(plain) return ' '.join(cipher[idx:idx + BLOCK_SIZE] for idx in range(0, len(cipher), BLOCK_SIZE)) def decode(ciphered): return base_trans(ciphered) print(decode('zmlyhgzxovrhlugvmzhgvkkrmthglmv'))Show answer
Answer
anobstacleisoftenasteppingstoneWhy: With no spaces the whole run is one block. Each letter is mirrored and the 33 characters spell an obstacle is often a stepping stone.
-
3. What is the total price of the three coffees?
prices = [18, 22, 15] total = 0 for price in prices: total += price print(total)Show answer
Answer
55Why: The loop adds 18, 22 and 15 into total, and print shows 55; total must start at 0 or the first += would raise NameError. Accumulation is the whole pattern.
-
4. What is the largest element of the set?
s = {3, 1, 2} print(max(s))Show answer
Answer
3Why: max scans the set and returns the largest value, which is 3. Order does not matter because max compares values, not positions.
-
5. What does `rebase(10, [5], 2)` return?
def from_digits(digits, base): return sum(number * base ** idx for idx, number in enumerate(reversed(digits))) def to_digits(number, base_to): result = [] while number > 0: result.append(number % base_to) number //= base_to if result: return result[::-1] # list(reversed(result)) return [0] def rebase(from_base, digits, to_base): if from_base < 2: raise ValueError("input base must be >= 2") if to_base < 2: raise ValueError("output base must be >= 2") if any(True for idx in digits if idx < 0 or idx >= from_base): raise ValueError("all digits must satisfy 0 <= d < input base") return to_digits(from_digits(digits, from_base), to_base) print(rebase(10, [5], 2))Show answer
Answer
[1, 0, 1]Why: 5 in base 2 is 4 + 1, so the digits from the highest power down are 1, 0, 1. The function keeps dividing and collecting remainders.
-
6. The subclass overrides the member total method. What is printed?
class Order: def __init__(self, price): self.price = price def total(self): return self.price class MemberOrder(Order): def total(self): return round(self.price * 0.9, 2) print(MemberOrder(18).total())Show answer
Answer
16.2Why: The override multiplies the stored price by 0.9 and rounds to two decimals: 18 × 0.9 = 16.2, so the member price replaces the base one.
-
7. Revenue descending, name ascending. What is printed?
rows = [('Hubin', 1800, 'B'), ('Hubin', 1500, 'A'), ('Gaoxin', 1800, 'C')] print(sorted(rows, key=lambda r: (-r[1], r[0])))Show answer
Answer
[('Gaoxin', 1800, 'C'), ('Hubin', 1800, 'B'), ('Hubin', 1500, 'A')]Why: -1800 sorts before -1500, so the two 1800 rows come first, and the name decides between them because Gaoxin precedes Hubin.
-
8. Count occurrences of “latte”. What is printed?
print('latte latte americano'.count('latte'))Show answer
Answer
2Why: count reports non-overlapping occurrences, so the word 'latte' appears twice in that sentence. count is case-sensitive, so 'Latte' would not be included in those two.
-
9. The walrus operator assigns and then compares. What is printed?
if (n := 5) > 3: print(n)Show answer
Answer
5Why: The walrus assigns 5 to n inside the condition and hands back that value, so 5 > 3 is true and n is already bound when the body prints it.
-
10. Deduplicate headlines while preserving order. What is printed?
titles = ['New arrival', 'Member day', 'New arrival', 'Ranking'] seen = [] for title in titles: if title not in seen: seen.append(title) print('|'.join(seen))Show answer
Answer
New arrival|Member day|RankingWhy: The loop appends a title only when it is not already in seen, so the second 'New arrival' is dropped and the original order survives.
Common questions
How is output prediction graded?
The practice engine compares the exact trimmed text, including case, spaces, and line breaks.
Why are some answers on multiple lines?
A program may print several lines. Those questions use a textarea and expect the complete output in order.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.