Note that this note is not just used to record knowledge points, if you know what to say, then OK, if you see what is written, then review.
1, code comments with "#" number
2, print with a comma "," separated
3. Format the string:
eyes= ' Blue '
Hair= ' Brown '
Print "He ' s got%s eyes and%s hair"% (Eyes,hair)
4. Print "." *10 #打印10个 "."
5. Adaptive Format%r
6, print "How old is You?", #最尾的逗号使print输出换行
Age = Raw_input ()
7, the above example can be written directly:
Age=raw_input ("How old is You?")
8, three quotation marks "" "can define multiple lines of string
9. Incoming parameters:
Assuming that the Py file name is ex20.py, the contents are as follows
from SYS Import argv
Script, input_file = argv
...
Then run Python ex20.py text.txt
Script equals ex20.py, Input_file equals text.txt
10. File operation
Close,read,readline,truncate,write
11. Function:
def print_two (*args):
Arg1,arg2=args
Print Arg1,arg2
12. Conditional statements
If a>b:
Print "A>b"
13. Circular Statements
the_count=[1,2,3,4,5]
For num in The_count:
print "num =%d"%num
14. While I<b:
Print "i=%d"%i
I=i+1
15, dictionary container (can be considered as C + + inside the map)
stuff={' name ': ' Zed ', ' Age ': 36}
Print stuff[' name ']
16, the dictionary container can put functions
def FM (x, y):
....
Fun_map = {' fun ': ' xxx '} #这句没什么意义
fun_map[' my_fun ' = FM
fun_map[' My_fun ' (x, y) #相当于调用fm函数
17. More Flexible cycle
1) for (Index,char) in enumerate (s):
Print Index,char #同时得到下标和元素
2) for (a,b,c) in Zip (TA,TB,TC):
Print (A,B,C)
18. Yield Application, Example:
Def FAB (max):
N,a,b = 0,0,1
While N<max:
Yield b
A,b=b,a+b
N=n+1
For N in Fab (3):
Print n
1
1
2
The yield effect is actually to turn a function into a generator, and the Fab (3) actually returns a Iterable object
19. Map () function
Re=map (lambda x:x+3), [1,3,5,6]) #结果 [4,6,8,9]
Re=map (lambda x,y:x+y), [1,2,3],[6,7,9]) #结果 [7,9,12]
20. Filter function
def func (a):
If a>10:
Return True
Return False
Filter (func,[2,100,32,9]) #结果 [100,32]
21. Reduce function
Reduce ((lambda x,y:x+y), [1,2,3,4]) #结果10
22. Exception Handling
Try
Xxx
except XXX:
Xxx:
except XXX:
Xxx:
Else
Xxx
Finally: #不管是否有异常, the last thing to do
Xxx
23. Python Standard Library
First Class Python Enhancements
1) word processing string,re (regular expression)
2) Data objects, various data types
3) DateTime Date Time
4) Mathematical Operations Decimal,fractions,random,match
5) store In fact Python can input and output arbitrary objects that can be converted to binary format by pickle package, stored in a file, or read backwards
Sqlite3 XML csv with corresponding packages
The second type of system interaction
1) Python Run control
2) Operating system
3) Threads and processes
The third type of network
1) socket
2) HTTP, etc.
24. Sequence method tuple,list,string are all sequences
Len (s) min (s) max (s)
All (s) #如果所有元素都为True, returns True
If any (s) #如任一元素为True, returns True
SUM (s) #返回所有元素的和, but not for string
S.count (x) #x在s中出现的次数
S.index (x) #x在s中第一次出现的位置
L.extend (L2) l.append (x) l.sort () sort l.reverse () reverse order L.pop () del L[i]
25. DateTime
can be calculated and compared
With string conversions:
format = "Ot-%y-%m-%d-%h%m%s.txt"
str = "Ot-1997-12-23-030000.txt"
T=datetime.strptime (Str,format)
In turn: strftime (format)
26. Paths and Files
Os.path glob path processing, very rich glob is mainly equivalent to the LS command
27. subprocess Package for executing external commands and programs
Features similar to shell
Call Check_call Check_output
Repen PIPE. Popen.wait popen.communicate
Python functionality can be greatly expanded by invoking external applications and outputting the application results to Python and allowing Python to continue processing
28. Multithreading
Threading Thread
29. Multi-process
Multiprocessing Process
Process Pool
30. Math Package
Math Random
31. Socket HTTP
A simple HTTP server side is easy to implement, search Vamei blog, the example is very clear
32. Skills
1) Skip the test statement with __name__,__name__= ' __main__ '
2) Python-i test.py after running into commiseration line
33. With...as ... Example
F=open ("New.txt", "W")
F.closed ()
F.close ()
Equivalent to:
With open ("New.txt", "W") as F:
F.closed () #返回是否关闭
#出了with ... I'll turn off F.
34. Object Properties __dict__
bird.__dict__ #查bird的属性对
35. Python supports generating immediate attributes
Class Chicken (bird)
Fly=false
def __init__ (self,age):
Self.age=age
def getadult (self):
If Self.age>1.0:return True
Else:return False
Adult=property (Getadult)
It's OK:
@ Property
def getadult (self):
.....
36, function adorner note, function adorner can also take parameters
def Dec (F):
Def NF (A, B):
Print ("Input", A, B)
Return F (A, B)
Return NF
@dec
Def ADDC (A, B):
Return a+b
37, there are decorative categories, and decorative functions similar to the application
Python Learning Notes