Python3 - The function

1. Definitions

Function: implements a particular function.

     It can be reused.

E.g:

len () Function: Get length.
input () Function: Input console
print () function: Output

grammar:

def function name (parameter):
  # code that implements the function block.

2. function parameters

2.1 positional parameters (will pass, when the value is not defined.)

Order parameter and parameter arguments corresponded to

In the form of parameters, received specific values. Will become

E.g:

DEF out_going (the above mentioned id, Ticket):
     Print ( " Please produce their identity cards and airline tickets! " )
     Print ( " ID is: " + the above mentioned id, " ticket is: " + Ticket)
     IF ! len (the above mentioned id) = 18 :
         Print ( " ID cards do not meet the requirements !! " )
     IF the above mentioned id IS  not None and Ticket IS  not None:
         Print ( " ! Please aircraft " ) 

# call the function 
out_going ( "363636200001023212 " , " Eastern Airlines " ) 

Result: 
Please produce their identity cards and airline tickets! 
ID is: 363636200001023212 airline tickets: Eastern Airlines 
, please on the plane!

2.2 default parameters (defining a function, parameter to a specific default value.)

Chuan Chuan can not

After the default parameters to pass on all the necessary parameters

E.g:

DEF out_going (the above mentioned id, Ticket, the Enter = " Shanghai " , H = " 5 " ): 
    Enters = [ " Shanghai " , " Beijing " , " Shenzhen " ]
     Print ( " ! Please show identity cards and airline tickets " )
     Print ( " ID is: " + the above mentioned id + " \ the n- " , " ticket is: " + ticket + " \ the n- " , "Arrival City is: " + the Enter +" \ The n- " , " time-consuming: " + H + " \ the n- " )
     IF len (the above mentioned id) = 18! :
         Print ( " ID cards do not meet the requirements !! " )
     elif the Enter not  in Enters:
         Print ( " Current City It has yet to open the airport !! " )
     elif the above mentioned id IS  not None and Ticket IS  not None:
         Print ( " ! Please aircraft " ) 

# call the function
out_going ( " 363636200001023212 " , " Eastern Airlines " ) 

Result: 
Please produce their identity cards and airline tickets! 
 ID is: 363636200001023212 
 airline tickets: Tickets 
 arrival cities: Shanghai 
 time: 5 

please on the plane!

2.3 specified parameters (time of the call to specify the parameter data =)

= Parameter value. Order may not be the position of mass participation

E.g:

DEF out_going (the above mentioned id, Ticket, the Enter = " Shanghai " , H = " 5 " ): 
    Enters = [ " Shanghai " , " Beijing " , " Shenzhen " ]
     Print ( " ! Please show identity cards and airline tickets " )
     Print ( " ID is: " + the above mentioned id + " \ the n- " , " ticket is: " + ticket + " \ the n- " , "Arrival City is: " + the Enter +" \ The n- " , " time-consuming: " + H + " \ the n- " )
     IF len (the above mentioned id) = 18! :
         Print ( " ID cards do not meet the requirements !! " )
     elif the Enter not  in Enters:
         Print ( " Current City It has yet to open the airport !! " )
     elif the above mentioned id IS  not None and Ticket IS  not None:
         Print ( " ! Please aircraft " ) 

# call the function
out_going ( " 363636200001023212 " , " Eastern Airlines " , H = " 10 " , the Enter = " Beijing " ) 

Result: 
Please produce their identity cards and airline tickets! 
 ID is: 363636200001023212 
 airline tickets: Eastern Airlines 
 arrive cities: Beijing 
 time: 10 

please on the plane!

2.4 variable parameter (* args, ** kwargs)

A fixed number of parameters. Time of the call to determine several parameters.

*args

Inside the function, in the form of tuples to represent.

After the on position parameters, default parameters.

E.g:

def my_args(*args):
    print(args)

# 调用函数
my_args(12,34,True,False,[1,2])
my_args("hello",True)
my_args()

结果:
(12, 34, True, False, [1, 2])
('hello', True)
()

** kwargs

Inside the function, it is expressed in the form of a dictionary.

Definition of a function which, have * args, ** kwargs. First * args, then ** kwargs.

E.g:

def my_all_args(num,*args,**kwargs):
    print(num,args,kwargs)
    if "my_class" in kwargs:
        if kwargs["my_class"] == "python17":
            print("正确!")
            
# 调用函数
my_all_args(12,23,34,45,hello="world",name="python17")

结果:
12 (23, 34, 45) {'hello': 'world', 'name': 'python17'}

3. The function returns the (return)

1, data representative of the function returns

2, the function call is terminated 

3, can not return back with any data. In fact, it represents the return None. return None

4, call functions, if the function returns a value, to take the initiative receives the return value of the function with variable

5, return any type of data can be returned

6, defined functions when not used return. Will call the function, returns a value? There are, to None

Guess you like

Origin www.cnblogs.com/renshengruxi/p/12084532.html