一、py列表
1、python -V 查看本機py版本。ps:V是大寫的
2、直接輸入python 則進入到解析器,退出 :quit()
3、py列表 類似于數組,但是又有區別
4、列表更加強大的功能 單一列表
>>> cast = ['cleese','palin','jones','idle']
>>> print cast//輸出列表中所有內容
['cleese', 'palin', 'jones', 'idle']
>>> print len(cast)//輸出列表長度
4
>>> print cast[5]
Traceback (most recent call last):
File "", line 1, in
print cast[5]
IndexError: list index out of range//cast中的參數為5,實際長度為4,所以提示out of range
>>> print cast[3]//輸出cast中的第三個元素的值
idle
5、python列表增加字段 append
>>> cast.append('dubailing')
>>> print cast
['cleese', 'palin', 'jones', 'idle', 'dubailing']
6、python列表中刪除字段
>>> cast.pop('jones')
Traceback (most recent call last):
File "", line 1, in
cast.pop('jones')
TypeError: an integer is required//參數錯誤,不能指定刪除某個字段
>>> cast.pop()
'dubailing'
>>> print cast
['cleese', 'palin', 'jones','idle']
7、將另一個表中的字段加到某表中
>>> cast.extend(['haha','nihao'])
>>> print cast
['cleese', 'palin', 'jones', 'haha', 'nihao']
8、python刪除表中指定項
>>> cast.remove('palin')
>>> print cast
['cleese', 'jones', 'haha', 'nihao']
>>>
9、在某個指定位置插入某個字段//在列表元素位置為1的地方插入test字段,輸出后,發現第二位為test,想插入到什么位置,只需修改元素位置號即可
>>> cast.insert(1,'test')
>>> print cast
['cleese', 'test', 'jones', 'haha', 'nihao']
10、python列表中可以存儲任意類型的數據
11、python for 循環:
name = ['we','ssd','gg']
for i in name:
print i
//若想輸出每個值,則可以使用 print name[0] 這種方法,but 若列表中有多個值,使用此方法就不美麗了,最好的就是使用for循環
12、>>> print cast
['cleese', '1975', 'test', '1979', 'jones', '1983', 'haha', 'nihao']
>>> while coutn < len(cast)://使用while循環 方式輸出cast列表內容、但是處理迭代問題,使用for循環是最好的,while循環需要提供額外的控制
print cast[coutn]
coutn +=1
cleese
1975
test
1979
jones
1983
haha
nihao
13、python 對大小寫是敏感滴哦,所以在變成過程中一定要注意
14、列表中嵌套列表
>>> movies = ['the holy grail',1975,'terry jones &terry gilliam',91,['graham chapman',['michel palin','john','terry','eric','jones']]]
>>> print movies
['the holy grail', 1975, 'terry jones &terry gilliam', 91, ['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]]
>>> for i in movies:
print i
the holy grail
1975
terry jones &terry gilliam
91
['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]
//如何將列表中的列表讀取出來
使用到if....else語句,但是如何判斷里面還有沒有內嵌列表呢?那就要使用到python的一個內置函數
15、isinstance(要判斷的對象,對比類型)? python的一個內置方法。(dir(__builtins__))
>>> print movies
['the holy grail', 1975, 'terry jones &terry gilliam', 91, ['graham chapman', ['michel palin', 'john', 'terry', 'eric', 'jones']]]
>>> isinstance(movies,list)//因為movies是一個列表所以返回結果為true
True
>>> isinstance(movies,int)因為movies是一個列表而對比類型為int,兩者不一致,所以結果為false
False
>>>
代碼如下:
>>> for eachname in movies:
if isinstance(eachname,list):
for nexteach in eachname:
if isinstance(nexteach,list):
for each in nexteach:
print each
else:
print nexteach
else:
print eachname
the holy grail
1975
terry jones &terry gilliam
91
graham chapman
michel palin
john
terry
eric
jones
//一層層的判斷,實際處理過程是先處理內列表,在處理外列表,由內而外進行處理,但是這樣子就引申出一個問題,那就是重復代碼,如何解決呢?函數來幫忙
16、定義函數
>>> def print_lol(the_list):
for eachname in the_list:
if isinstance(eachname,list):
print_lol(eachname)
else:
print eachname
>>> print_lol(movies)
the holy grail
1975
terry jones &terry gilliam
91
graham chapman
michel palin
john
terry
eric
jones
//使用函數完美的解決了代碼重復問題
二、函數模塊
函數模塊,則可以理解為一個含有.py的文件,模塊中要含有注釋代碼,先介紹這是個什么模塊,包含什么函數,其作用是什么,其次在標注這個函數的作用
函數模塊的使用,可以通過import 方式導入,一般導入文件代碼放在上方
在py編程過過程中可以將代碼放置一行,并通過分隔符進行分割,但是這樣子不利于閱讀,建議每行只包含一行py代碼
關于注釋 可以使用三個成對的單引號或者雙引號進行注釋,當然也可使用#進行注釋
BIF內置函數:range,返回一個迭代器,根據需要生成一個指定范圍內的數字
>>> for num in range(4):
print num
0
1
2
3
三、文件異常處理
1、>>> import os//導入os模塊
>>> os.getcwd()//當前工作目錄是什么
'D:\\Python27'
>>> os.chdir('../headfistpython/chapter3')
>>> os.getcwd()
'D:\\headfistpython\\chapter3'
2、讀取文件:
>>> data = open('sketch.txt')
>>> print data.readline()//讀取一行
hello
>>> print data.read()//讀取整個文件
haha
ahaha
>>> data.close()//關閉文檔
文件處理-》打開文件-》讀取文件內容-》處理后關閉文件
3、split(sep,[maxsplit])
4、find 通過find的返回值 判斷
5、異常處理
對于會出現異常的情況,放入try/cath模塊中-
其次增加更多的異常代碼的處理
下面是對 一個解析的文件是否存在進行處理
>>> import os
>>> if os.path.exists('du.txt')://判斷文件是否存在,如果存在則繼續執行,不存在則提示文件丟失
data = open('du.txt')
for each in data:
if not each.find(':') == -1:
(role,num)= each.split(':',1)
print role
print num
print(role,num)
data.close()
else:
print ('The data file is missing')
man
is? this the tight room foe an atgumen?
('man', ' is? this the tight room foe an atgumen?\n')
other man
I've told you once.
('other man', " I've told you once.\n")
man
No you haven't!
('man', " No you haven't!\n")
other? man
Yes? I? have.
('other? man ', 'Yes? I? have.\n')
man
When?
('man', '? When?\n')
other man
now let's get one thing quites clear:i most definitely told you!
('other man', " now let's get one thing quites clear:i most definitely told you!\n")
man
no you didn't!
('man', " no you didn't!")
//修改打開文件名稱
則提示
The data file is missing
使用try/catch
>>> try:
data = open('du.txt')
for each in data :
try:
(role,num)= each.split(':',1)
print role
print '++++++++++++++++++++++++++'
print num
print '++++++++++++++++++++++++'
print (role,num)
except:
pass
except:
print 'the is not exists'
the is not exists
>>>
//無論使用if else 還是try catch? 均能達到處理異常的目的,但是使用try的成本低
strip()//剔除字符串中不想要的空白符
num = num.stript()