What does def in python do

Python uses def to start the function definition, followed by the function name. Inside the brackets are the parameters of the function, and inside are the specific function implementation code of the function. If you want the function to have a return value, use return in the logic code in expressions.

Basic use

def function_name(parameters):
 expressions

Instance

def function():print('This is a function')
a =1+2print(a)

Above we defined a function named function. The function does not accept parameters, so the brackets are empty, and the function code of the function follows. If you execute the script, you find that no output is output, because we only defined the function, but did not execute the function. At this time, we enter the function call function() in the Python command prompt. Note that the parentheses for calling the function cannot be omitted. Then the function code inside the function will be executed and the output result:

This is a function3

If we want to call the script in the script, we only need to add the function call statement at the end of the script

1 function()

Then when the script is executed, the function will be executed.

DEF function parameters

When we use the calling function, we want to specify the value of some variables to be used in the function, then these variables are the parameters of the function. When the function is called, just pass in.

Basic use

def function_name(parameters):
 expressions

The position of parameters is the parameter of the function, which can be passed in when calling.

Instance

def func(a, b):
c = a+b
print('the c is ', c)

The parameter of a function defined here is two values, and the function of the function is to add the two parameters together. After running the script, call the function func in the Python prompt. If the parameter func() is not specified, an error will occur; output func(1, 2), pass a=1, b=2 to the function, and output the c is 3. Therefore, when calling a function, the number and position of parameters must be defined in accordance with the function. If we forget the position of the parameters of the function and only know the names of the parameters, we can specify the specific parameters func(a=1, b=2) during the function call, in this case, the position of the parameters will not be affected , So func(b=2,a=1) is the same effect.

DEF function default parameters

When we define a function, sometimes some parameters are the same in most cases, but in order to improve the applicability of the function, some optional parameters are provided. In order to facilitate the function call, we can set these parameters as default parameters. Then the parameter does not need to be explicitly given during the function call.

Basic use

def function_name(para_1,...,para_n=defau_n,..., para_m=defau_m):
expressions

Function declarations only need to be given with the = sign where default parameters are needed, but note that all default parameters cannot appear before non-default parameters.

Instance

def sale_car(price, color='red', brand='carmy', is_second_hand=True):print('price', price,'color', color,'brand', brand,'is_second_hand', is_second_hand,)

A sale_car function is defined here. The parameter is the attribute of the car. In addition to price, colors, brand and is_second_hand all have default values. If we call the function sale_car(1000), it will be the same as sale_car(1000,'red ','carmy', True) is the same effect. Of course, you can also pass in specific parameters during the function call to modify the default parameters. By default parameters can reduce the complexity of our function call.

Basic knowledge points supplement:

**def **

Necessary function of custom function: def

Instructions:

def function name(Parameter 1, parameter 2, parameter...):
 Function body (statement block)
 return[return value]

Precautions

So far, this article on what def does in python is introduced. For more related what is def in python, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!

Recommended Posts

What does def in python do
What does np do in python
What does rc1 mean in python
What does the tab key in python mean
What can python collections do
What is introspection in python
What is object-oriented in python
​What are the numbers in Python?
What is an anonymous function in Python
What are web development frameworks in python
What is a sequence table in Python
What kind of work can python do
What is the function of adb in python
03. Operators in Python entry
What conditions are needed for infinite loop in Python
Join function in Python
12. Network Programming in Python3
print statement in python
What software do I need to install to learn Python?
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
What are the ways to open files in python
Do you still know how to draw cakes in python? ? ?
Concurrent requests in Python (part 2)
What are python class attributes
Subscripts of tuples in Python
How does Python output integers
How does python output backslashes
Talking about inheritance in Python
What can python crawlers crawl
Noteworthy update points in Python 3.9
What databases can python use
Does Python support multiple inheritance?
Containerize Python applications in 3 minutes
How does python update packages
What is Python variable scope
Generators and iterators in Python
Talking about strings in Python