← All lessons

Default values

Sign in · Sign up

Lesson 6.3

A parameter can have a default value: if no argument is passed, that value is used.

def greet(name="friend"):
    print(f"Hello, {name}!")

greet()        # Hello, friend!
greet("Mia")   # Hello, Mia!

Parameters with a default value go at the end of the list: def f(a, step=1) works, while def f(step=1, a) does not.

The check sets the number limit and the step step. Write a function count_to(n, step=1) that prints the numbers from 1 to n inclusive with that step: count_to(5) — each one in a row, count_to(10, 2) — every other one.

Print --- between the two calls: count_to(limit), then ---, then count_to(10, step).

Hint

A default value is written right in the brackets: step=1.

range(1, n + 1, step) goes from 1 to n with the step step. Two calls: the first without the second argument, print("---") between them, the second with step from the task.