Keyboard habits for learning Python

Python relies on colons, underscores, indentation and brackets. Learn simple keyboard habits that make these symbols quick to type, with short practice drills.

Coding Published: 6 min read

Most people who start learning Python get stuck on the keyboard before they get stuck on the logic. You write a line that looks right, run it, and get an error. Often the cause is a missing colon, a wrong underscore, or an extra space at the start of a line. This article covers four things Python asks of your fingers every day: colons, underscores, indentation and bracket pairs. For each one you will see where the key is, how to type it comfortably, and a short drill you can try right away. The examples assume a standard US keyboard.

Colons mark the start of a block

In Python, a line that begins with if, for, while or def opens a block, and it ends with a colon. The code under it must be indented. Leave out the colon and Python refuses to run the program. Error messages sometimes point to the line after the real mistake, so a missing colon can take a while to find.

def greet(name):
    print("Hello,", name)

for number in range(3):
    print(number)

On a US keyboard the colon shares a key with the semicolon. You hold Shift and press that key, which sits on the right side of the home row. Your right pinky usually reaches it, and your left pinky holds Shift. The motion feels awkward at first because the two hands do different jobs. Practise it slowly until your hands know it without you looking. Typing the colon right after a word, with no space before it, is a good way to build the habit.

For a drill, write the program above, then delete the colon at the end of the def line and run it again. Read the error carefully and find the line it points to. Put the colon back and run the program once more. Doing this once teaches the rule better than reading about it three times. If an error message seems confusing, read the line it points to first, then the line just above it, because the missing colon is often on that earlier line.

Underscores replace spaces in names

Python names cannot contain spaces. first score is not valid, so programmers join words with underscores instead. This style is called snake_case, and it is the normal way to name variables and functions in Python. Once you start reading other people’s code, you will see it everywhere. Many Python tutorials and style guides use it, so getting comfortable with it early saves confusion later.

first_score = 10
second_score = 25
total_score = first_score + second_score
print("Total:", total_score)

On a US keyboard the underscore is Shift plus the hyphen key. The hyphen sits in the number row, just to the right of 0. Use your right pinky for it and Shift with your left pinky. After you type an underscore, bring your hands back to the home row so the rest of your typing stays steady. Small returns like this add up over a long session.

Keep variable names to plain English letters, digits and underscores. Python 3 accepts accented letters in names, but code that uses them can cause trouble when you share it with other people or run it on other systems. A simple rule keeps your code portable.

For a drill, create three variables: apples, pears and fruit_total. Give the first two a number, set the third to their sum, and print it. As you type each underscore, say it out loud. It sounds a little silly, but it trains your attention on the symbol instead of letting your hands run on autopilot.

Indentation: four spaces, never a mix

Python uses indentation instead of curly braces to group code. The spaces at the start of a line are therefore part of the program. The official style guide, PEP 8, recommends four spaces for each level. Most code editors turn the Tab key into four spaces automatically, but check that setting yourself. Mixing tabs and spaces in the same file can cause a TabError, and that error is confusing when you do not expect it.

On the keyboard, Tab sits on the left side, in the area your left pinky covers. Spaces are typed with your thumbs. Instead of tapping the space bar four times for every level, let the editor insert the indent for you. Your fingers stay in one place and the code stays tidy.

def show_section(title):
    print("---", title, "---")
    for i in range(2):
        print("line", i + 1)

show_section("Indentation")

Here the function body is indented four spaces, and the lines inside the loop are indented eight. Try changing the indent of one line by a single space and run the program. You will usually see an IndentationError. That error is a useful hint: in Python, the structure of your code is written in its spacing. After fixing it, run the program twice in a row to confirm the output is the same both times.

Brackets and quotes come in pairs

Python uses round brackets for function calls, square brackets for lists and indexing, and curly braces for dictionaries. Text is wrapped in quotes. A single line often holds several pairs, so bracket typing is a large part of everyday Python work.

On a US keyboard, round brackets are Shift with the 9 and 0 keys. Square brackets are plain keys to the right of the P key, and curly braces are Shift with those same keys. Double quotes are Shift with the apostrophe key, which sits to the right of the semicolon. Each of these symbols lives in a different place, so practising them together is the fastest way to build the habit. Once the shapes feel familiar, try naming each symbol out loud as you type it, such as open round bracket or close square bracket. Saying the name slows your hands just enough to make the sequence stick.

student = {"name": "Elif", "grade": 7}
scores = [90, 85, 77]
print(student["name"], len(scores))

This snippet contains curly braces, square brackets, round brackets and quotes. Try typing it from memory, then compare your version with the original. Many editors close a bracket for you as soon as you open one. That helps, but you should still be able to type the closing symbol yourself. Otherwise you will spend time deleting extra brackets you never meant to add.

A daily practice routine

Ten focused minutes a day beats one long session each week. Pick one habit for each day: colons on Monday, underscores on Tuesday, indentation on Wednesday and brackets on Thursday. Write three short lines, then run a small program. When you hit an error, try to find the cause yourself before asking for help. Working out the cause is what makes the lesson last.

For general typing speed, use the typing test on this site. For symbol practice tied to real code, go back to the Python lessons. Speed comes after accuracy, so slow down when a symbol still feels unfamiliar. Your fingers will catch up with your understanding within a few weeks of steady practice. Keep a short note of which symbol gave you trouble each day, and return to it the next morning before you start new material.

Next step

When these drills feel comfortable, move on to the Python course, where you practise code and symbols together. You can track your general speed alongside it with the typing test. If you want more examples of the symbols that appear most often in programs, read our article on the most used symbols in code.

Try what you learned

The typing test takes one minute and needs no sign-up. For steady practice, look at the 16-week courses.

Read this post in Turkish: Python öğrenirken klavye alışkanlıkları

Getting started with Small Basic

Learn the core ideas of Small Basic: printing text, loops, conditions, subroutines and arrays. Each one comes with a short program you can type yourself.

Coding 6 min read

The most used symbols in code

Get to know the parentheses, brackets, equals signs and quotes you'll meet most in code, what each does, and how to type them on a US keyboard.

Coding 5 min read