Python Foundation Nineth Day

Source: Internet
Author: User

Today's content

The first knowledge of 1 functions

function name, function body, keyword, function return value.

2. Parameters of the function

1. Initial knowledge of functions

What is a function:

Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function .

Functions can improve the modularity of the application, and the reuse of the code.

Basic structure of a function

Def my_len ():
Count = 0
For I in L1:
Count + = 1
Print (count)

My_len () #函数的执行: function name + ()

def keyword. Define a function

My_len function name. Writing rules are the same as variables.

A space between Def and the function name.

Function name (): Plus colon

function Body:

Colon (:) code written below

Note: Write function, do not write print () in function

The function of return

1) in the function, a return is encountered to indicate the end function. Similar break

2) Returns the value to the caller of the function.

return value of the function

1). No return in function

return None

2) return None

The return value is None

3) return returns a value that is the value that is returned directly to the caller of the function.

Caller is function name ()

4) Teturn returns multiple values, puts multiple values into a tuple, and returns to the caller of the function.

2. Parameters of the function:

There are two aspects to the function's parameter transfer:

Actual parameter angle:
1, location parameter. correspond in order.

2, the key word to pass, not in order, each correspondence.

3, mixed parameter, the keyword argument is always behind the position parameter.

Formal parameter angle:
1, location parameter. correspond in order.

Consistent with the above operation.

2, default parameter. The pass-through is overwritten, and the default parameter is always followed by the position parameter.

1) If set, override default value

2) If not set, the value of the default parameter is used

Exercises:

Enter the name and gender of the person in the class into a document and ask for a person to be in line.

Here are the questions that are done with the function:

def infor_entry (username,sex= ' man '):
With open (' name_list ', encoding= ' utf-8 ', mode= ' a ') as F:
F.write (' {}\t{}\n '. Format (username,sex))

While True:
username = input (' name (boys start with 1): ')
if ' 1 ' in username:
username = username[1:]
infor_entry (username)
Else:
infor_entry (username, ' female ')

Python Foundation Nineth Day

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.