(已經存在的模板)html
標準庫:不須要安裝便可安裝 getpasspython
sys 模塊git
Sys.path #打印環境變量api
Sys.argv #打印相對路徑服務器
os 模塊app
os.system(」ls」) 調後直接使用輸出再屏幕,不保存屏幕,若是定義變量輸出,顯示0 iphone
import oside
cmd_res = os.popen("ls -a").read()this
print("--->",cmd_res)編碼
可輸出結果
調用腳本後,本地路徑生成 .pyc文件
:Python 再程序運行後,編譯的結果會位於內存的pycodeobject中,當Python程序運行結束,解釋器會將pycodeObject寫回》pyc文件中,當程序第二次運行時,程序會尋找此 pyc文件,若是找到直接載入
預編譯完的半成品
數字 {整數、長整數、浮點數、複數
浮點型 表示型號:小數
布爾值 :真或假 1或0
第三方庫:須要下載安裝才能使用
字符串經常使用操做:
name = "my name" print(name.capitalize()) #首字母大寫 print(name.count("i")) #計數 :i print(name.center(50,"-")) #name 放中間不夠的使用 - 補充 print(name.endswith("m")) #判斷name 字符串是否以此結尾 print(name[name.find("i"):3]) #字符串切片,取值 print(name.format(name = 'kim')) #輸出結果爲:my name is kim print(name.format_map({'name':'kim'})) #字典 print('ab12'.isalnum()) #判斷字符是否包含阿拉伯數字和字母,不包含特殊字符 print('ab12'.isalpha()) #判斷是否純字母 print('12'.isdigit()) #判斷是否爲整數 print('1A'.isdecimal()) #判斷是否爲十進制 print('_a1'.isidentifier()) #判斷是否是一個合法的標識符 print('aaa'.islower()) #判斷是否小寫 print('my'.isupper()) #判斷是否大寫 print('aaa22'.isnumeric()) #判斷是否是隻有數字 print(' '.isspace()) #判斷是否空格 print('my is'.istitle()) #判斷是否爲標題(首字母大寫) print('='.join(['kim','lnnac'])) #字符的形式鏈接後面列表 print(name.ljust(30,'*')) #第一個長度,字符不足長度以*代替,(後面接着補) print(name.rjust(30,'*')) #前面開始補 print('KIM'.lower()) #大寫變小寫 print('kim'.upper()) #小寫變大寫 print(' Kim\n'.strip()) #\n 換行,下一個輸入去掉換行+空格 print('kim'.replace('i','I')) #替換 print('abcdeb'.rfind('b')) #找到最右邊的字符 顯示下標 print('a+b+c+d+e'.split('+')) #根據括號內的分隔符顯示列表 print('a+b\n+c+d+e'.splitlines()) #根據換行來分 print('herbie KIM'.swapcase()) #大寫變爲小寫,小寫變爲大寫 print('herbie kim'.title()) #變爲標題,首字母大寫 print('herbie kim'.zfill(30)) #運算時不夠以0補充
a,b,c = 1,3,5
d = a if a <b else c
print(d)
d=1
https://jingyan.baidu.com/album/47a29f24292608c0142399cb.html?picindex=1
字符串 <—> 二進制 string <—> bytes
string 編碼(encode)bytes bytes 解碼(decode)bytes
語法:
people = {
'list1':"cang jingkong",
'list2':"Longze luola",
'list3':"xiaoze Maliya",
'list4':"sunwukong",
}
取值查找 :print(people[「list1」]) #但字典中無此key 索引就會報錯
取值查找: print (people.get(‘list2’)) #字典中無此key返回 None
print (people.has_key("list1")) #查找keylist1是否存在,在返回true不在返回false .in py2.* print('list1' in people) .in py3.*
特色1:字典打印是無序的
增長:people[「list5」] = 「kakaluote」
修改: people[「list2」] = 「wutenglan」
刪除1:del people[「list2」] #內置刪除方法, 不止可刪除字典
刪除2:people.pop(「list2」)
刪除3:people.popitem() 隨機刪除
People.values() 打印全部的值
people.keys() 打印key 的值
多級子彈嵌套及操做
av_catalog = { "歐美":{ "www.youporn.com": ["不少免費的,世界最大的","質量通常"], "www.pornhub.com": ["不少免費的,也很大","質量比yourporn高點"], "letmedothistoyou.com": ["可能是自拍,高質量圖片不少","資源很少,更新慢"], "x-art.com":["質量很高,真的很高","所有收費,屌比請繞過"] }, "日韓":{ "tokyo-hot":["質量怎樣不清楚,我的已經不喜歡日韓範了","據說是收費的"] }, "大陸":{ "1024":["所有免費,真好,好人一輩子平安","服務器在國外,慢"] } }
(來源:http://www.cnblogs.com/alex3714/articles/5717620.html)
setdefault : 在字典中查詢如沒有則增長,若有key 則返回值
av_catalog.setdefault("taiwan",{"baidu.com":[1,2]})
updata :字典infoa、字典b;
infoa.updata(b)
print (infoa) #合併,如B有重複A的key 則更新,沒有的新增
A.fromkeys([])
print (infoa.items()) #將字典轉換爲列表
三級菜單實例(low版)
data = { '我':{ '佳佳':{ '口紅':{ 'diao':{}, 'ysy':{}, 'yeye':{} }, '小吃':{ '豬蹄':{}, '鴨脖':{}, '臭豆腐':{}, }, '大盤雞':{ '小份':{}, }, }, '算算':{ '沙河':{ '老男孩':{}, '北航':{}, }, '天通苑':{ '孫悟空':{}, }, }, }, '你':{ '利羣':{ "陽光":{ '硬陽光':{} } }, '中華':{ '軟中華':{ '硬中華':{} } }, }, } exit_flag = False while not exit_flag: for i in data: print(i) choice = input(">>選擇進入第一層:") if choice in data: while not exit_flag: for i2 in data[choice]: print("\t",i2) choice2 = input(">>選擇進入第二層:") if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print("\t\t", i3) choice3 = input(">>選擇進入第三層:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print("\t\t",i4) choice4 = input("最後一次選擇,按b返回:") if choice4 == "b": pass elif choice4 =="q": exit_flag = True if choice3 == "b": break elif choice3 =="q": exit_flag = True if choice2 =="b": break elif choice2 =="q": exit_flag = True
列表 [] name=[] 中括號代替
好比多人存儲到name中:name = ["zhangtang","guyun","hha"] 格式
name = ["zhangsan","lisi","jingkong","maliya","wukong"]
print (name[1:4]) #切片,顧頭不顧尾
print(name[-1])#當不知道列表數時取最後的值
print (name[-2:]) #從後取切片,取最後兩位數 0 能夠忽略掉
name.append("kim") #將kim插入變量後面
name.insert(1,"hei") #想插入第幾個位置就填寫位置。計算機內是從0開始計數
name[2] = "xiedi" #覆蓋,將jingkong改成xiedi
copy :淺複製;deepcopy:深複製
copy: 根據內存存儲地址來記錄,一個元素一個地址.被複製出的列表引用第一個列表的元素
deepcopy:完整克隆
import copy
name = ["zhangsan","lisi","jingkong",["xiaojin","xixu"],"maliya","wukong"]
name2 = copy.deepcopy(name)
name[3][0] = "MALIYA"
name[1] = "li"
print (name)
print (name2)
購物車實例:
#Herbie Kim #!/usr/bin/python # -*-coding:utf-8-*- shopping = [ ('iphone',6000), ('Mac pro',12000), ('watch',800), ('book',80), ('cat',100000) ] shopping_list = [] saleable = input("請輸入你的工資:") if saleable.isdigit(): saleable = int(saleable) #判斷輸入是否爲數字 while True: for index,item in enumerate(shopping): #打印商品列表,獲取下標 print(index,item) user_choice = input("選擇要買的商品:") if user_choice.isdigit(): #判斷輸入的必須爲數字 user_choice = int(user_choice) if user_choice < len(shopping) and user_choice >= 0: #判斷輸入的數字是否和列表長度同樣 p_item = shopping[user_choice] #經過下標取出商品 if p_item[1] <= saleable: #比較商品與工資,表明買的起 shopping_list.append(p_item) #添加到商品列表變量中 saleable -= p_item[1] #工資扣除商品 print("Added %s into shopping cart,你的餘額是 \033[31;1m%s\033[0m" %(p_item,saleable)) else: print("\033[41;1m你的餘額只剩[%s]啦,不能買了\033[0m" % saleable) else: print("商品列表[%s]不存在"% user_choice) elif user_choice =='q': print("--------shoping list------") for p in shopping_list: print(p) print("你的餘額:",saleable) exit() else: print("輸入有誤,請輸入數字")
集合是一個無序的,不重複的數據組合
一、去重
#Herbie Kim #!/usr/bin/python # -*-coding:utf-8-*- list_1 = [1,2,3,3,2,6,7] list_1 = set (list_1) #集合,去重 list_2 = set ([0,2,6,4,22]) print(list_1,list_2,type(list_2))
二、關係運算
#Herbie Kim #!/usr/bin/python # -*-coding:utf-8-*- list_1 = [1,2,3,3,2,6,7] list_1 = set (list_1) #集合,去重 list_2 = set ([0,2,6,4,22]) print(list_1,list_2,type(list_2)) ''' print(list_1.intersection(list_2)) #交集 print(list_1.union(list_2)) #並集,將兩個合集合並,去除重複 print(list_1.difference(list_2)) #差集,我這有你那沒有的,例子是list1有,list2沒有 list_3 = set ([2,3,3]) print(list_3.issubset(list_1)) #子集判斷,list3是list1的子集 print(list_1.issuperset(list_3)) #父集判斷,list_3是否被包含於list_1,list1是list3的父集 print(list_1.symmetric_difference(list_2)) #對稱差集,將兩個列表中都有的項去除 print("------------") print(list_2.isdisjoint(list_1)) #判斷兩個集合是否有交集 ''' #簡單化 print(list_1 & list_2) #交集 print(list_1 | list_2) #並集 print(list_1 - list_2) #差集,in list_1 not in list_2 print(list_1 ^ list_2) #對稱差集 。
集合中添加、刪除
#list_1.add(99) #添加一項數據 list_1.update([10,11]) #添加多項 print(list_1) list_1.remove(11) #刪除一項 print(list_1)
讀文件:
#Herbie Kim #!/usr/bin/python # -*-coding:utf-8-*- f = open("today2",'w',encoding="utf-8") #文件句柄 data = f.read() #讀完了後光標停留在文本最後 print(data)
寫文件:
W 模式以寫的狀態來打開一個文件,建立一個文件(會覆蓋以前文件)
f = open("today2",'w',encoding="utf-8") #文件句柄
f.write("這是剛纔寫入的文檔")
f.write("這是剛纔寫入的文檔,\n") #換行
f.write("我須要換行寫")
a 追加
f = open("today2",'a',encoding="utf-8")
打印前五行,小方法
f = open("today2",'r',encoding="utf-8") for i in range(5): print(f.readline())
循環讀 文件(小文件)
f = open("today2",'r',encoding="utf-8") for index,line in enumerate(f.readlines()): #循環讀一遍,取出每一個元素下標 ,注意可讀小文件 if index == 9: print('-----------分割線--------') continue print(line.strip()) #去空格,換行
大文件讀
f = open("today2",'r',encoding="utf-8") ''' for index,line in enumerate(f.readlines()): #循環讀一遍,取出每一個元素下標 ,注意可讀小文件 if index == 9: print('-----------分割線--------') continue print(line.strip()) #去空格,換行 #for i in range(5): # print(f.readline()) ''' count = 0 for line in f: #內存一行行讀 if count == 9: print('-----我是分割線----') count += 1 continue print(line) count += 1
光標移動讀
f = open("today2",'r',encoding="utf-8") print(f.tell()) #記錄光標所讀 print(f.readline()) print(f.tell()) f.seek(0) #光標返回到0位置從新讀 print(f.readline())
f.flush() # 以write 寫時,實時刷新數據在內存上
進度條以下