Python functions,

Source: Internet
Author: User

Python functions,
I. Functions

1. reduce repeated code

2. Easy to modify and expand

3. Maintain code consistency

Ii. Function Format

Def function name ():

Function body

Iii. Naming rules for function names:

1. The function name must start with an underscore (_) or a letter. It can contain any combination of letters, numbers, or underscores (_) and cannot contain any punctuation.

2. The function name is case sensitive.

3. The function name cannot be a reserved word.

Iv. Call

Function Name ()

V. function parameters 1. Direct Parameters

When defining a function, place the parameters in brackets. That is, def function name (parameter 1, parameter 2 ,......)

Def function (name, age): # define a function

Print ('name is % s, age is % s' % (name, age) # function content

Function ('Tom ', '18') # Call the function. The result is name is tom and age is 18.

2. Keyword Parameters

Def function (name, age): # define a function

Print ('name is % s, age is % s' % (name, age) # function content

Function (age = '18', name = 'Tom ') # Call a function. After assigning a value to a parameter, you do not need to specify one-to-one correspondence when passing the parameter value. The result is name is tom, age is 18.

3. Default Parameters

Def function (name, age, sex = 'man '):

Print ('name is % s, age is % s, is % s' % (name, age, sex ))

Function (age = '18', name = 'Tom ') # The default value of sex is 'man', name is tom, age is 18, is man.

Function ('Jim ', '20') # The default value of sex is 'man', name is jim, age is 20, is man.

Function ('Anna ', '20', 'Woman') # assign 'women', name is anna, age is 20, is woman to the value of sex.

4. variable length parameter a) variable length is achieved by converting the parameter into a tuples

Add "*" before the parameter.

Def add (* add): # define the function add

Print (add) # function content

Add (1, 2, 3) # Call a function. The result is (1, 2, 3)

B) variable length can be achieved by converting parameters into dictionaries.

Add "**" before the parameter. For mixed parameters (variable length parameters for conversion of tuples and variable length parameters for dictionary conversion, the variable length parameters that need to be passed separately and the conversion of tuples must be placed on the front side, and the variable length parameters for dictionary conversion must be placed on the back side, as shown below:

Def add (* add, ** args): # define a function

Print (add, args) # function content

Add (1, 2, 3, job = 'it', holobby = 'sing') # Call the function (1, 2, 3) {'job': 'it ', 'hobby': 'sing '}

Vi. return1 and Return functions:

A) end Function

B) returns an object.

2. Set the return value.
Def add (* addS): # define the function add
Sum = 0 # define function content
    for i in addS:
        Sum += i
Return Sum # Set the return value
A = add (1, 2, 3) # Call A function. And assign the returned value to.
print(A)                             #6
Note:
1. If there is no return in the function, a None is returned by default.
2. If multiple objects are returned, Python will encapsulate multiple objects into a single tuples.
VII. nonlocal and global

Python has four scopes:

L: local, local scope, that is, the variables defined in the function;

E: enclosing: The local scope of nested parent functions, that is, the local scope of the upper-level functions that contain this function, but not global;

G: global. global variables are defined at the module level;

B: built-in, variables in the system's fixed module, such as int and byrearray. The priority order of search variables is: local scope> outer scope> global variables in the current module> Python built-in scope, that is, LEGB.

X = int (2.9)# Int built-in
G_count = 0# Global
DefOut ():
O_count = 1# Enclosing
DefInner_1 ():
I _count = 2# Local

 

Variables defined outside the function are global variables, and variables defined in the function. If there is no special description, they are local variables. You can call global variables in a function but cannot change the value of global variables. To change the value of a global variable, you must add the global declaration before the variable as a global variable.

If you define an internal function when defining a function, you need to declare it using nonlocal.

Conut = 0
DefOuter ():
Conut = 10
# Print (conut)
DefInner ():
NonlocalConut
Conut = 20
Print (conut)
# Return conut
Inner ()
Outer ()

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.