Python Cheat Sheet Part 02 - Control Flow (If, Else, Loops)

Welcome to Cyber Supto! I'm Supto, and this Python Cheat Sheet series is designed to help you quickly understand Python with practical examples and quick references.

In Part 01 we learned Python basics like variables, data types, and operators. Now in this part, we will explore control flow — how Python makes decisions and repeats tasks using conditions and loops.

What is Control Flow?

Control flow determines how a program executes code based on conditions or repetition. Python mainly uses:

  • Conditional statements (if, elif, else)
  • Loops (for loop, while loop)
  • Loop control statements (break, continue, pass)

If Statement

The if statement allows a program to make decisions.

age = 18

if age >= 18:
    print("You are an adult")

If the condition is true, the code inside the block will run.

If-Else Statement

Use else when you want an alternative block of code.

age = 16

if age >= 18:
    print("You can vote")
else:
    print("You cannot vote yet")

If-Elif-Else

Use elif to check multiple conditions.

score = 75

if score >= 90:
    print("Grade A")
elif score >= 70:
    print("Grade B")
elif score >= 50:
    print("Grade C")
else:
    print("Fail")

Nested If Statements

You can place an if statement inside another.

age = 20
has_id = True

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

Python Indentation

Python uses indentation instead of brackets.

Correct:

if True:
    print("Correct indentation")

Incorrect:

if True:
print("Error")

Use 4 spaces for indentation (recommended).

For Loop

A for loop is used to iterate over a sequence.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Loop Through a List

languages = ["Python", "JavaScript", "C++"]

for lang in languages:
    print(lang)

The range() Function

The range() function generates numbers.

range(5)
range(2, 10)
range(1, 10, 2)

Example:

for i in range(1, 6):
    print(i)

While Loop

A while loop runs as long as the condition is true.

count = 0

while count < 5:
    print(count)
    count += 1

Infinite Loop Warning

If the condition never becomes false, the loop will run forever.

while True:
    print("Running forever")

Use carefully.

Break Statement

break stops a loop immediately.

for i in range(10):
    if i == 5:
        break
    print(i)

Continue Statement

continue skips the current iteration.

for i in range(5):
    if i == 2:
        continue
    print(i)

Pass Statement

pass acts as a placeholder.

for i in range(5):
    pass

Loop Else Clause

Python loops can have an else block.

for i in range(3):
    print(i)
else:
    print("Loop finished")

Quick Tips

  • Use if statements for decision making
  • Use for loops when iterating over lists or ranges
  • Use while loops when the number of iterations is unknown
  • Avoid infinite loops unless intentionally required
  • Keep conditions simple and readable

Other Parts of This Python Cheat Sheet

This cheat sheet is part of the Cyber Supto Python Learning Series, built to give developers quick references while coding.

Thanks for reading,
Cyber Supto.