1.Python Foundation
2. Basic data type: Str,dict,list,int
s= "WZC" =>str
3. Functional programming
function definition
Built-in functions
File processing
Attention:
Li=[11,22,33,44]def Fi (ARG): arg.append (in) fi (LI) print (LI) li=fi (LI) print (LI)
It is necessary to note that the function returns none by default, so we need to refer to it by parameter when we use it.
4. Other
Ternary operations
Lambda expression
s= "haha" bytes (s,encoding=utf-8) false:[],{}, (), none,0, ""
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcdef F1 (): passprint (Callable (F1))
英文说明: Check whether object is callable. If return true,object still may fail, but if return false, calling object Ojbect will never succeed.
Note : The class is callable, and an instance of the class implements the __call__ () method to invoke.
version : This function is available in the python2.x version. However, it was removed in the python3.0 version and was re-added in the later version of python3.2.
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzca=chr (+) print (a) B=ord ("B") print (b)
ASCII code can be found , can also be reversed to find , the number converted to letters, letters converted to numbers, which need to rely on the corresponding ASCII code relationship
Generate random numbers
Andom. Randrange This is the range of random numbers . If you combine with CHR, there will be random letters, but be aware that Acsii is a range, and65-90 is the range of uppercase letters .
The scope of this is 1<=i<5 , so you need to add a bit later when you type.
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcimport randomli=[]num=0for i in range (6): r= Random.randrange (0,5) if r = = 2 or r = = 4: c=random.randrange (0,9) b=str (c) Li.append (b) else: c=random.randrange (65,91) B=CHR (c) Li.append (b) num=0re= "". Join (LI) print (re)
This is a random verification code that produces a combination of numbers and English.
1. read file contents open,str to memory
2.python, string = "compile =" Special code
3. Execute code
Compile, which is used to compile the code.
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcs= ' Print ("123") ' #将字符串编译成python code
R=compile (S, "string", "exec") # There's no relationship between exec and the following
Print (R) Exec (r) #执行Python code
Single: Compile the above code into a one-line Python program
Eval: Compiling into an expression
EXEC: Compiling to the exact same pattern
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcr1=exec ("7+8+9")
Print (R1)
R2=eval ("7+8+9") #eval: This function allows you to convert a string to an expression, and to perform the following
Print (R2)
EXEC: better than eval , exec can execute all Python code and strings, and eval can only execute expressions
But one more function of eval than exec is that Eval has a return value, and exec has no
Quick view of what features the object provides
Print (dir (list))
Paging function
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcr=divmod (99,10) print (R)
The paging function is the calculation of the quotient, while the remainder can be obtained. returned is a meta-ancestor
Object is an instance of a class
Object "WZC", "www" their class is "str"
objects [11,22,33],["W", "Z", "C"] their class is List
And so on Dict's class, everybody knows.
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzca=["A", "B"]r=isinstance (a,list) print (R)
Isinstance, Determines whether an object is an instance of a class
The difference between this and type is that isinstance can be looked up, used in the base class, and type cannot be
Filter (functions, objects that can be iterated)
Lists, meta-ancestors, dictionaries, are objects that can be iterated
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcli=[11,22,33,44,]def F1 (ARG): If arg > 22:return Tru Eret=filter (f1,li) Print (list (ret))
Inside the filter, the second parameter is first cycled and then the first parameter is executed inside each loop
#filter内部, loop the second parameter #result=[] #for item in the second parameter # r= the first argument (item) # if r:# result (item) #return result# The second parameter of the filter loop function allows each loop element to execute a function if the function returns Ture, indicating that the element is valid
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcli=[11,22,33,44,]def F1 (ARG): if arg >: return True Ret=filter (f1,li) Print (list (ret) li=[11,22,33,44,]ret1=filter (lambda a:a>33,li) print (list (RET1)) Ret=map ( Lambda a:a+100,li) Print (list (ret))
This allows the same functionality to be achieved with La MBDA expressions
Map (functions, objects that can be iterated (something for Loop))
Filter: function returns ture and adds elements to the result
Map: Add a function return value to the result
Frozenset (): Immutable collection
Globals (): All Global variables
Locals (): All local variables
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcs= ' wzcceqwediojdfiosjdjkfnjkwdhuiahjcnzkxcnx ' Print (hash (s))
At the time of storage, more is to save a hash value, other languages are basically this
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcs= "haha" print (len (s)) b=bytes (s,encoding= "Utf-8") print (len (b))
Python3 is by character, Python2 is in bytes , Python3 can be passed by character, or it can be viewed by byte
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcs= ' {"K1": "v1"} ' #里面必须是双引号, single quote not print (type (s), s) Import jsonn= Json.loads (s) #将一个字符串转换成Python基本数据类型 {},[]print (Type (n), N)
To convert through JSON , the elements inside must be enclosed in double quotes and single quotes .
Max (): Max
Min (): min
SUM (): sum
Memoryview (): A class related to memory address
Object (): A class that is a sub-class of all classes
POW (): Seeking square, equivalent to 2**10==pow (2,10)
Reverse (): Flip, List.reverse () ==reverse (list)
Round (): Rounding
Slice (): Take interval, s= "12345", print ([0:2:2]) function same
Sorted (): Sort, List.sort () =sorted (list)
Built-in functions that need to be mastered
Adorner : decorating a function from the outside
Open Closure principle
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzc# def f1 (): # print ("F1") # def f2 (XXX): # xxx () # F2 (F1) def o Uter (func): def Inter (*args, **kwargs): print ("before") R=func (*args, **kwargs) print ("after") return R return inter@outerdef F1 (ARG): print (ARG) return "haha" r=f1 ("Heihei") print (r) print ("#") @ Outerdef F2 (A1,A2): print (A1,A2) F2 (11,12)
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcdef F1 (): Print ("F1") def f2 (XXX): xxx () f2 (F1)
A function can be called as a parameter, and the F 1 inside is executed.
def outer (func): Def Inter (): print ("before") func () print ("after") return Inter@outerdef F1 (): Print ("F1") F1 ()
@+ function
Function:
1, automatically executes the outer function, and the following function name F1 as a parameter passed
2, the return value of the outer function is re-assigned to F1
#!/usr/bin/env python#-*-coding:utf-8-*-# author:wzcdef Outer (func): def Inter (*args, **kwargs): Print ("Befo Re ") R=func (*args, **kwargs) print (" after ") return R return inter@outerdef F1 (ARG): Print (ARG) return "haha" r=f1 ("Heihei") print (r) print ("#") @outerdef F2 (A1,A2): print (A1,A2) F2 (11,12)
When the fi has parameters, the adorner outer also needs to follow the parameters to execute
When there are multiple parameters, you can use * * to handle
Python Finishing-day4