1. 弱類型:不須要爲變量指定類型。(C語言是強類型,必須給變量指定類型。如:int,string等。強類型的好處:方便作語法檢查) html
1 a = 1234 2 print(a) 3 a = ‘abcd’ 4 print(a)
#outcomes:python
1234 abcd
2. 變量必須先賦值。Python中變量要先賦值再使用。而C語言中,定義一個變量(int i),i是有一個初始值。app
3. Python是經過引用傳遞變量的。引用變量的地址空間。ide
4. Python2 和Python3的常見差別函數
1. none是True和False以外的一種狀態。能夠等價爲false。this
2. del是從容器裏刪除對象。spa
3. python中只有三種操做:與、或、非。沒有&&,||等。debug
4. 函數是沒有返回值的,要返回值,需加return3d
默認參數:調試
1 def hello(who = 'world'): 2 print('hello %s!' %(who)) 3 4 hello() 5 hello('sea')
#Outcome:
hello world! hello sea!
Lambda VS Normal defunction:
1 def g(x): 2 return x*5 3 def f(gf,x): # gf has got the memory address of g(x), no matter what it is called. 4 return gf(x)+100 # gf(x) call g(x) and pass the argument x to g(x). 5 print(f(g,100)) # g -> pass the memory address of g(x) to the argument. 6 print(f(lambda x:x*110,100)) 7 # process: 1. lambda generate a memory address for itself (x*110) 8 #2. call f(g,100) and pass the memory address of lambda to the argument gf. 9 #3. gf(x) call lambda x*110 and pass 100 to x, so the result of lambda equals 11100
#Outcome:
600 11100
1 def f(gf,x,y): 2 return gf(x,y)+100 3 print(f(lambda x,y:x*y,100,200))
#Outcome:
20100
5. while & for
1 totoal = 0 2 i = 1 3 while i <= 100: 4 total += i 5 i += 1 #沒有++i或者--i 6 print(total)
#outcome:
5050
for循環只做用用於容器!!
沒有這種寫法:
for (i = 0; i<100; ++i)
pass
上面這種循環只能用while實現。
1 i = 0 2 while i < 3: 3 j = 0 4 while j <=3: 5 if j == 2: 6 break / continue #只退出當前循環,循環繼續執行j=3.。。 7 print(i,j) 8 j += 1 9 i +=1
#outcomes:
Break: 0 0 0 1 1 0 1 1 2 0 2 1
Continue: 0 0 0 1
1. List
用lambda對第一元素作排序
切片
2. String
Mode
‘r’ – Read mode which is used when the file is only being read ‘w’ – Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated)
'b' - appended to the mode ('rb','rb+','wb','wb+') opens the file in binary mode ‘a’ – Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end ‘r+’ – Special read and write mode, which is used to handle both actions when working with a file
'b'
Open a file
>>> f = open('workfile', 'w')
Open a file with 'with'
>>> with open('workfile') as f: #If you’re not using the keyword, then you should call to close the file and immediately free up any system resources used by it. ... read_data = f.read() >>> f.closed #If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while.
#with as equals:
try:
f = open('workfile','r')
for line in f.readlines():
print(line)
except:
...
finnally:
f.close()withf.close()
Read a file:
>>> f.read() #Read a whole file. #Outcome: 'This is the entire file.\n' >>> f.readline() #Read a line of the file #Outcome: 'This is the first line of the file.\n' >>> f.readline() #Outcome: 'Second line of the file\n' >>> for line in f: #Read a file line-by-line ... print(line, end='') #outcomes: This is the first line of the file. Second line of the file
Write a file:
f.write('This is a test\n')
#錯誤處理
1 import logging #在生產環境中,最有效的調試方式。 2 3 ''' 4 做業,本身實現將不一樣的等級的信息寫到不一樣日誌文件。 5 logging.info() 6 logging.debug() 7 ''' 8 9 try: 10 r=10/0 11 except ZeroDivisionError as e: #捕捉異常 12 print(type(e)) 13 print(e) 14 finally: #主要是防止服務端資源泄漏! 15 print('always come here.')