University of Geneva · CUI BSc · Algorithmics & Data Structures
Python Basics I: Control Structures
Seminar 2
1 / 11
Today's Agenda
- Truthiness — Python's approach to True / False
- Branching and loops: if / for / while
- Why complexity matters — preview for later
- Big O intuition (preview)
- List comprehensions — concise & fast
2 / 11
Truthiness in Python
Python treats certain values as False in boolean context — these are called falsy.
# Falsy values
bool(0) # False
bool(0.0) # False
bool('') # False — empty string
bool([]) # False — empty list
bool({}) # False — empty dict
bool(None) # False
Everything else is truthy (non-zero numbers, non-empty containers, objects).
# Pythonic checks
if not items: # ✓ check for empty list
if items == []: # less idiomatic; only checks lists
if x is None: # ✓ identity check
if x == None: # avoid; prefer 'is None'
if 0 < x < 100: # ✓ chained comparison
Pitfall: mutable default arguments interact badly with truthiness checks.
3 / 11
Branching with if/elif/else + for
if / elif / else — choose one path based on conditions:
score = 78
if score >= 90:
grade = 'A'
elif score >= 75:
grade = 'B'
else:
grade = 'F'
for — iterate over a sequence (range, list, dict, ...):
for i, name in enumerate(['Alice', 'Bob']):
print(i, name) # 0 Alice, 1 Bob
4 / 11
while + break/continue
while repeats while a condition is True.
count = 0
while count < 5:
count += 1
if count == 2:
continue # skip 2
if count == 4:
break # stop early at 4
print(count) # prints: 1, 3
5 / 11
List Comprehensions — From Loop to One-liner
| Pattern | Traditional for-loop | List comprehension |
| Basic transform | squares = []
for x in range(10):
squares.append(x**2) | squares = [x**2 for x in range(10)] |
| With filter | evens = []
for x in range(20):
if x % 2 == 0:
evens.append(x) | evens = [x for x in range(20) if x % 2 == 0] |
6 / 11
Preamble: Why Does Complexity Matter?
- This is a preview for intuition; formal Big-O is in Seminar 6
- Every algorithm consumes resources: time and memory
- As input grows, poorly scaling code hits limits quickly
- A brute-force idea may work for 100 items, not 1,000,000
- Big O describes how resource usage grows with input size n
- Two key dimensions:
- Time complexity — how many steps / operations
- Space complexity — how much memory is used
- Goal: pick algorithms that scale well for the problem at hand
- Today we only build intuition; full formal treatment comes later
7 / 11
Why Big O Matters (Preview)
At n = 5000 the nested loop takes 0.9 s while the single loop finishes in 0.0002 s — that is a 4500× difference. This slide is intuition, not formal proof.
8 / 11
Today's Exercises
- Ex 1 — find_max(a,b,c) using if/elif/else
- Ex 2 — FizzBuzz (classic interview question)
- Ex 3 — Leap year checker
- Ex 4 — Calculator loop: keep asking until 'quit'
- Ex 5 — Print multiplication table for n
9 / 11
Key Takeaways
- Python's truthiness: use if not items, if x is None
- if / for / while cover all control flow you need
- Prefer enumerate() over range(len(...))
- Big O preview: O(n) and O(n²) scale very differently
- List comprehensions are concise for simple transforms
- Always think: how many times does this loop run?
10 / 11
University of Geneva · CUI BSc · Algorithmics & Data Structures
Thank You & Have Fun!
11 / 11