avatarMario Rodriguez

Summary

The provided web content offers an overview of defining and using functions in Python, including the use of default parameters, and the * and ** operators for handling variable numbers of arguments.

Abstract

The web content titled "Functions in Python" explains the concept of functions as a means to create modular and reusable code blocks in Python. It covers the syntax for defining functions, including the use of parameters and return values, and provides examples of functions with and without default parameter values. The article also delves into the use of the * operator to pass a list or tuple of arguments and the ** operator to pass keyword arguments from a dictionary, demonstrating how these operators can be used together to accept a flexible number of arguments in a function. The content emphasizes the importance of functions in organizing code for better readability and maintainability, and it concludes with a brief mention of a Python course for further learning and a referral link to join Medium.

Opinions

  • Functions are presented as essential for organizing and reusing code, making it more modular, understandable, and maintainable.
  • The use of default values for function parameters is highlighted as a feature that adds flexibility to function calls.
  • The * and ** operators are showcased as powerful tools for writing functions that can handle a variable number of arguments, enhancing the versatility of Python functions.
  • The article suggests that understanding these concepts is crucial for effective functional programming in Python.
  • The inclusion of a course index and a referral link to join Medium indicates an encouragement for readers to continue learning about Python and to support the platform and its writers.

Functions in Python

Write functions to create a modular code

Photo by David Clode on Unsplash

In Python, a function is a block of code that performs a specific task and may return a result. Functions help you organize and reuse your code, making it more modular and easier to understand and maintain.

Defining and calling functions

Here is the general syntax for defining a function in Python:

def function_name(parameters): 
    # code to be executed return result

Here is an example of a simple function that takes a single parameter and returns the square of that number:

def square(x): 
    return x * x

To call a function in Python, you simply need to use the function’s name followed by a set of parentheses, and any required parameters inside the parentheses. For example, you can call the square() function like this:

result = square(3) 
print(result) # Output: 9

Functions can also have default values for their parameters, which means that you can call the function with fewer arguments than there are parameters. If you do not provide a value for a parameter with a default value, the default value will be used.

Here is an example of a function with default values for its parameters:

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

You can call this function with one or two arguments. If you provide two arguments, the first will be raised to the power of the second. If you only provide one argument, it will be squared (raised to the power of 2). Here are some examples of calling the power() function:

result = power(3) 
print(result) # Output: 9
result = power(3, 3) 
print(result) # Output: 27

* and ** Operators

In Python, the * operator can be used to call a function with a variable number of arguments. When used in a function call, the * operator is used to pass a tuple or a list of arguments as separate positional arguments to a function.

For example, you have a function that takes 3 arguments, and you have a tuple or list that contains these 3 arguments. You can use the * operator to unpack the tuple/list and pass its elements as separate positional arguments to the function. Here's an example:

def my_function(a, b, c):
    print(a, b, c)

args = (1, 2, 3)
my_function(*args)  # prints 1 2 3

This way the tuple args was unpacked as 3 separate arguments and passed to the function.

It’s also used to pass keyword arguments to function using ** operator to unpack the key-value pairs from a dictionary.

def my_function(a, b, c):
    print(a, b, c)

kwargs = {"a": 1, "b": 2, "c": 3}
my_function(**kwargs)  # prints 1 2 3

In the above example the dictionary kwargs was unpacked to separate keyword arguments and passed to the function.

It’s often used in a combination of both * and ** when calling a function, in this way:

def my_function(a, b, c, *args, **kwargs):
    print(a, b, c, args, kwargs)

pos_args = (1, 2, 3)
key_args = {"d": 4, "e": 5}
my_function(*pos_args, **key_args) # prints 1 2 3 () {'d': 4, 'e': 5}

This allows a function to accept a variable number of arguments and keyword arguments, it’s also used in functional programming libraries like functools.partial and functools.wrap.

It’s important to note that when using * and ** operators to call a function, the arguments passed to them must be iterable (list, tuple) for * operator and a dictionary for ** operator, also the order of passing these arguments matters, *args needs to be passed before **kwargs.

Course index:

Have you spent your learning budget for this month, you can join Medium here:

Python Programming
Coding
Learning To Code
Course
Python
Recommended from ReadMedium