Home >Backend Development >Python Tutorial >How to use functions in Python?
Python is a general-purpose, high-level programming language. It is a very flexible and powerful language because it has many built-in functions, as well as the ability to write your own functions. Writing functions allows you to reuse code within your code, making Python a very practical language. In this article, we will discuss how to use functions in Python.
Defining a function is the first step in using it. In Python, functions can be defined using the keyword def. Function names should consist of one or more letters and underscores, and should be defined on the first line of the function. After the function name, the parameters of the function should be defined in parentheses. In Python, parameters are optional, which means if the function does not require parameters, the parentheses can be omitted. After the first line of a function, a colon (:) should be used to indicate the beginning of the function, followed by the function's code block.
For example: Define a simple function in Python that adds two numbers and returns their sum:
def add_numbers(num1, num2): sum = num1 + num2 return sum
The function name is add_numbers, and it has two parameters num1 and num2 . In the body of the function, num1 and num2 are added and the sum is stored in the variable sum. Finally, the sum is returned using the return keyword in Python.
Once a function is defined, it can be used by calling the function. To call a function, simply pass it the function name and the required arguments.
For example: Call the function add_numbers in Python, passing parameters 2 and 3:
sum = add_numbers(2, 3) print(sum)
This will display the output 5 on the screen.
In Python, function parameters can be passed as positional parameters or keyword parameters. Positional parameters are parameters passed in the order in which the function is defined. For example, in the function add_numbers above, the parameter num1 is defined before the parameter num2, so when the function is called, the value of num1 should be passed as the first parameter.
For example: Call the function add_numbers in Python, passing parameters 5 and 10:
sum = add_numbers(5, 10) print(sum)
This will also output 15 on the screen.
Keyword parameters, on the other hand, are parameters passed by passing the parameter name and value as key-value pairs to the function.
For example: Call the function add_numbers in Python, passing the values of the keyword parameters num1 and num2:
sum = add_numbers(num1=2, num2=3) print(sum)
This will also output 5 on the screen.
In Python, you can also specify default parameters when defining a function. Default parameters are a set of parameter values specified during function definition. The values of these default parameters will be used when no parameters are passed explicitly.
For example: Define a function greet with default parameters in Python:
def greet(name, greeting="Hello"): print(greeting, name)
In this example, the greet function has two parameters, a required parameter name and a default parameter greeting . If no greeting parameter is passed, its default value of "Hello" will be used.
For example: Call the function greet in Python, passing only the required parameters:
greet("John")
This will display the output "Hello John" on the screen.
For example: Call the function greet in Python and pass the value of the greeting parameter:
greet("John", greeting="Hi")
This will display the output "Hi John" on the screen.
In Python, functions can also define variable parameters. Variadics are an indeterminate number of parameters. In functions, variable parameters are passed in the form of tuples.
For example: Define a function sum_numbers with variable parameters in Python:
def sum_numbers(*numbers): sum = 0 for num in numbers: sum += num return sum
In this example, the sum_numbers function uses an asterisk (*) to specify any number of positional parameters. . In a function, numbers is a tuple containing all the parameters passed in. Within the function, you can use a for loop to iterate through each number in the tuple and add them together.
Example: Calling the function sum_numbers in Python, passing any number of arguments:
print(sum_numbers(1, 2, 3, 4, 5, 6))
This will display the output 21 on the screen.
In Python, you can also use an asterisk () to unpack parameters. Unpacking arguments uses asterisks () to pass tuples or lists to the function as separate arguments rather than passing them as one object.
For example: Define a function greet with three parameters in Python:
def greet(name, greeting, punct): print(greeting, name, punct)
Now, we have a tuple containing these three parameters:
args = ("John", "Hello", "!")
We The argument args can be unpacked and passed to the function greet:
greet(*args)
This will display the output "Hello John!" on the screen.
In Python, you can also use the lambda function. A lambda function is an anonymous function. It can contain just one expression and return the result of the expression.
For example: Using the lambda function in Python, create a function that multiplies a number by two:
double = lambda x: x * 2 print(double(5))
This will display the output 10 on the screen.
In this article, we discussed how to use functions in Python, including defining functions, calling functions, positional and keyword parameters, default parameters, Variable parameters, unpacked parameters and lambda functions. Functions are a very important and useful tool to make your code more modular and easily reusable. Proficient use of functions can make Python programming more efficient and convenient.
The above is the detailed content of How to use functions in Python?. For more information, please follow other related articles on the PHP Chinese website!