標籤:讀書筆記python
python編程是一件愉快的事情!!!
一、python的特點:
它注重的是如何解決問題而不是程式設計語言的文法和結構。
當你用Python語言編寫程式的時候,你無需考慮諸如如何管理你的程式使用的記憶體一類
的底層細節。
可移植性
支援物件導向和面向過程的編程
可嵌入性:可以在python代碼中嵌入c或者c++
豐富的庫
二、python代碼執行的過程:
原始碼.py-> 位元組碼.pyc ->位元組碼在PVM(Python虛擬機器)中執行
二、python IDE:
http://www.pydev.org/
三、python的相關基本概念:
by \n"。
記住,在你處理文字檔的時候使用Unicode字串,特別是當你知道這個檔案含有用
非英語的語言寫的文本。
‘your name?‘會被自動轉為"What‘s your name?"。
寫成‘\\1‘或r‘\1‘。
什麼是後向引用backreference:\n斜杠加數字表示前面括弧的內容。例如:\b(\d+)\b\s+\1\b這樣這個會匹配如:123 123重複的數字串
元字元
名稱
匹配對象
.
點號(dot)
單個任一字元
\n
換行(newline)
換行
\r
斷行符號(return)
斷行符號
\t
定位字元(Tab)
定位字元
[...]
字元組(Character Class)
列出的任一字元
[^...]
排除型字元組(Negated Character Class)
未列出的任一字元
\d
數字(digit)
數字
\D
非數字
\w
單詞(word)
單詞字元(字母或者數字)
\W
非單詞字元
\s
空白字元(whitespace)
空白字元
\S
非空白字元
^
脫字元(caret)
行的起始位置
$
美元符(dollar)
行的結束位置
\<
單詞的起始位置
\>
單詞的結束位置
\b
邊界(Boundary)
單詞邊界
\B
非單詞邊界
(?=...)
肯定順序環視(Positive Lookahead)
成功如果右邊能夠匹配
(?!...)
否定順序環視(Negative Lookahead)
成功如果右邊不能夠匹配
(?<=...)
肯定逆序環視(Positive Lookbehind)
成功如果左邊能夠匹配
(?<!...)
否定逆序環視(Negative Lookbehind)
成功如果左邊不能夠匹配
?
它之前的字元可以不出現,也可以只出現一次
+
它之前的字元可以出現無數次,但至少要出現一次
*
它之前的字元可以出現無數次,也可以不出現
{n}
它之前的字元精確匹配n次
{n,}
它之前的字元可以出現無數次,但至少要出現n次
{n,m}
它之前的字元至少要出現n次,至多出現m次
(...)
圓括弧(parenthese)
分組子運算式和記錄它包含的字表達匹配的內容
(?:...)
分組子運算式
\1 - \9
後向引用
|
豎線(bar)
匹配分割兩邊的任意一個運算式
第一個字元必須是字母表中的字母(大寫或小寫)或者一個底線;
其他部分可以由字母(大寫或小寫)、底線(‘ _ ’)或數字(0-9)組
成;
大小寫敏感;
想要在一個物理行中使用多於一個邏輯行,那麼你需要使用分號(;)來特別地標明這
種用法。
i = 5
print i
可以寫成
i = 5;
print i;
也可以寫成
i = 5; print i;
四、python格式注意:
Python是大小寫敏感的
每一行的開始字元前沒有空格或者定位字元
五、合理使用協助資訊:
help(str)——這會顯示str類的幫
助
help你需要經常用
六、python控制流程:
If/while/for/break/continue
#!/usr/bin/python
a=12
b=int(raw_input("pls input a int:"))
if b==a:
print "congratudations"
elif b<a:
print "your guess lower than it"
else:
print "done"
#!/usr/bin/python
a=20
flag=True
#這個地方需要注意首字母大寫
while flag:
guess=int(raw_input("pls input a int:"))
if guess==a:
print "good"
flag=False
elif guess<a:
print "should bigger"
else:
print "should lower"
print "done"
#!/usr/bin/python
# Filename: for.py
for i in range(1, 5):
print i
else:
print ‘The for loop is over‘
else部分是可選的。如果包含else,它總是在for迴圈結束後執行一次。
#!/usr/bin/python
b="abc"
while True:
a=raw_input("pls input:")
if a==b:
break
else:
print "the length is",len(a)
print "done"
七、python模組:
標準庫模組:sys模組
#!/usr/bin/python
# Filename: using_sys.py
import sys
print ‘The command line arguments are:‘
for i in sys.argv:
print i
print ‘\n\nThe PYTHONPATH is‘, sys.path, ‘\n‘
-
位元組編譯的檔案 :這些檔案以.pyc作為副檔名
-
From import語句:
-
模組的__name__:
假如我們只想在程式本身被使用的時候運行主塊,而在它被別的模組輸入的時候不運行主塊。每個Python模組都有它的__name__,如果它是‘__main__‘,這說明這個模組被使用者單獨運行。
記住這個模組應該被放置在我們輸入它的程式的同一個目錄中,或者在sys.path所列目錄之
一。
#!/usr/bin/env python
def say():
if __name__==‘__main__‘:
print ‘this program is being run by itself‘
else:
print ‘this module is being imported from another module‘
version=‘0.1‘
#!/usr/lib/env python
import name #調用name模組
name.say()
print ‘version‘,name.version
#!/usr/bin/python
# Filename: mymodule_demo2.py
from mymodule import say, version
# Alternative:
say()
print ‘Version‘, version
可以使用內建的dir函數來列出模組定義的標識符。標識符有函數、類和變數。
例如上面定義的模組name.py:
>>> import name
>>> dir(name)
[‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘say‘, ‘version‘]
注意,如果dir()參數為空白:
>>> import name
>>> dir(name)
[‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘say‘, ‘version‘]
>>> dir()
[‘__builtins__‘, ‘__doc__‘, ‘__name__‘, ‘__package__‘, ‘name‘]
#這裡import模組名稱是列表的一部分
八、 python標準庫:
一組模組組成了標準庫。
九、python異常
十、python資料結構
三種內建的資料結構:數組、字典、元組
列表是 可變的 資料類型。
例子:手動的給列表添加item和刪除:
#!/usr/bin/env python
def buy():
mylist=[‘potato‘,‘chilli‘,‘banana‘,‘apple‘]
print ‘i have‘,len(mylist),‘to buy\n‘
print ‘the items are:‘
for i in mylist:
print I,
flag=True
while flag:
judge=raw_input(‘you can input yes or no:‘)
if judge==‘yes‘:
goods=raw_input(‘maybe you need to buy more goods:‘)
mylist.append(goods)
else:
flag=False
for i in mylist:
print I,
print ‘my shop list is now:‘,mylist
mylist.sort()
print ‘the sorted shop list are‘,mylist
flag2=True
while flag2:
judge2=raw_input(‘if you want to delte?yes or no:‘)
if judge2==‘yes‘:
print ‘which itme you want to delte ‘,mylist
print ‘you can choose the item from 0 to‘,len(mylist)-1
a=int(raw_input(‘the item is:‘))
del mylist[a]
else:
flag2=False
print ‘the remained shop list are:‘,mylist
buy()
在print語句的結尾使用了一個 逗號 來消除每個print語句自動列印的分行符號。
元組和字串一樣是 不可變的 即你不能修改元組。元組通過圓括弧中用逗號分割的項目定義。
一個空的元組由一對空的圓括弧組成,如myempty = ()。
你必須在第一個(唯一一個)項目後跟一個逗號,這樣Python才能區分元組和運算式中一個帶圓括弧的對象。
元組最通常的用法是用在列印語句中。
元組的使用舉例:
!/usr/lib/env python
def zoo():
zoo=(‘lion‘,‘tiger‘,‘rabbit‘,‘dragon‘,‘snake‘)
print ‘the number of the zoo is‘,len(zoo)
for i in range(len(zoo)):
print zoo[i],
zoo_new=(‘dog‘,‘monkey‘,zoo)
print ‘the animal number is ‘,len(zoo_new)
print ‘the sencond animal is‘,zoo_new[1]
print ‘the animals brought from old zoo are:‘,zoo_new[2]
print ‘all animals:‘,zoo_new
a=(123)
print a
a=(123,)
print a
zoo()
輸入結果:
the number of the zoo is 5
lion tiger rabbit dragon snake the animal number is 3
the sencond animal is monkey
the animals brought from old zoo are: (‘lion‘, ‘tiger‘, ‘rabbit‘, ‘dragon‘, ‘snake‘)
all animals: (‘dog‘, ‘monkey‘, (‘lion‘, ‘tiger‘, ‘rabbit‘, ‘dragon‘, ‘snake‘))
123
(123,)
例子2:
元組用於列印中:
#!/usr/bin/python
# Filename: print_tuple.py
age = 22
name = ‘Swaroop‘
print ‘%s is %d years old‘ % (name, age)
print ‘Why is %s playing with that python?‘ % name
字典類似於你通過連絡人名字尋找地址和連絡人詳細情況的地址簿,即,我們把鍵(名字和值(詳細情況)聯絡在一起。注意,鍵必須是唯一的。
十一、python函數
舉例:
#!/usr/bin/python
# Filename: function1.py
def sayHello():
print ‘Hello World!‘ # block belonging to the function
sayHello() # call the function
參數在函數定義的圓括弧對內指定,用逗號分割。
#!/usr/bin/python
# Filename: func_global.py
def func():
global x
print ‘x is‘, x
x = 2
print ‘Changed local x to‘, x
x = 50
func()
print ‘Value of x is‘, x
#!/usr/bin/python
# Filename: func_default.py
def say(message, times = 1):
print message * times
say(‘Hello‘)
say(‘World‘, 5)
-
關鍵參數:通過命名來為這些參數賦值。
-
return語句:
#!/usr/bin/python
# Filename: func_return.py
def maximum(x, y):
if x > y:
return x
else:
return y
print maximum(2, 3)
#!/usr/bin/python
# Filename: func_doc.py
def printMax(x, y):
‘‘‘Prints the maximum of two numbers.
The two values must be integers.‘‘‘
x = int(x) # convert to integers, if possible
y = int(y)
if x > y:
print x, ‘is maximum‘
else:
print y, ‘is maximum‘
printMax(3, 5)
print printMax.__doc__
函數的第一個邏輯行的字串是這個函數的 文檔字串
文檔字串的慣例是一個多行字串,它的首行以大寫字母開始,句號結尾。第二行是空行,
從第三行開始是詳細的描述。
。請記住Python把 每一樣東西 都作為對象,包括這個函數。這句話非常重要,通過上面的執行個體的自後一行代碼可以提現。
十二、python變數
十三、python運算子與運算式:
650) this.width=650;" title="clip_image001" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" alt="clip_image001" src="http://s3.51cto.com/wyfs02/M00/8A/9D/wKiom1g1NtyxxI_hAAFcZOAj09I254.png" border="0" height="320" />
+加號可以做算術運算,也可以串連2個字串。
**冪
650) this.width=650;" title="clip_image002" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" alt="clip_image002" src="http://s3.51cto.com/wyfs02/M01/8A/9D/wKiom1g1Nt6i3URTAAHE1UhFxlg510.png" border="0" height="317" />
650) this.width=650;" title="clip_image003" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" alt="clip_image003" src="http://s3.51cto.com/wyfs02/M01/8A/99/wKioL1g1Nt_igqifAAGm2J3EvAw220.png" border="0" height="289" />
我建議你使用圓括弧來分組運算子和運算元,以便能夠明確地指出運算的先後順序。
650) this.width=650;" title="clip_image004" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" alt="clip_image004" src="http://s3.51cto.com/wyfs02/M02/8A/9D/wKiom1g1NuDCd2keAACsAvF37CM869.png" border="0" height="373" />
650) this.width=650;" title="clip_image005" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" alt="clip_image005" src="http://s3.51cto.com/wyfs02/M02/8A/99/wKioL1g1NuGAsBc2AAEIyLt-5z8373.png" border="0" height="430" />
本文出自 “Victor的奮鬥曆程” 部落格,請務必保留此出處http://victor2016.blog.51cto.com/6768693/1875786
簡明python教程的讀書筆記