python functions defined basic grammar 5

function

1. What is the function of
  a function is a tool.
  You can recall

2, why should function
  1, to prevent the code redundancy (rong) of more than
  2, poor readability

3, how to use the function

  1, defined functions -> manufacturing tool
  2, the function calls -> tools


  1, no function parameter:
    DEF index ():
    Print ( 'OK')
  2, empty function:
    DEF Login ():
      Pass
  . 3, has a function parameter:
    DEF Login (username):
      Print (username)

It can be: a = index

   a () function using

Return Value:
  1, do not write return: default return None
  2, write only return: only the end effect function body returns None
  . 3, the write return None: write only return the same effect
  . 4, return to return a value: may be returned results, is used as a variable value
  5, return return multiple values:
    1, the plurality of return values, stored in the default tuple returns
    2, the return value of the function is modified not want
    3, return data may specify their own the type
parameters of the function:
  type parameters:
    parameter:
    argument:

  Parameter passing mode:
    the location parameter
    keyword arguments
    default parameters

  The variable length parameters:
    * args: receiving all overflowing positional parameters
    ** kwargs: receiving all keyword parameters overflow
    *: the argument is broken into

 

Naming function names and variable names, like

Keyword (def) function name (index) in brackets:
  Function Description: The function description function body code

def index(a, b):
    if a > b:
        return a
    else:
        return b
 print(index(1, index(2, 3)))    #调用函数

 

Function's return value

1, do not write return: default return None
2, write only return: only the end effect function body returns None
. 3, the write return None: write only return the same effect
. 4, return to return a value: may return results , is used as a variable value
5, return return multiple values:
  1, the plurality of return values, stored in the default tuple returns
  2, the return value of the function is modified not want
  3, you can specify the data type returned their
return: it is a function of the end of the flag, whenever the function body to the return, the end of the function execution

l1 = [1, 6, 3, 4, 5, 6]

def my_len():
    count = 0
    while True:
        for i in l1:
            if i == 4:
                print(count)
                return
            count += 1

print(my_len())
------------------------------
def func(a, b, c, d, e):
    return a, b, c, d, e

print(func(a, b, c, d, e))
-------------------------------
def func1():
    return 1, "2"

print(func1())

Function parameters

Function is to define, after calling
at the definition stage only detect syntax
does not execute any code

positional arguments positional parameters


Parameter:
  a parameter to the function specified in the definition phase, it is equivalent to the definition of variable variable names when
arguments:
  is the incoming call to the stage function parameters, it is equivalent to the variable value of variable definitions

# Python code is executed from top to bottom
index DEF (A):   # parameter: variable name corresponds
Print (A)
Print (B)
IF A> B:
Print (A)
the else:
Print (B)

A. 1 =
B 2 =
index (A) # argument : equivalent to the value of the variable

Location parameters:
  passaged by reference positions

Keyword arguments:
  specify the parameters of mass participation

ps: positional parameters must be sure before the keyword arguments must,
  when used in conjunction, do not repeat the same parameter assignment

DEF foo (A, B, c, d):
     Print (A, B, c, d) 

foo ( . 1, 2, C =. 1, D = 2)     # A, B for the location parameter, c, d keywords parameter

The default parameters:
  function definition phase has passed parameters,
  if you're in argument passed a new parameter, the new parameter will use
  the default parameter values at the time of transfer, not the variable type as a parameter passing

  Scenario:
    using default parameters in the case where the parameter value corresponding to the recurring

DEF REG (username, password, Gender = ' MALE ' ):
     Print (F " User Name: {username}, password: {password}, Gender: {Gender} " ) 

REG ( ' Tank ' , ' DSB ' ) 
REG ( ' Jason ' , ' DSB ' ) 
REG ( ' Egon ' , ' XSB ' ) 
REG ( ' Hu Chenyang ' , ' DSB ' , 'female')

Note: If the default parameters using variable data type, then the phase will address the same

# A first solution 
DEF REG (Hobby, L1 = None):   # four parameter 
    IF L1 == None: 
        L1 = [] 
    l1.append (Hobby) 
    Print (F " hobby: {L1} " ) 


REG (Hobby = ' oysters ' ) 
REG ( ' great care ' ) 
REG ( ' female coaches ' ) 

# The second solution 

DEF REG (Hobby, L1):   # four parameter 
    l1.append (Hobby)
     Print (f " Hobbies: L1} { " )


REG ( ' oysters ' , []) 
REG ( ' great care ' , []) 
REG ( ' female coach ' , [])

Variable-length parameters:
  * args: Receive all overflow location parameter
    values received are stored in a tuple
  official certification: * args
  as long as there is a variable-length parameters * effect

  *: You break up the incoming container type

  ** kwargs: Receive all spilled keyword arguments
    values received are stored in a dictionary

  Official certification: ** kwargs

DEF index (A, B, C, D, E):
     Print (A, B, C, D, E) 

index ( 1,2, * (3,4, 5 ))
 --------- -------------------------------
 DEF index (a, B, C, * args):    # receiving overflow position parameter 
    Print (A, B, C, * args) # is not written print * args tuple 

index ( 1,2,3,4,5)     # . 1 2. 5. 4. 3 
------------- ---------------------------- DEF FUNC (a, B, C, D, ** kW):   # receiving all key overflow word parameter Print (A, B, C, D, kW)     # print dictionary, only write * (broken display Key), not write ** 
FUNC ( . 1, 2,. 3 = C, =. 4 D, E = . 5, F =. 6) # . 1. 4. 3 {2 'E':. 5, 'F':. 6}

    
= L1 [1,2,3,4 ] 
T1 = (1,2,3,4 ) 

Print (L1 *) # . 1 2. 3. 4 
Print (T1 *)   # . 1 2. 4. 3 
-------- ----------------------- DEF foo (A, B, C, D, E, F):
     Print (A, B, C, D, E , F) DEF bar (* args, ** kwargs):     # receives all positional parameters, the keyword parameter 
    foo (* args, ** kwargs) 
bar ( l, 2,3, D = 10, E = 20 is, F = 30)



 

Guess you like

Origin www.cnblogs.com/ludingchao/p/11828665.html
Recommended