Preliminary knowledge of Python functions

Source: Internet
Author: User

One: Why use a function, the function of the definition of the format

1, if there is no function, then the written code will be reused, and the readability is poor.

2, function format :def func (x): def is a keyword, you must define a fun function name (to have a meaning) (x) parameter
the func () function name + () represents the execution of this function .

3. When using a function, be aware that the function is function-oriented and that there is no print inside the function as far as possible.

Two: Five kinds of return values of functions

1. function execution to return, stop, no longer execute

2. Do not write return none

3.return None

4.return Single Value

5.return multiple values are returned after the form of the tuple

1. Return only

def My_len ():    L1=["SBC", "all-in-A-Z"    count=0    for in   L1:        count+=1    returna=My_len ()  Print(a)           #None

2,return None

def My_len ():    L1=["SBC", "all-in-A-Z"    count=0    for in   L1:        count+=1    return  nonea=My_len () Print (a)

3. Do not write return

def My_len ():    L1=["SBC", "all-in-A-Z"    count=0    for   in L1:        count+=1ret=my_len ()print(ret)

4.return A single value returns a value

def My_len ():    L1=["SBC", "all-in-A-Z"    count=0    for in   L1:        count+=1    return  counta=My_len () Print (a)
5.return Multiple values  return multiple values
 def   My_len (): L1  =["  SBC   ", 1,2,3]  return  l1, " Span style= "COLOR: #800000" >a   ", 2,5ret  =my_len ()  print  (ret) #   The result is a tuple ([' SBC ', 1, 2, 3], ' A ', 2, 5)  Ret1,ret2,ret3,ret4 =my_len ()  print  (RET1,RET2,RET3,RET4) Span style= "COLOR: #008000" >#   multiple receive values, assigning values [' SBC ', 1, 2, 3] a 2 5  

Three: The method of the function's parameter (argument and formal parameter)
From the angle analysis of the actual parameters, there are three forms: 1. By position (one by one, less one is not) 2. by keyword 3. Position and keyword Mix (position parameter must be in front, keyword in the rear).

1.1 By Location: 20,18 is the argument, x, y is the formal parameter, and the corresponding relationship is 20,y or 18.

Copy Code def func (x, y    ): if x>y:        return  True    Else:return  Falsea=func (20,18)print(a)

1.2. By keyword: When the argument is passed to the parameter, the 18 is passed to the Xdef func (x, y):
Pass
func (y=20,x=18)

1.3 Mixing: The keyword parameter is always behind the position parameter.
def func (a,b,x,y):
Pass
Func (15,25,y=20,x=18)

1.4 Ternary algorithm: if X>y z=x, otherwise z=y
def func (x, y):
Z=x if x>y else y
Return Z
Print (func (20,18))

2. Formal parameter: Formal parameters
From the perspective of formal parameters: there are two forms
1. Press position 2. Default
Default parameter: The value of Y is always 20

def func (x,y=20):
Pass
Func (18)

Example: Enter name, display name and gender in text, if Name contains ' Fang ', sex shows female
defFunc (Name, sex="male"): With open ("D:\\lianxi\\python\\day2\\ban","a", encoding='Utf-8') as F:f.write ('{} {}\n'. Format (name,sex)) whiletrue:m=input ("Please enter your name:")       if "Fang" inchM:func (M,sex="female")#Result: Small hafnium jerd male small Fang female       Else: Func (m)

Preliminary knowledge of Python functions

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.