CodeAgeBy Sitezack
Chapter progress0/3

Lesson 2 of 3

Parameters and default values

A parameter can have a default value: def gross(net, rate=0.19). The caller can then omit it or override it.

This is the idiomatic mechanism for options: gross(40) applies the standard rate, gross(100, 0.07) the reduced one.

def power(base, exponent=2):
    return base ** exponent

print(power(5))      # 25
print(power(5, 3))   # 125

🎯 Exercise

Define gross(net, rate=0.19) returning the gross price round(net * (1 + rate), 2), then print gross(40).

PRO lesson

This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.

Discover PRO