Chapter 3: Control Flow


1. Conditional Statements

Conditional statements execute different code blocks based on conditions.

1.1 if Statement

The simplest form of control flow. If a condition is True, the code inside the if block is executed.

Syntax:

if condition:
    # Code to execute if the condition is True

Example:

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

1.2 if-else Statement

Provides an alternative code block if the condition is False.

Syntax:

if condition:
    # Code to execute if the condition is True
else:
    # Code to execute if the condition is False

Example:


1.3 if-elif-else Statement

Tests multiple conditions in sequence.

Syntax:

Example:


1.4 Nested if Statements

if statements can be nested for more complex conditions.

Example:


1.5 Ternary Conditional Expression

A one-liner for if-else statements.

Syntax:

Example:


1.6 Precautions with Conditionals

  1. Proper Indentation: Python relies on indentation to define blocks. A mismatch will cause errors.

  2. Avoid Overcomplicating: Use elif instead of nesting multiple if statements.

  3. Boolean Values: Avoid redundancy. For example, instead of:

    Simply use:


2. Loops

Loops allow you to execute code repeatedly.

2.1 while Loop

Repeats as long as a condition is True.

Syntax:

Example:

Precautions:

  • Infinite Loops: Ensure the condition eventually becomes False.

  • Use a break statement to exit loops if necessary.


2.2 for Loop

Iterates over a sequence (e.g., list, string, range).

Syntax:

Example:


2.3 range() in for Loops

The range() function generates a sequence of numbers, commonly used in for loops.

Examples:


2.4 break and continue

  • break: Exit the loop entirely.

  • continue: Skip to the next iteration.

Examples:


2.5 Nested Loops

Loops inside other loops.

Example:


3. Control Flow Tools

3.1 The pass Statement

Does nothing; acts as a placeholder.

Example:


3.2 The else Clause in Loops

A for or while loop can have an else clause. The else block runs if the loop completes normally (i.e., no break).

Example:


4. Comprehensions

4.1 List Comprehensions

A concise way to create lists.

Example:

4.2 Dictionary Comprehensions

Create dictionaries in one line.

Example:


5. Precautions and Tricks

  1. Avoid Infinite Loops:

    • Always ensure the loop condition will eventually be False.

  2. Keep Logic Simple:

    • Use comprehensions where possible for clarity and efficiency.

  3. Don’t Modify a Collection While Iterating:

    • If you need to modify a list while looping, iterate over a copy:

  4. else in Loops:

    • Remember that the else block executes only if the loop wasn't terminated by break.


Summary Table: Control Flow Tools

Control Flow Tool
Description
Example

if

Executes block if condition is True.

if x > 5: print(x)

if-else

Adds an alternative block.

if x > 5: ... else: ...

if-elif-else

Tests multiple conditions.

if x > 5: ... elif x == 5: ...

while

Repeats as long as condition is True.

while x < 5: ...

for

Iterates over a sequence.

for item in list: ...

break

Exits the loop entirely.

if x == 3: break

continue

Skips to the next iteration.

if x == 3: continue

pass

Does nothing (placeholder).

if x > 5: pass

else in loops

Executes if loop completes normally.

for x in range(5): ... else: ...

Comprehensions

One-line list or dict creation.

[x**2 for x in range(5)]


Last updated