University of Geneva · CUI BSc · Algorithmics & Data Structures

Python Basics I: Control Structures

Seminar 2

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

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.

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

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

List Comprehensions — From Loop to One-liner

PatternTraditional for-loopList comprehension
Basic transformsquares = [] for x in range(10): squares.append(x**2)squares = [x**2 for x in range(10)]
With filterevens = [] for x in range(20): if x % 2 == 0: evens.append(x)evens = [x for x in range(20) if x % 2 == 0]

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

Why Big O Matters (Preview)

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.

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

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?
University of Geneva · CUI BSc · Algorithmics & Data Structures

Thank You & Have Fun!