← Python Crash Course

PCC·05 · Chapter record

if Statements

Chapter 5 — teaching your programs to make decisions

Done

Five sessions on branching logic: writing conditional tests, combining them with and/or/in, if/else, if-elif-else chains, and putting if statements to work inside loops over lists. Each one explains the concept, lists the key terms, then gives you exercises — try them yourself before revealing the code.

5.1

Equality & Comparison Tests

  • At the heart of every if statement is a conditional test — an expression that evaluates to True or False.
  • == checks equality and != checks inequality — a single = assigns a value, a double == asks a question.
  • Equality is case-sensitive: 'Audi' == 'audi' is False. Convert with .lower() before comparing if case shouldn't matter — it doesn't change the original variable.
  • The usual numeric comparisons work too: < <= > >=.
conditional test
an expression that evaluates to True or False
==
checks whether two values are equal
.lower()
converts a string to lowercase, for case-insensitive comparisons
Activity 1

Write at least five conditional tests using ==, !=, <, <=, >, and >=. Predict True or False before you run each one.

Activity 2

Store a name with mixed capitalization, then write a test that compares it to a lowercase version using .lower().

▶ Comparison Lab