From python basic functions to lambda

Estimated read time 4 min read

write in front

In this tutorial, we will review Python functions and their practical applications. We’ll cover the basic steps needed to start a Python function project, and dive into some advanced features like defining default parameters and using the partial library. We will use Python3 for this tutorial.

1. Simple function

Let’s start by creating a simple Python function that takes two arguments and returns their sum. Create a new file named in the project directory basic_func.pyand add the following code:


def add_numbers(a, b):
    return a + b

Call functions

Now, let’s create a main.pynew file called and use basic_func.pythe function we created in. Add the following code to

from basic_func import add_numbers

result = add_numbers(3, 9)
print(result)  # 输出:12

2.Default parameters in functions

Directly define default parameters

Python functions allow us to define default values ​​for function parameters. Let’s modify our add_numbersfunction to provide a default value for the second parameter. The updated basic_func.pyfiles are as follows:


def add_numbers(a, b=0):
    return a + b

Calling the updated function
Now, when main.pycalling add_numbersthe function, sometimes a second parameter is provided, sometimes not:

from basic_func import add_numbers

result1 = add_numbers(3)
result2 = add_numbers(3, 9)

print(result1, result2)  # 输出:3 11

As for the partial library, it is a module in the Python standard library, located inside the functools module. The partial module provides a partial() function for partial application functions. Partial application refers to fixing part of the parameters of a function and generating a new function that is called with some fewer parameters than the original function.

partial library

The main scenario for using partiala library is when you need to convert a function with many parameters into a function with fewer parameters so that it can be used more conveniently in a specific context. This enables default parameter functionality and needs to be defined in advance. Here’s a simple example:

from functools import partial

# 原始函数
# base是基数,exponent是次方 
def power(base, exponent):
    return base ** exponent

# 创建一个新函数,固定base为2
power_of_2 = partial(power, 2)

# 调用新函数,只需提供exponent参数
result = power_of_2(3)  # 相当于调用power(2, 3)
print(result)  # 输出:8

In the above example, we use partialfunction to create a new function power_of_2, which is powera specific version of the function where basethe parameters are fixed 2. This way we exponentcan call the new function simply by providing the parameters without having to pass basethem every time.

3. Recursive functions

A recursive function is a function that calls itself. Let’s create a recursive function to calculate the factorial of a number. Add the following code to


def factorial(n):
    if n == 0:
        return 1
        return n * factorial(n-1)

result = factorial(5)
print(result)  # 输出:120

4.Lambda function

Python supports creating anonymous functions (i.e. functions that are not bound to a name) using the lambda keyword. Let’s create a lambda function that doubles the input value. Updates main.pyare as follows:


double = lambda x: x * 2

result = double(5)
print(result)  # 输出:10

Best Practices

  1. Use meaningful function and variable names to improve code readability.
  2. Use meaningful function and variable names to improve code readability.
  3. Use docstrings to describe a function’s purpose, parameters, and return values.
  4. Keep functions short and focused on a single task.

write at the end

In this tutorial, we reviewed the basics of Python functions, including creating simple functions, using default parameters, and explored advanced features such as the partial library, lambda functions, and recursive functions. We also set up a project environment and organized the code into separate files to improve maintainability.

You May Also Like

More From Author

+ There are no comments

Add yours