Mr Laliberte

Flow Control with if-else

Introduction to Programming · Control Flow · Lesson 2

Courses › Introduction to Programming › Flow Control with if-else

Flow control statements let a program decide which instructions to run and which to skip, instead of just running every line from top to bottom. These decisions are based on conditions that evaluate to True or False.

1. The Boolean Data Type

Booleans are a data type with only two possible values: True and False. They must be capitalized exactly like this (not “true” or “TRUE”), and they are never written in quotes, quotes would make them strings, not Booleans.

spam = True

You cannot use True or False as variable names, Python reserves them as keywords, so trying to assign to them causes a SyntaxError.

2. Comparison Operators

Comparison (relational) operators compare two values and evaluate down to a single Boolean value.

Operator Meaning Example
== Equal to 4 == 2 + 2 is True
!= Not equal to 1 != 2 is True
< Less than 1.999 < 5 is True
> Greater than 99 > 4 + 8 is True
<= Less than or equal to 5 <= 5 is True
>= Greater than or equal to 5 >= 4 is True
42 == 42 # True``42 == 99 # False``'dog' != 'cat' # True``42 == '42' # False (an int is never equal to a str)``42 == 42.0 # True (an int CAN equal a float)``eggs = 42``eggs <= 42 # True
Watch out!• = is the assignment operator (stores a value).• == is the equal-to comparison operator (compares two values).• Mixing these up — or typing < instead of <= — is one of the most common beginner bugs.

3. Boolean Operators: and, or, not

The Boolean operators and, or, and not combine or modify Boolean values. and and or are binary (they need two Boolean values); not is unary (it needs only one).

And, True only if both sides are True

Expression Evaluates to
True and True True
True and False False
False and True False
False and False False

Or, True if either side is True

Expression Evaluates to
True or True True
True or False True
False or True True
False or False False

Not, flips the Boolean value

Expression Evaluates to
not True False
not False True

Comparison operators and Boolean operators are often combined:

(4 < 5) and (5 < 6)   # True
(4 < 5) and (9 < 6)   # False
(1 == 2) or (2 == 2)  # True

Order of operations: math operators evaluate first, then comparison operators, then Boolean operators, and among Boolean operators, not evaluated first, then and, then or.

4. Conditions and Blocks

A condition is just an expression that evaluates to True or False. It’s the same idea as an expression, but the word “condition” is used in the context of flow control statements.

A block is a group of lines of code, marked by indentation. Python uses indentation (not curly braces) to show which lines belong together.

username = 'Mary'
password = 'swordfish'
if username == 'Mary':
    print('Hello, Mary')
    if password == 'swordfish':
        print('Access granted.')
    else:
        print('Wrong password.')

5. if Statements

The if statement runs its clause only when its condition is True. The clause is skipped if the condition is False.

name = 'Alice'
if name == 'Alice':
    print('Hi, Alice.')

6. else Statements

An else statement (no condition of its own) runs its clause only when the if statement’s condition was False.

name = 'Alice'
if name == 'Alice':
    print('Hi, Alice.')
else:
    print('Hello, stranger.')

7. elif Statements

An elif (“else if”) statement provides another condition that is checked only if all the conditions above it were False. You can chain as many elif statements as you want, and only one clause in the whole chain will ever run.

name = 'Alice'
age = 33
if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')

Order matters! Once a True condition is found, the rest of the elif clauses are automatically skipped, even if a later condition would also have been True.

name = 'Carol'
age = 3000
if name == 'Alice':
    print('Hi, Alice.')
elif age < 12:
    print('You are not Alice, kiddo.')
elif age > 100:              # checked first...
    print('You are not Alice, grannie.')
elif age > 2000:             # never runs
    print('Unlike you, Alice is not an undead, immortal vampire.')

An else statement can optionally follow the last elif. This guarantees that exactly one clause in the chain will run: if every if/elif condition is False, the else clause runs instead.

name = 'Carol'``age = 3000``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.')
Rules for if / elif / else• There is always exactly one if statement, and it always comes first.• Any number of elif statements can follow, order matters! Once a condition is True, the rest are skipped.• At most one clause in the whole chain will run.• An optional else statement goes last and guarantees one clause runs if every if/elif condition was False.

Quick Reference Card

SymbolMeaning
Boolean valuesTrue False
Comparison operators== != < > <= >=
Boolean operatorsand or not
if statementif condition:
else statementelse:
elif statementelif condition:

Welcome back