Home > Backend Development > Python Tutorial > Day Everything You Need to Know About Functions in Python

Day Everything You Need to Know About Functions in Python

DDD
Release: 2025-01-08 20:42:44
Original
482 people have browsed it

Detailed explanation of Python functions: definition, call and parameter types

Day Everything You Need to Know About Functions in Python

Function definition and function

A function is a reusable block of code that is executed only when needed. In Python, functions are defined using the def keyword.

For example, the following code defines a sum function:

<code class="language-python">def sum(a, b):
  print(a + b)</code>
Copy after login

This function calculates the sum of two integers a and b. When you need to find the sum of two numbers, you can directly call the sum(a, b) function.

Function call

Calling a function means executing the code inside the function through the function name followed by parentheses. For example:

<code class="language-python">def sum(a, b):
  print(a + b)

sum(1, 3)</code>
Copy after login

Here:

  • a and b are parameters in the function definition.
  • 1 and 3 are the parameter values ​​(actual parameters) passed to the function.
  • sum is the function name.

Parameter type

Python functions support four parameter types:

  1. Required parameters: Parameters that must be provided when the function is called. If not provided, Python will throw a TypeError exception.
<code class="language-python">def sum(a, b):
  print(a + b)

sum(1, 3)  # 正确
sum()      # TypeError</code>
Copy after login

a and b are required parameters as they have no default value.

  1. Keyword arguments: Pass parameters by explicitly specifying the parameter name.
<code class="language-python">def new_print(a, b):
   print(f"{a} is a friend of {b}")

new_print(b="Alice", a="Bob")</code>
Copy after login

Output: Bob is a friend of Alice

Keyword parameters correctly assign values ​​to the corresponding parameters even if the parameters are in the wrong order.

  1. Default parameter: Set the default value for the parameter, which can be omitted when calling the function.
<code class="language-python">def sum(a=0, b=0):
   print(a + b)

sum()      # 输出:0
sum(1, 3)  # 输出:4</code>
Copy after login

Even if no values ​​for a and b are provided, the function will execute normally because they have a default value of 0.

  1. Variable parameters: The function can accept any number of parameters.

    a. *args: used to receive any number of non-keyword parameters, accessed in the form of tuples inside the function.

<code class="language-python">def sum(*numbers):
   total = 0
   for i in numbers:
     total += i
   print(total)

sum(1, 2, 3, 4)  # 输出:10</code>
Copy after login

b. **kwargs: used to receive any number of keyword parameters, accessed in the form of a dictionary inside the function.

<code class="language-python">def print_kwargs(**kwargs):
  for key, value in kwargs.items():
     print(f"{key}={value}")

print_kwargs(name="Alice", age=30, city="New York")</code>
Copy after login

Four methods of digital exchange

The following four methods can exchange the values ​​​​of two numbers:

Method 1: Use temporary variables

<code class="language-python">P = 5
Q = 4
temp = P
P = Q
Q = temp</code>
Copy after login

Method 2: Tuple unpacking

<code class="language-python">P = 5
Q = 4
P, Q = Q, P</code>
Copy after login

Method 3: Use bitwise operators

<code class="language-python">P = 5
Q = 4
P = P ^ Q
Q = P ^ Q
P = P ^ Q</code>
Copy after login

Method 4: Use addition and subtraction

<code class="language-python">P = 5
Q = 4
P = P + Q
Q = P - Q
P = P - Q</code>
Copy after login

The above is the detailed content of Day Everything You Need to Know About Functions in Python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template