Everything else is ready: study the parts, the key terms and the quiz below.
Arguments and Scope
Pass arguments every way Python allows, and know where names live.
In this lesson9 parts
- 01One rate is not enough
- 02Positional and keyword arguments
- 03Default values
- 04Any number of arguments
- 05Keyword-only, and reading a signature
- 06Local names
- 07Global and built-in names
- 08Changing what you were given
- 09Recap, your challenge, and next
Key terms
The words this lesson introduces, each in one line. The module’s glossary collects them all.
**kwargs- A parameter with two stars, which collects extra keyword arguments into a dictionary:
def order(**quantities). *args- A parameter with one star, which collects extra positional arguments into a tuple:
def total(*prices). - default value
- The value a parameter takes when a call does not pass that argument:
rate=0.08. - global variable
- A name bound at the top level, which any function can read. Rebinding it inside a function needs
global. - keyword argument
- An argument passed as
name=value, matched to the parameter with that name:rate=0.2. - local variable
- A name bound inside a function, which exists only during a call.
- positional argument
- An argument matched to a parameter by its position.
- scope
- The part of a program where a name can be used. Python looks a name up in the local frame, then the global namespace, then the built-ins.
- signature
- A function's name and its parameters, as the documentation lists them:
sorted(iterable, /, *, key=None, reverse=False).
Quiz 5 questions
Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.
Practice
Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.
The self-checking notebook for this lesson is Notebook 1 · Functions and Arguments.
Common mistakes
What you will see when it goes wrong, why it happens, and the fix.
Where it’s used
Where this lesson’s ideas turn up in real work.