Programming fundamentalsLesson 3 of 3 in the course Functions

Python Function Arguments, Defaults, and PEP 8 Spacing

5 flashcards · 5 quiz questions · 5 match pairs · 3 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Functions can receive values in more than one way. Understanding positional arguments, keyword arguments, and default parameter values helps you call functions clearly and design them to work with optional information.

A function definition names its parameters; a function call supplies arguments. With positional arguments, Python assigns each value to a parameter according to its position in the call.

All flashcards

What is a positional argument?
An argument whose value is assigned to a parameter according to its position in the function call.
What is a keyword argument?
An argument that supplies a value by naming the parameter, for example, count=3. It can make a function call clearer and can be written in a different order from other keyword arguments.
What rule applies when positional and keyword arguments are mixed?
All positional arguments must come first, in the correct order, and keyword arguments must follow them.
What is a default parameter value?
A value specified in a function definition that Python uses when a caller does not provide an argument for that parameter. A caller can override the default.
Why avoid mutable defaults, and what is a common alternative?
A mutable default object, such as a list, is created once and may retain changes between calls. Use None as the default and create a new list inside the function when needed.

Quiz questions

  1. How does Python assign a positional argument to a parameter?Answer: By its position in the function call.
  2. For def greeting(msg, name, count), which call correctly mixes positional and keyword arguments?Answer: greeting("Hello", name="Maya", count=2)
  3. In a function definition, where should parameters with defaults appear?Answer: After parameters that do not have defaults.
  4. Why should a list generally not be used directly as a default parameter value?Answer: The default list is created once and can retain changes between function calls.
  5. According to PEP 8, how should equals signs be spaced in keyword arguments and default values?Answer: Use no spaces around = in keyword arguments or defaults.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons