Python function One

Source: Internet
Author: User
Tags function definition

First, what is the 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. such as print (), Len (), and so on. But you can also create your own functions, which are called user-defined functions.

Second, the definition and invocation of functions
# function Definition def Mylen ():     """ calculate the length of a S1 """   ' Helloworld'    = 0    for      inch S1:         = length+1    print(length)# function call   Mylen ()

The above is where we wrote a function and successfully called it.

Definition: Thedef keyword begins with a space followed by the function name and parentheses (), and finally a ":".  def

Functions are function-oriented, and the function interior should not have print

Third, the return value of the function

The function of the return keyword

1. no return value

If you do not write a return, a none is returned by default

return None Returns None

2. Returns a value

Just write what you want to return after return.

3. Return multiple values

Can return any number of values of any data type

defret_demo1 ():" "return multiple values" "     return1,2,3,4defRet_demo2 ():" "returns multiple values of any type" "    return1,['a','b'],3,4Ret1=Ret_demo1 ()Print(RET1) Ret2=Ret_demo2 ()Print(Ret2)

Multiple values returned are returned by the organization Narimoto group, or multiple values can be used to receive

defRet_demo2 ():return1,['a','b'],3,4#returns multiple values, received with a variableRet2 =Ret_demo2 ()Print(Ret2)#returns multiple values, received with multiple variablesA,b,c,d =Ret_demo2 ()Print(a,b,c,d)#receive return values with multiple values: Returns several values, received with several variables, must be one by one correspondingA,b,c,d =Ret_demo2 ()Print(a,b,c,d) receive of multiple return values
Four, the parameters of the function

Real participation Formal parameters

The parameters are also different:

The "Hello World" passed when we called the function is called the actual argument, because this is the actual content to be given to the function, referred to as the argument .

The name of the variable that defines the function is called the formal parameter , because it is only a form when defining a function, which means that there is a parameter, referred to as a formal argument.

Passing Multiple parameters

Parameters can be passed multiple, and multiple parameters are separated by commas.

def Mymax (x, y    ): if Else y     return  = Mymax (10,20)print(MA)

Position parameters

Standing at the argument angle

1. Transfer Values by location

def Mymax (x, y    ): # at this time x=10,y=20    if Else y     return  = Mymax (10,20)print(MA)

2. Pass the value by keyword

def Mymax (x, y    ): # at this point x = 20,y = Ten    Print (x,    y) if Else y     return  = Mymax (y = 10,x =)print(MA)

3. The position, the keyword form mixes with

def Mymax (x, y    ): # at this point x = 10,y =    Print (x,    y) if Else y     return  = Mymax (10,y =)print(MA)

Correct usage:

Problem one: The positional parameter must precede the keyword argument

Problem two: Only one parameter can be assigned once

Standing in the form parameter angle

Positional parameters must be passed value

defMymax (x, y):#at this point x = 10,y =    Print(x, y) The_max= XifX > YElseyreturnThe_max#calling Mymax does not pass parametersMa =Mymax ()Print(MA)#ResultsTypeerror:mymax () Missing 2 required positional arguments:'x'  and 'y'

Ternary Operation method

def Leng (x, y        ): if Else y         return Z Print (Leng ('1','2'))

Default parameters

1. Normal use

How to use

Why do I have a default parameter: Set the small change value to the default parameter

2. Definition of default parameters

def " male " ):    "" " print student information function, because most of the students in the class are boys,        so set the default parameter sex's default value is    ' Male '" "    Print(name,sex) stu_info ('Alex') stu_info (  'Eva','female')

3. Parameter traps: The default parameter is a mutable data type

def defult_param (a,l = []):    l.append (a)    print(l) defult_param ('  Alex') defult_param ('Egon')

Python function One

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.