Built with Tutify.io

    AIStudyLabs

    Indentation and Code Blocks

    Indentation and Code Blocks

    Introduction

    In this lesson, you’ll learn how Python uses indentation to define code blocks instead of braces ({} and }). You’ll see how indentation controls the flow of your programs, how to structure common constructs like if statements, loops, and functions, and how to avoid (and fix) typical IndentationError issues. By the end, you’ll be able to use indentation both correctly and intentionally to make your code clean and readable.


    Why Indentation Matters in Python

    In many languages, code blocks are defined with braces:

    if (x > 10) {
        printf("Big number");
    }
    

    Python removes the braces and uses indentation to mark the start and end of blocks:

    if x > 10:
        print("Big number")
    

    Everything at the same indentation level is part of the same block. When the indentation returns to a previous level, the previous block ends.

    Key rules:


    Structuring if, elif, else Blocks

    An if block in Python looks like this:

    age = 17
    
    if age < 13:
        category = "child"
    elif age < 18:
        category = "teenager"
    else:
        category = "adult"
    
    print("Category:", category)
    
    # Output
    Category: teenager
    

    Explanation:

    Nested Conditional Blocks

    You can nest if blocks by adding another level of indentation:

    score = 92
    
    if score >= 90:
        grade = "A"
        if score >= 95:
            plus = "+"
        else:
            plus = ""
    else:
        grade = "B or below"
        plus = ""
    
    print("Final grade:", grade + plus)
    
    # Output
    Final grade: A-
    

    Explanation:


    Indentation in Functions and Loops

    Functions with Indented Bodies

    Function bodies are also code blocks defined by indentation:

    def describe_number(n: int) -> str:
        if n == 0:
            return "zero"
        elif n % 2 == 0:
            return "even"
        else:
            return "odd"
    
    result = describe_number(7)
    print(result)
    
    # Output
    odd
    

    Explanation:

    Loops with Nested Blocks

    Loops work the same way:

    numbers = [1, 2, 3, 4, 5, 6]
    
    for n in numbers:
        if n % 2 == 0:
            print(n, "is even")
        else:
            print(n, "is odd")
    
    # Output
    1 is odd
    2 is even
    3 is odd
    4 is even
    5 is odd
    6 is even
    

    Explanation:


    Deeply Nested Blocks: A Practical Example

    This example combines a function, a loop, and conditionals, showing multiple indentation levels:

    def filter_and_label_numbers(numbers):
        labels = []
    
        for n in numbers:
            if n < 0:
                label = "negative"
            elif n == 0:
                label = "zero"
            else:
                if n % 2 == 0:
                    label = "positive even"
                else:
                    label = "positive odd"
    
            labels.append((n, label))
    
        return labels
    
    items = filter_and_label_numbers([-2, -1, 0, 3, 4])
    for number, label in items:
        print(number, "->", label)
    
    # Output
    -2 -> negative
    -1 -> negative
    0 -> zero
    3 -> positive odd
    4 -> positive even
    

    Explanation:

    This structure visually mirrors the program’s logic.


    Common IndentationError Issues and How to Fix Them

    Python raises IndentationError when your blocks don’t line up correctly.

    1. Missing Indentation After a Colon

    x = 10
    
    if x > 5:
    print("x is big")  # Incorrect
    

    This will raise:

    IndentationError: expected an indented block
    

    Fix: Indent the block after the if:

    x = 10
    
    if x > 5:
        print("x is big")  # Correct
    

    2. Inconsistent Indentation Within the Same Block

    Mixing different indentation widths (or tabs and spaces) causes errors:

    value = 3
    
    if value > 0:
        print("positive")
          print("still positive")  # Extra spaces cause IndentationError
    
    # Typical error
    IndentationError: unexpected indent
    

    Fix: Use the same number of spaces for all lines in the block:

    value = 3
    
    if value > 0:
        print("positive")
        print("still positive")
    

    3. Misaligned elif / else

    n = 5
    
    if n > 10:
        print("big")
        else:  # Incorrect alignment
            print("not big")
    
    # Typical error
    IndentationError: unexpected indent
    

    Fix: Align else (and elif) with the corresponding if:

    n = 5
    
    if n > 10:
        print("big")
    else:
        print("not big")
    

    Using Indentation to Improve Readability

    Beyond avoiding errors, indentation should make your logic easy to see at a glance:

    Example of refactoring for readability using indentation and functions:

    def is_eligible_for_discount(age, is_student):
        if age < 0:
            return False
    
        if age < 18:
            return True
    
        if is_student:
            return True
    
        return False
    
    print(is_eligible_for_discount(16, False))
    print(is_eligible_for_discount(25, True))
    print(is_eligible_for_discount(30, False))
    
    # Output
    True
    True
    False
    

    Explanation:


    Summary / Key Takeaways

    In this lesson on Indentation and Code Blocks, you have learned:

    Python Programming – Complete Course with Practical Examples — Python Syntax, Indentation, and Core Concepts

    Getting Started with Python and the Course

    Python Syntax, Indentation, and Core Concepts

    Variables, Data Types, and Type Conversion

    Operators, Expressions, and Control Flow

    Loops and Iteration Patterns

    Working with Strings in Depth

    Collections: Lists, Tuples, Sets, and Dictionaries

    Functions and Program Structuring

    Functional Programming Concepts in Python

    Modules, Packages, and Code Organization

    Error Handling and Debugging Fundamentals

    File Handling and Data Formats

    Object-Oriented Programming in Python

    Advanced Python: Iterators, Generators, and Decorators

    Python Standard Library for Real-World Tasks

    Python for Automation and Scripting

    Testing and Quality Assurance in Python

    Tutorial Not Found