function
- If a piece of code is used many times during development, it needs to be packaged into a small module, which is called a function.
- Function to simplify the program structure, increase the reading line, code reuse,
- Functions are divided into declaring functions and calling functions.
def print_hello(a):Declare a function
print(Hello world.)# Function content
print_hello()# call function
Copy the code
Results of the above code execution:
Function parameters
Positional arguments
- How many parameters are there, and how many arguments must be passed
- The arguments correspond to the parameters one-one
def calcu(x, y):# x, y, called parameters
print("X is %d"%x)
print("Y is %d"%y)
z = x + y
print(z) calcu (1, 2)# 1, 2 arguments
Copy the code
Results of the above code execution:
Keyword parameter
- It is possible to pass parameters in a different order than the defined parameters
- Positional arguments must precede keyword arguments, and there is no order between keyword arguments
def calcu(x,y):# x, y, called parameters
print("X is %d"%x)
print("Y is %d"%y)
z = x + y
print(z)
calcu(1.2)# 1, 2 arguments
calcu(x=1,y=2)
calcu(y=2,x=1)
calcu(1,y=2)
# # calcu (x = 1, 2) is not correct
# # calcu (y = 2, 1) is not correct
# # calcu (x = 2, 1) is not correct
Copy the code
Results of the above code execution:
The default parameters
- When a function is called, if no arguments are passed, the default arguments are used. If a parameter is passed, the passed parameter is used.
- Default arguments must be placed after positional arguments. Bear in mind that
def introduce(age,name="Wang"):
print("My name is % S and I'm % D"%(name,age))
introduce(23)# if no name is passed, Lao Wang will be used by default
introduce(23."Xiao Ming")# pass name, the passed value will be used
The following defines the function as an error the default argument must be placed after the positional argument ~ remember! Def introduce (name = "Lao wang," age) : print (" my name is % s, I "age of % d % (name, age)) ' ' '
Copy the code
Results of the above code execution:
Variable parameter
- When you want a function to handle more than one argument, those arguments are called mutable arguments.
def introduce(age,name,*args):#*args stands for variable parameters, or variable length parameters
print("My name is % S and I'm % D"%(name,age))
print(args)The variable argument is a tuple
for arg in args:
print(arg)# walk through the mutable arguments
introduce(23."Xiao Ming"."Male"."Characters from elementary school textbooks.")
Copy the code
Results of the above code execution:
- Dictionary type arguments for mutable arguments
def introduce(age,name,*args,**kwargs):
Args stands for mutable arguments, or indeterminable arguments **kwargs stands for dictionary type mutable arguments. ' ' '
print("My name is % S and I'm % D"%(name,age))
print(args)The variable argument is a tuple
print(kwargs)Mutable arguments, is a dictionary
for arg in args:
print(arg)# walk through the mutable arguments
for k,v in kwargs.items():# walk through the dictionary
print(k)
print(v)
introduce(23."Xiao Ming"."Male"."Characters from elementary school textbooks.",address="Beijing",phone="123456")
Copy the code
Results of the above code execution:
- unpacking
def introduce(age,name,*args,**kwargs):
Args stands for mutable arguments, or indeterminable arguments **kwargs stands for dictionary type mutable arguments. ' ' '
print("My name is % S and I'm % D"%(name,age))
print(args)The variable argument is a tuple
print(kwargs)Mutable arguments, is a dictionary
for arg in args:
print(arg)# walk through the mutable arguments
for k,v in kwargs.items():# walk through the dictionary
print(k)
print(v)
# introduce (23, "Ming", "male", "primary school textbook" characters, address = "Beijing", phone = "123456")
desc = ("Male"."Characters from elementary school textbooks.")
other = {"address":"Beijing"."phone":"123456"}
introduce(23."Xiao Ming",*desc,**other)# This method is called unpacking
Copy the code
Results of the above code execution:
Combination of parameters
def introduce(name,age,*args,sex="Male",**kwargs):
print(name)
print(age)
print(sex)
print(args)
print(kwargs)
introduce("Xiao Ming".12."Textbook characters."."Female",phone="123")
# introduce (12, "Ming", "textbook" characters, phone = "123")
# Introduce (" xiao Ming ",12," people in textbook ") # Introduce (" Xiao Ming ",12," people in textbook ")
# introduce (" Ming ", 12)
Copy the code
Results of the above code execution: