← All lessons

def: your own command

Sign in · Sign up

Lesson 6.0

You can create your own command — a function:

def hello():
    print("Hello!")
    print("How are you?")

hello()

There are two parts here:

  • the definition — def with a name, brackets and a colon, and an indented body;
  • the call — the name with brackets: hello().

A definition on its own prints nothing: the code inside runs only when you call it, and you can call it as many times as you like.

Write a function hello() that prints Hello! and then How are you?, and call it exactly twice.

Hint

A function is def with a name, brackets and a colon.

The body is indented and holds the print. A call is the name with brackets, otherwise the function never runs.