Contents

Python Interactive Guide - Step 3 Functions (1) - Function Basics (2)

Series - Python Interactive Guide
Info
  • 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.”

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
Function Definition (With Parameters)
def function_name(parameter1, parameter2, ...): function process return return_value
Function Call (With Parameters)
function_name(argument1, argument2, ...)
📚Exercise

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

When calling a function, you need to provide arguments for all parameters:

If the required arguments are not provided, a TypeError will occur:

If extra arguments are provided, a TypeError will also occur:

Arguments can also be specified in the form <parameter>=<argument> when calling a function:

Function Call (Keyword Arguments)
function_name(parameter=argument, ...)
  • 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:

Function Call (Positional and Keyword Arguments)
function_name(argument1, argument2, parameter3=argument3, ...)

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.

Function Definition (Default Arguments)
def function_name(parameter=default_argument, ...): function process return return_value

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.

Caution when using mutable objects as default arguments!

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:

📚Exercise

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

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).

docstring (Documentation String)
def function_name(): """ Function description (docstring) is written here """ function process return return_value
  • 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)
Docstring Styles

There are several styles for writing docstrings in Python. Here are three representative styles:

Simple, readable, and recommended by Google.

Commonly used in scientific computing libraries.

Works well with documentation generation tools like Sphinx.

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.

For more detailed explanations about docstrings, refer to:

📚Exercise

Create a docstring for the function from the previous exercise.
Use any of the three styles mentioned above.

Example Solution

Related Content