Python Learning bit record-day03

Source: Internet
Author: User

Function basics

I. Benefits of using functions

#使代码组织结构清晰
#减少代码冗余
#可以统一管理且降低维护难度
function can be used as a tool in daily life, with some kind of function of the object, directly to use.

Second, the classification of functions

Built-in functions: The Python interpreter has a number of functions built into it for developers to use directly

Custom functions: Functions that you write according to your own needs

Third, the use of functions

Defining functions

The definition of a function is similar to the definition of a variable and needs to be defined in advance to call
Only syntax is detected when a function is defined, no code is executed
def function name (ARG1,ARG2,ARG3):
"' Notes '
function body
return value

Calling functions

Function name ()

Iv. three forms of defining functions

No parameters: The scenario simply performs some action, such as interacting with the user, printing
Parameter: It is necessary to perform the corresponding logic, such as statistical length, to calculate the maximum minimum value, according to the parameters passed in externally.
Empty function: Design code structure

V. Invocation of functions

Find the name of the function first
Calling code based on function name
The return value of a function does not have any type restrictions
0->none no return, default return None
1-> returns 1 values return a value returns the value
Multiple---tuples return can also return multiple values, list, dic, tuple, etc.
When should I have a return?
Call the function, after a series of operations, finally to get a definite result, you must have a return value
Usually have the parameter function need to have the return value, the input parameter, after computation, obtains one final result
When do I need to have a return?
Call the function, just perform a series of operations, and finally do not need to get any results, there is no need to have a return value
Normally no parameter function does not need to have a return value

Vi. three ways to call a function

  1 Statement form: foo () #单单的只是执行一个函数
2 expression form: 3*len (' hello ') #将执行函数后的返回值做运算操作

Seven, the function of the parameters explained

1. Definition

Formal parameters: When defining a function, the arguments in the function parentheses are called formal arguments

Features: Formal parameters can be interpreted as variable names

def my_func (x, y): #x, Y is the formal parameter

Print (x)

Print (y)

Arguments: When a function is called, the argument inside the function's parentheses is called an argument

Features: Arguments can be understood as variable values

My_func (#1), 2 is the argument

2. Understanding

Parameter (variable name) will be bound when called (variable value) argument

After the call is finished, unbind

3. Specific application

Positional parameters: Parameters defined in left-to-right order

Positional parameters: Required parameter
Positional arguments: Pass values to parameters by location

Keyword parameter: In the form of Key=value

Note: The keyword parameter must be placed behind the positional argument

and can only pass the value once

Default parameter: When a function is defined, it is already assigned a value, which means that it is not possible to pass a value at the call stage

Note: The default parameter must be placed after the position parameter

The default parameter is assigned only once in the definition phase, and only once

The value of the default parameter should be defined as an immutable type

Named keyword parameter: the parameter defined after * is called a named keyword argument and must be passed as a keyword argument

Python Learning bit record-day03

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.