Python Interactive Guide - Step 3 Functions (1) - Function Basics (2)
- This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
- The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
- You can run and experiment with every code example.
Feel free to try things out - reloading the page will reset everything.
This is a continuation of “Step 3 Functions.”
3.1. Function Basics
3.1.4. Parameters and Arguments
Parameters and arguments are used to pass values to functions.
- Parameter: A variable specified when defining a function
- Argument: The actual value passed when calling a function
Let’s modify the function from the previous exercise to receive a list as a parameter and return the minimum and maximum values.
Example Solution
Specifying Arguments
When calling a function, you need to provide arguments for all parameters:
When Arguments Are Missing
If the required arguments are not provided, a TypeError
will occur:
When There Are Too Many Arguments
If extra arguments are provided, a TypeError
will also occur:
Positional Arguments and Keyword Arguments
Arguments can also be specified in the form <parameter>=<argument>
when calling a function:
- Arguments specified this way are called keyword arguments
- Conversely, regular arguments are called positional arguments
With keyword arguments, you can change the order of specification:
Positional arguments and keyword arguments can be mixed.
However, keyword arguments must come after positional arguments:
Default Arguments
You can set default arguments for parameters by specifying them as <parameter>=<argument>
when defining the function.
Parameters with default arguments can have their arguments omitted when calling the function.
You cannot have parameters without default arguments after parameters with default arguments.
Therefore, when setting default arguments for parameters, you need to either set default arguments for all subsequent parameters or change the parameter order.
Try to predict the output of the following program before running it:
The result is not Added 1: [1]
, Added 2: [2]
, but Added 1: [1]
, Added 2: [1, 2]
.
This happens because default arguments are created when the function is defined, and the same object is used thereafter:
To prevent such unintended behavior and to use mutable values as default values, set the default argument to None
and add logic within the function to convert it when it’s None
:
Create a function that takes two sets as input and returns the size of their union.
Make the second set optional, and if omitted, return the size of the first set.
Also, test if it works correctly by actually using it.
Example Solution
3.1.5. docstring (Documentation String)
You can describe the function by including a string enclosed in triple quotes ("""
) immediately after the function definition.
This string is called a docstring (documentation string).
- Writing docstrings has the following benefits:
- Explicitly shows how to use the function
- Descriptions are visible in mouse-over displays in editors like VSCode
- Function documentation can be automatically generated using documentation tools (such as Sphinx)
There are several styles for writing docstrings in Python. Here are three representative styles:
1. Google Style
Simple, readable, and recommended by Google.
2. NumPy Style
Commonly used in scientific computing libraries.
3. reStructuredText (Sphinx) Style
Works well with documentation generation tools like Sphinx.
Which Style Should I Use?
Any style is fine, but it’s important to maintain consistency within a team or project.
Starting with the Google style is recommended for beginners.
To Learn More
For more detailed explanations about docstrings, refer to:
Create a docstring for the function from the previous exercise.
Use any of the three styles mentioned above.