Skip to main content

Chapter 2 - if-else and Flow Control (Python)

Here's a concise walkthrough of the main ideas in Chapter 2, each with a small example.


Boolean values

Python has two Boolean values: True and False. They must be capitalized exactly this way.

spam = True
eggs = False
print(type(spam)) # <class 'bool'>

Comparison operators

Comparison operators evaluate to True or False. There are six of them.

42 == 42     # True
42 != 99 # True
42 < 100 # True
42 > 100 # False
42 <= 42 # True
42 >= 43 # False

You can also compare strings:

"hello" == "hello"  # True
"hello" == "Hello" # False (case-sensitive)

The difference between == and =

== is the comparison operator (checks if two values are equal). = is the assignment operator (stores a value in a variable). They are not interchangeable.

spam = 42       # assignment: store 42 in spam
spam == 42 # comparison: is spam equal to 42? (True)

Boolean operators: and, or, not

and is True only if both sides are True. or is True if at least one side is True. not flips a Boolean.

True and True    # True
True and False # False
True or False # True
False or False # False
not True # False
not False # True

Mixing Boolean and comparison operators

You can combine comparison operators with Boolean operators for complex conditions.

age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")

temperature = 72
if temperature < 60 or temperature > 90:
print("Stay inside")

Conditions

A condition is just an expression that evaluates to True or False. Conditions are used in flow control statements to decide which code to run.

name = "Alice"
name == "Alice" # This is a condition — evaluates to True

Blocks of code

A block of code is a group of indented lines that belong together. Python uses indentation (not braces) to define blocks. The rules are:

  1. Blocks begin when the indentation increases.
  2. Blocks can contain other blocks (nesting).
  3. Blocks end when the indentation decreases back to zero or to a containing block's indentation.
if name == "Alice":
print("Hi, Alice!") # start of block
print("How are you?") # still in the block
if age > 18:
print("You are an adult.") # nested block
print("Done") # outside all blocks

Program execution

By default, Python executes code from top to bottom, one line at a time. Flow control statements can cause the execution to skip or jump to different lines instead of just going straight down.


if statements

An if statement runs a block of code only when the condition is True. If the condition is False, the block is skipped.

name = "Alice"
if name == "Alice":
print("Hi, Alice!")

else statements

An else clause runs when the if condition is False. It provides an alternative path.

password = "fish"
if password == "swordfish":
print("Access granted")
else:
print("Wrong password")

elif statements

elif (else if) lets you check multiple conditions in order. Only the first matching branch runs.

age = 12
if age < 5:
print("Free entry")
elif age < 13:
print("Child ticket")
elif age < 65:
print("Adult ticket")
else:
print("Senior ticket")

Order matters: because conditions are checked top to bottom, the first true condition wins. If you put age < 65 before age < 13, a 12-year-old would get "Adult ticket" instead of "Child ticket".


Adding a final else

A final else after your elif chain guarantees that at least one block will always run, even if none of the conditions are True.

name = "Bob"
age = 30
if name == "Alice":
print("Hi, Alice.")
elif age < 12:
print("You are not Alice, kiddo.")
else:
print("You are neither Alice nor a little kid.")

A short program: Opposite Day

This example uses a Boolean variable to flip the logic of an if/else statement.

name = input("Enter your name: ")
is_opposite_day = True

if is_opposite_day:
if name != "Alice":
print("Hello, Alice!")
else:
print("I don't know you.")
else:
if name == "Alice":
print("Hello, Alice!")
else:
print("I don't know you.")

A short program: Dishonest Capacity Calculator

This example uses Boolean operators with user input and conditional logic to check venue capacity honestly (or dishonestly).

capacity = 30
guests = int(input("How many guests? "))
bribe = input("Has the inspector been bribed? (yes/no) ")

if guests <= capacity:
print("You're within capacity.")
elif bribe == "yes":
print("We'll look the other way.")
else:
print("You are over capacity! Shut it down!")

Overall idea of the chapter

The chapter's main message is: flow control lets your program make decisions using if, elif, and else statements, powered by Boolean values and comparison/Boolean operators. Code blocks are defined by indentation, conditions are checked in order, and these tools let your programs respond differently to different situations instead of just running top to bottom.