← Back to Resources

Python Indentation Typing Practice: Three Short Drills

Practice Python's colons, four-space indents, and dedents with three small code drills, then apply them in a Python typing test.

By TypeNCode

Original practice examples checked against the Python language reference and PEP 8. Cover illustration was AI-generated.

Glowing space bar beneath three progressively indented lines in a code editor

Python indentation is part of the language's structure: the Python language reference explains how leading whitespace marks the start and end of blocks. For practice, follow PEP 8's four-space convention unless the project you work in uses a different established style.

These drills focus on typing the newline, indent, and dedent at the right moment. They are small enough to repeat in your editor without racing a timer. Use spaces consistently; Python can reject an inconsistent mix of tabs and spaces.

Drill 1: Colon, then one level deeper

def greet(name):
    if name:
        return "Hello, " + name
    return "Hello"

Type the colon at the end of def and if, press Enter, then enter one four-space indent for each new block. On the final return, go back to the function's indentation level. Check the start of every line before checking the words.

Drill 2: Repeat the same indent

for item in items:
    if item is None:
        continue
    print(item)

The last line remains inside the loop but outside the if. If your editor inserts indentation automatically, verify the resulting whitespace rather than adding another four spaces blindly. Practice once with automatic indentation and once in a plain text editor to notice the difference.

Drill 3: Close two levels

def count_long(words):
    total = 0
    for word in words:
        if len(word) > 4:
            total += 1
    return total

The final line returns to the function block. Repeat the example slowly until you can spot each change in indentation before typing it. Then change 4 to another threshold and type the edited version from scratch.

Apply it on TypeNCode

Open the code typing test, choose Python, and use a timed run to practice longer code samples. The test uses its own samples, so it will not reproduce the three drills above exactly. If punctuation is still the sticking point, work through the code symbol typing guide first. For a repeatable practice schedule, use the daily typing routine.

Ready to apply what you learned?

Start practicing with our interactive typing games designed specifically for developers.