Week 10: Functions

Python code typing course. 19 wpm, 93% accuracy.

A function is a named block of code that you define once and call whenever you need it. In Python you start a function with the word def, then write its name followed by inputs inside parentheses. This week you use the parentheses you learned earlier in a new context, and you add commas between the inputs. The body of the function is indented, just as in a loop.

What you will learn

On the US English layout, the three symbols are typed like this:

  • ( is the 9 key held with Shift. Hold Shift with the opposite pinky and press 9 with the right ring finger.
  • ) is the 0 key held with Shift. Hold Shift with the opposite pinky and press 0 with the right pinky.
  • , is the comma key, pressed with the right middle finger.

Here is a short function that takes two inputs:

def add(a, b):
    return a + b

The first line names the function add and lists two inputs, a and b. The comma separates them. The second line returns the sum and is indented by four spaces.

Technique tips

  • As soon as you type the opening parenthesis, think of the closing one. Keep your eyes on the end of the line.
  • Put a space after each comma. The space makes the inputs easier to read and to check.
  • Press Shift with the opposite pinky before the parentheses, and release both after the symbol appears.
  • The colon at the end of the def line is required. The body starts on the line below it.
  • Say the function name once in your head before typing it. Knowing the name reduces spelling errors in later calls.

Lessons

  1. Lesson 1 (10 to 15 minutes): Learn. Watch the keyboard demo for the parentheses and the comma. Then type def add(a, b): slowly.
  2. Lesson 2 (10 to 15 minutes): Practice. Repeat return a + b with its indentation. Check that the parentheses and commas match in every line.
  3. Lesson 3 (10 to 15 minutes): Review. Take the 2-minute test built from this week’s function lines. Note any line where a bracket or comma went missing and repeat it next time.

This week’s goal

The goal is 19 wpm with 93% accuracy. The first star comes when you finish the lessons. The second star comes at 93% accuracy, and the third when you also reach 19 wpm. A missing parenthesis or comma spoils the whole definition, so accuracy comes first.

Symbols this week

The colored keys are the ones you learn this week. The color shows which finger presses the key.

` ~ 1 ! 2 @ 3 # 4 $ 5 % 6 ^ 7 & 8 * 9 ( 0 ) - _ = + Backspace Tab Q W E R T Y U I O P [ { ] } \ | Caps A S D F G H J K L ; : ' " Enter Shift Z X C V B N M , < . > / ? Shift Ctrl Win Alt Alt Gr Win Menu Ctrl

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

    New symbols: ( ) ,

    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