Python Basic 5 primary function

Source: Internet
Author: User

The most important purpose of the function is to make it easy for us to reuse the same program. Some operations are subordinate to a function, and in the future when you want to implement the same operation, you can simply invoke the function name, without having to repeatedly knock all the statements.

def My_len ():           def keyword    Li=[1,2,3,4,5,6]    = 0    for in  li:        count+=1 return      count     return keyword set=My_len ()        function call and return value of the receive  Print(set)

Writing functions is best not to print in functions, as far as possible function-oriented.

return value:

A condition with no return value:

1. Do not write Return===return None

2. Just write a return, not with the thing behind ==return None

Two cases with return value:

Function of return: 1. Returns one or more values

2. Terminates the continuation of a function.

def My_len ():     Print (1)     Print (2)     return    Print (+) Print (A) Print (My_len ())

Results:

The               # # # #为什么会先打印出44, because the program first runs to the Def Line, and then runs print (44), instead of running print (1), because the function has not yet been called. None

1. Returns a value: What data type can be returned and what is received.

2. Return multiple values:

If a variable is used to receive the return value , it returns a tuple, why a tuple is returned, and the concept of Python parsing package is used here. Python has a method of assigning values to multiple variables at once called sequence unpacking.

Example 1

>>> 1,3            input (1, 3)               results >>> 2,3,6         input (2, 3, 6)            results
x,y,z=1,2,3Print(x, Y, z)           two variables swap x, y=y,xprint(x, Y, z)

It allows the function to return more than one value and to package the Narimoto group, which is then easily accessed through an assignment statement. The number of elements in the unpacked sequence must be exactly the same as the number of variables placed to the left of the assignment symbol (=), otherwise Python throws an exception when the value is assigned.

Example 2:

def My_len ():     return 2, 3,4set1,set2,set3=my_len ()print(Set1,set2 set3)

Results:

(2, 3)

The return value is received with multiple variables, so the number of returned values should be the same as the number of received values, otherwise an error will be made.

Example one:

def My_len ():     return 2, 3,4set1,set2,set3=my_len ()print(Set1,set2, Set3)

Results:

2 3 4

Parameters:

def func (LST):             receive parameter called formal parameter    count=0    for in  lst:        count+=1    return  countl=[1,2,34,5print(func (L))           incoming parameter called argument

The pass function can pass arbitrary data types and transmit what to receive.

Standing in the parameters of the angle, a total of two kinds of parameters to pass the way.

The first type: Pass parameters according to location.

The second type: Pass parameters according to the keyword.

These two ways can be mixed, but Note that positional parameters cannot be placed after the keyword parameter : A location can accept only one parameter, and it is not allowed to receive multiple parameters.

Default parameters: python provides a default parameter mechanism to simplify the invocation of functions.

Note: 1. The default parameter must be placed after the required parameter, otherwise python will error.

2. The default parameter must point to an immutable object

Example

defClassmate (name,sex="male"):    Print("Name:%s, Gender:%s"%(name,sex))returnClassmate ("Little Red") Classmate ("Small Flower") Classmate ("Little Blue","female")

Results:

Name: Little Red, Sex: Male name: floret, Sex: Male name: Little blue, Sex: female

Default parameters for pits:

Example:

def add_end (l=[]):    l.append ('we')    return  Lprint(add_end ())print(Add_end ()) Print (Add_end ([1])) Print (Add_end ())

Results
['we'][ ' we ' ][1, ' we ' ['we ', ' we ' We'we']

Nani, why not always be [' we '].

Python Basic 5 primary function

Related Article

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.