Week 15: Small programs II

Python code typing course. 24 wpm, 94% accuracy.

Last week you walked through a list with a loop. This week you go one step further: you define your own function, then call it. The function takes a number, returns its square, and the program prints the result. No new symbols are introduced. The goal is to type the function definition, the return line and the call smoothly, one after another.

What you will learn

This week there are no new symbols. The symbols in the program were introduced in weeks 2, 4, 8 and 10, so you can revisit their key positions there. Here is a short version of the program:

def square(x):
    return x * x
print(square(4))

The first line is the function header and ends with a colon. The second line is indented and returns the value of x multiplied by itself. The third line calls the function with 4, and the output is 16.

Technique tips

  • As soon as you type the opening parenthesis in the header, think of the closing one. Type the x between them afterwards.
  • After the word return, leave a space before the expression. The space separates the keyword from the value it returns.
  • The multiplication sign is typed with Shift held by the opposite pinky, pressing 8 with the right middle finger.
  • Before you start an indented line, check that the line above it ends with a colon.
  • In the call line, think of the parentheses as one pair. Typing both in the same movement reduces mistakes.

Lessons

  1. Lesson 1 (10 to 15 minutes): Learn. Type the first two lines of the function slowly. Then explain in your own words what the header and the return line do.
  2. Lesson 2 (10 to 15 minutes): Practice. Type the whole program from start to finish three times. Check the indentation, the parentheses and the colon each time.
  3. Lesson 3 (10 to 15 minutes): Review. Take the 2-minute test built from a short function and call. Note the line where you made a mistake and type it again.

This week’s goal

The goal is 24 wpm with 94% accuracy. The first star comes when you finish the lessons. The second star comes at 94% accuracy, and the third when you also reach 24 wpm. A missing parenthesis or colon stops the program, so accuracy comes before speed.

This week’s lessons

Do the lessons in order. Each one ends with a test, and your best result is saved.

  1. Lesson 1: Learn

    Keywords

    12 minutes

    Start
  2. Lesson 2: Practice

    Practice

    12 minutes

    Start
  3. Lesson 3: Review and test

    Review and test

    13 minutes

    Start

All weeks