Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Defaults, Scope & Docstrings

โญ beginnerโณ 12 min read๐Ÿ“ Lesson 25 of 169

Optional parameters, keyword calls, local scope, and documentation.

Default arguments

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

power(5)        # 25 โ€” exponent defaults to 2
power(5, 3)     # 125

Defaults make optional behavior explicit. Never use a mutable default (a list or dict) โ€” it is shared across calls, a famous Python trap you will meet again in Intermediate.

Keyword arguments

Call with name=value โ€” order stops mattering and calls become readable:

def make_coffee(size, milk, sugar):
    return f"{size}, milk={milk}, sugar={sugar}"

make_coffee("L", milk=False, sugar=True)

Scope: local by default

Names created inside a function exist only inside it:

def calc():
    result = 42     # local
calc()
print(result)       # NameError โ€” result is gone

Parameters are local too. Functions get information in through parameters and out through return values โ€” not through globals. That discipline is what keeps larger programs sane.

Docstrings

A triple-quoted first line documents the function; help(name) shows it:

def celsius_to_f(c):
    '''Convert a Celsius temperature to Fahrenheit.'''
    return c * 9 / 5 + 32