(record) Python article: six _ Function 1

Source: Internet
Author: User

Function

One: Custom functions

def num (x): #def定义函数代码, the name of the NUM custom function, the (x) function parameter, or a,b,x), or it can be defined as empty ...     The middle can be used as the judgment if, can also use the loop can other such definition and so on. Return x #返回的函数, can be multiple and return a,b,x

#return和print的区别:

Return jumps out of the current loop and prints

Print printing


Two: Parameters of the function

The parameters are categorized as:

Required parameters, default parameters, mutable parameters, named keyword parameters, and keyword parameters

The order of the parameter definitions must be:

Required parameters, default parameters, mutable parameters, named keyword parameters, and keyword parameters


Three: Required Parameters:

Def num (A, A, b): #定义num函数, the parameters are a and a, you must enter two parameters, not many and few, type customization, can be a return a, a, a string, and so on.


Four: Default parameters:

def num (a,b=2): #定义num函数, the parameters are A and B, Num (a) when the output of a parameter, at this point B will also be output by default return, A, a (a,2), if the output num (a,w), the default parameter becomes W.


Five: Variable parameters:

Is any combination of one or more, his length is variable, but it must be a tuple type.

(1): When the variable parameter is not added

def num (a): for N in a:n=a print (n)

Results: num= ([+]) or num= (()). The length is variable, but the internal Canadian dollar ancestor or list contains a combination that, if combined as num= (1), is not added internally at this time.

(2): Plus after

def num (*a): for-N in a:n=a print (n)

Results: num= (+/-), Parameter plus * represents variable, which simplifies the operation at this time.


Six: named keyword parameter:

def num (a,*,b,c): Print (A,B,C)

Result: The preceding parameter A is the default parameter, the post-named keyword parameter

Num (1,b=2,c=3) >>1 2 3

Must be added *


Seven: Keyword parameters:

def num (a,**kw): Print ("a=", A, "other=", kw)

Result: At this time * * Before the parameter is a is the default parameter, the Defender keyword parameter (kw can be defined, cannot be removed primarily for output)

Num (1) >>> "a=" 1, "other" ={}num (1,k= "B") >>> "a" =1, "other" ={"B" = "k"}

Summarize:

The above parameters can be added to a set, but must be assigned in order;

Required parameters restrict the input of the primary user to the required type;

The default parameter must use the immutable object, if it is a mutable object, the program will run with a logic error;

The variable parameter receives a tuple;

The keyword parameter receives a dict;

The keyword name parameter is primarily intended to limit the parameter name.



Learn from the Liu Junfeng of the official website of the study summary!

This article is from the "Twist Blog" blog, please be sure to keep this source http://mahua.blog.51cto.com/11572858/1969538

(record) Python article: six _ Function 1

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.