Why One-Liners Matter
HKDSE markers don't award bonus marks for style — but concise code shows understanding. Here are the one-liners every top student knows.
1. Sum of Squares
sum(x**2 for x in range(1, 11)) # 385 (sum of 1² + 2² + ... + 10²)
2. Check All Positive
all(x > 0 for x in lst) # True if every x > 0
3. Check Any Negative
any(x < 0 for x in lst) # True if at least one negative
4. Reverse a String
"HKDSE"[::-1] # "ESDKH"
5. Check Palindrome
s == s[::-1] # True if palindrome
6. Count Characters
sum(1 for c in s if c.isupper()) # count uppercase letters
7. Unique Items
list(set(lst)) # remove duplicates
8. Most Common Value
max(set(lst), key=lst.count) # finds most frequent element
9. Flatten 2D List
[x for row in grid for x in row]
10. Join with Newlines
"\n".join(lines) # prints each line on its own
11. Capitalise Each Word
" ".join(w.capitalize() for w in text.split()) # "hong kong" → "Hong Kong"
12. Reverse Key-Value
{v: k for k, v in d.items()}
13. Filter Dictionary
{k: v for k, v in d.items() if v >= 50}
14. Count Vowels
sum(1 for c in text.lower() if c in "aeiou")
15. Is Sorted?
lst == sorted(lst) # True if already sorted ascending
⚠️ Don't overdo it. If a one-liner becomes unreadable, use a loop. Clarity beats cleverness in exams.