Python function template

# Collection parameters variable length parameter is when the parameters did not know how much time would pass such a parameter representation like

DEF Test (* Vary):
     Print ( " length parameter is: " , len (Vary))
     Print ( " The third parameter is: " , Vary [2 ])

test(123,'ccd',[1,2,3,4],'23546')

Print () # outputs a space-time grid 
DEF test2 (* Vary, exp):
     Print ( " exp This parameter is: " , exp)
     Print ( " collected is the second parameter parameters: " , Vary [. 1 ])

test2(23,"hello",[1,2,3,4],"it's you",exp="EXP")

# In order not to confuse its own specific wants time parameters (exp) and then pass the trouble to collect parameters should also add keyword parameters Oh, keyword arguments to be placed at the end when the re-transmission of
# or mess default parameters directly in the form reference there ordained exp = "eXP", parameter passing time like direct eXP

Print ()
 DEF test2 (* Vary, exp = " the EXP " ):
     Print ( " exp This parameter is: " , exp)
     Print ( " collected is the second parameter parameters: " , Vary [. 1 ])

test2(23,"hello",[1,2,3,4],"it's you","EXP")
>>> def hello():
    print("hello world")    
>>> temp = hello()
hello world
>>> temp
>>> print(temp)
None
>>> type(temp)
<class 'NoneType'>
>>> # Even if no return statement or Python function will return something either return return or return none 
>>>

 

Return multiple values
 >>> DEF Back ():
     return . 1, ' One ' , 3.1414

>>> back()
(1, 'one', 3.1414)
>>> 

 

Global variables can be applied directly in the function and output, but a when you modify a global variable in a function, you will find that the output of the variables and values in a function output is not the same in function in vitro,
because once you change global variables, on the provisional application will function in the same name as a local variable, the function was modified, the modification is that local variables
(think for a ha .. still no change, because there is no pyhon variable declaration statement it so emm ~)
# If you want to change the non-global variables in a function values

count = 5
def MyFun():
    global count
    count=10
    print('aaa')

Print (COUNT) # output 5 
Print ()
MyFun()
Print (COUNT) # After calling the function output 10

 

# Built-in functions 
DEF fun1 ():
     Print ( " Here is fun1 function " )
     DEF fun2 ():
         Print ( " Here is fun2 function " )
    fun2 () # ! no words can not output fun2

fun1()
# And the fun1 site will no longer be able to call a function fun2 
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
 # function closure package fun2 function is a closure empathy is not available outside fun1 site using 
DEF fun1 (the X-):
     DEF fun2 (the y-):
         return the X-* the y-
     return fun2 # Since Pyhon everything is an Object so I write here so like 

the Result = fun1 (8) (5) # !!!! embodiment 
Print (Result)
、、、、、、、、、、、、、、、、、、、

 

count = 5
def text():
    count=count+1
    print(count)

text()
# Error because at this time you modify global variables count to apply for a new temporary variable ++ and direct but no initial value 
so:
 DEF fun1 ():
    x = 5
    def fun2():
        the X-nonlocal # !!! order to be able to apply tougher than the local variable 
        the X-* = the X-
         return the X-
     return fun2 () # ! Here we must add () Yeah

print(fun1())

 

# The lambda expression preceding the colon is the original function parameter after the colon is the original function return value 
DEF DS (X):
     return 2 * X +. 1

print(ds(5))
print()
G = the lambda X: 2 * X + 2
 Print (G (. 5 ))
 # ############# two parameters 
DEF the Add (X, Y):
     return X + Y
 Print (the Add (. 1 , 2 ))

g = lambda x,y:x+y
print(g(1,2))

 

,,,,,,,,,,,,,,,,,,
 # filter is to filter any non-true things to filter out 
list1 = list (filter (None, [0,1, True, False])) # The first argument is None or funcion (what they want mesh) 
Print (List1)
 # output: [. 1, True] 
Print ()
 DEF oDD (X):
     return X% 2 # find odd 

TEMP = Range (10 )
show = filter (ODD, TEMP) # here functions like a write-only do not add the function name () 
# At this time show an object list can be expressed in 
Print (list (show)) # output [1, 3, 5, 7 , 9]

# Implemented by the lambda 
Print (List (filter ( the lambda X: X 2%, Range (10 ))))
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
# Map filter parameters is still a function and iterative sequence 
# sequence of each element as a parameter calculation processing function until available iterative sequence element processing, return a new sequence 
Print (List (Map ( the lambda X: 2 * X, Range (10 ))))
 # outputs [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# BIF is built with two built-in functions Pyhon BIF iter () and next ()
# of a container object which is obtained by the iterator iter
# next call the iterator will return the next value
to provide a container of the iterative method # called the iterator: a list of tuples string dictionaries, etc. (these are container object)

for i in "ilovekk":
    print(i)
print()
directions = {"":'c',\
              '':'x',\
              '':'k'}
for each in directions:
    print("%s -> %s" %(each,directions[each]))
print()
String = " ilove Caicai " 
IT = ITER (String) # IT is an iterator the (string obtained container iterator) 
Print (Next (IT)) # I 
Print (Next (IT)) # L 
Print (Next (IT)) # O 
Print (Next (IT)) # V 
Print (Next (IT)) # E 
Print (Next (IT)) # dish 
Print (Next (IT)) # dish 
Print (Next (IT)) # iterator nothing can return to the pop StopIteration 
Print ()
string = 'fishc'
it = iter(string)
while True:
    try:
        each = next(it)
    except StopIteration:
        break
    print(each)

# Builder is a normal function in Riga yield statement
# python makes imitate cooperative program (function can pause or suspend and resume from where it left the program when needed or restart) can be achieved

>>> DEF mygun ():
     Print ( " producers are executed " )
     yield . 1
     yield 2
     # yield add a statement that functions as a generator, will yield equivalent yield only return parameters return later, and suspended in this position from the start of the next round next     
>>> MYG = mygun ()
 >>> the next (MYG)
Generator is performed
1
>>> next(myg)
2
>>> next(myg)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    next(myg)
StopIteration
>>> 
>>> for i in mygun():
    print(i)    
Generator is performed
1
2
>>> def libs():
    a = 0
    b = 1
     the while True: # seems to be a cycle of death as has been true but it is a generator ready interrupt pending and your first performance in the end of the next time can certainly perform ah to start from scratch chanting during a function 
        a, b = B, A + B
         the yield A

>>> for each in libs():
    if each > 100:
        break;
    else:
        print(each, end=" ")

        
1 1 2 3 5 8 13 21 34 55 89 

# Import module
to add the module prefix text.fun () when # 1. Import module name without adding the suffix .py / modules in the function call
# 2. From the module name import function name function name in parentheses even if you can not function there are parameters to be passed
. # 3 from module import * wildcard name inside all imported namespaces / not recommended
# 4 import module name as the name of the new natural, call the template function of time to bring this new name as a prefix.
role: packaging Code

 

> # If the main program using __name__ variable is an '__main__' template to get such a test is '__ __ template name' by __name__ == IF '__ main__'
> # can be judged is the main program or template and then decide whether or not to run or test
>>> Import SYS
>>> sys.path
[ 'E: \\ \\ TxGameDownload the Temp \\ Python', 'D: \\ Python Lib \\ \\ idlelib', 'D: \\ Python \\ python37.zip ',' D : \\ Python \\ DLLs ',' D: \\ Python \\ lib ',' D: \\ Python ',' C: \\ Users \\ dream \ \ AppData \\ Roaming \\ Python \\ Python37 \\ site-packages', 'D: \\ Python \\ lib \\ site-packages']
search path >>> system is out of the results are displayed as a list so of course, you can increase the path
sys.path.append ( "E: \\ python \\ test") introduced this folder
to import the package: the package name module name.

Guess you like

Origin www.cnblogs.com/yundong333/p/11074795.html