Python Flow & Functions

Jump to the topic you need: conditionals, loops, and list comprehensions.

Python variables and conditionals concept

Python Conditional Statements

Conditional statements drive decision-making. They execute code only when a boolean expression is true.

Basic if

temperature = 30
if temperature > 25:
  print("It's a hot day!")

if / else

temperature = 20
if temperature > 25:
  print("Hot")
else:
  print("Cool")

if / elif / else

temperature = 15
if temperature > 25:
  print("Hot")
elif temperature > 15:
  print("Warm")
else:
  print("Cold")

Practice with relational (>, >=, <, <=) and logical (and, or, not) operators for richer conditions.

Challenge

Write a grading system using if/elif/else:

score = 83
if score >= 90:
  print("A")
elif score >= 80:
  print("B")
elif score >= 70:
  print("C")
elif score >= 60:
  print("D")
else:
  print("F")

Tip: Order matters—first matching branch wins.

Try it live (simple decision)

Run and modify the code below:


  

Try it: grading system

Adjust score and rerun:


  

Try it: logical operators

Experiment with and/or/not:


  

In Python, there are three forms of the if...else statement:

  1. if statement
  2. if...else statement
  3. if...elif...else statement
More examples
# Basic if
temperature = 30
if temperature > 25:
  print("It's a hot day!")

# if...else
temperature = 20
if temperature > 25:
  print("It's a hot day!")
else:
  print("It's a cool day!")

# if...elif...else
temperature = 15
if temperature > 25:
  print("It's a hot day!")
elif temperature > 15:
  print("It's a warm day!")
else:
  print("It's a cold day!")

đź”— More Resources:

Python data types and list comprehension

List Comprehensions

Concise, expressive syntax for generating and transforming lists.

Syntax

[expression for item in iterable if condition]

Optional condition filters items.

Basic Example

# Traditional loop
squares = []
for x in range(5):
  squares.append(x**2)

# List comprehension
squares = [x**2 for x in range(5)]
print(squares)

Filtering

even = [x for x in range(10) if x % 2 == 0]
print(even)

Nested

matrix = [[i*j for j in range(1,4)] for i in range(1,4)]
print(matrix)

Transforming

names = ["Alice","Bob","Charlie"]
upper = [n.upper() for n in names]
print(upper)

Flatten

nested = [[1,2],[3,4],[5,6]]
flat = [item for group in nested for item in group]
print(flat)

Complex? Use loops

# Hard to read comprehension
result = [x*y for x in range(10) for y in range(5) if x + y > 5]

# Clearer loop
result = []
for x in range(10):
  for y in range(5):
    if x + y > 5:
      result.append(x*y)

Tip: Prefer clarity over cleverness—readability scales.


Try it live: build and transform lists

Basic squares vs comprehension:


  

Filtering even numbers:


  

Flatten & condition in one go:


  
Python Loops

Loops in Python

Loops repeat a block of code. Two primary forms: for (definite) and while (indefinite).

For Loop

fruits = ["apple","banana","cherry"]
for f in fruits:
    print(f"I love {f}!")

range()

for n in range(1, 6):
    print("Number:", n)

While Loop

count = 1
while count <= 5:
    print("Count:", count)
    count += 1

break / continue

for n in range(1, 10):
    if n == 5:
        print("Break at 5")
        break
    elif n % 2 == 0:
        continue
    print("Process", n)

Nested Loops

for i in range(1,4):
    for j in range(1,4):
        print(i, j)

Use break to exit, continue to skip, and nest loops carefully—they scale multiplicatively.


Try it live: loop patterns

Sum numbers with for:


    

Countdown with while:


    

Break / continue demo: