標籤:encoding str bin dir 城市 順序 pytho lan computer
print(sys.path) #列印環境變數
print(sys.argv) #列印相對路徑
print(sys.argv[1]) #列印對應的參數
1.在python最上有時候會匯入os模組,表示與系統有互動的時候,都用os模組
例如:
cmd_res=os.system(“dir”) #顯示當前路徑下的目錄
print(cmd_res) #結果為0,表示該命令執行正確
os.mkdir(“new_dir”) #表示在當前路徑下建立一個new_dir目錄
2.資料類型:
int:整數型,在python中沒有長整型,type(2**32)
float:浮點型,3.24,5.31E4,E表示10**4
布爾類型:用1和0表示 d=a if a>b else c
3.進位之間的轉換
二進位和十六進位的轉換
文本都是Unicode,二進位都是bytes(音頻、視頻都是二進位)
在python3中二進位(bytes)和字串是不可以拼接的
二進位-------->字串需要decode(解碼)
字串--------->二進位需要encode(編碼)
例如:
在python3中輸入如下代碼
msg="我愛北京"
print(msg.encode(encoding="utf-8"))#下面為編譯結果
E:\python3.5.2\python3.exe E:/workspace/s14/day1/codeing.py
b‘\xe6\x88\x91\xe7\x88\xb1\xe5\x8c\x97\xe4\xba\xac‘
上述就表示為將字串變為二進位代碼
msg="我愛北京"
print(msg.encode(encoding="utf-8").decode(encoding="utf-8"))#再將二進位轉換為字串
4.關於切片的模組含義
names=["[email protected]北京","X上海",["武漢","河南"],"x廣州","6天津"]
names.append("陝西")#在列表最後增加陝西
names.insert(4,"西安")#在列表對應位置4插入西安
print(names[0],names[2])#取0和2位置的對應值
print(name[1:3])#取1位置到3位置之前的所有值,不包括3
print(name[-1])#取最後一個值
print(name[-2:])#取導數第二個值到最後的值
names[2]=”河南”#修改位置2的值為河南
names.remove(“西安”)#去除西安
del names[1]#刪除位元置1的值
print(names.index(“武漢”))#列印武漢對應的位置
print(names(names.index(“武漢”)))#取出對應位置的城市
print(names.count(“西安”))#統計有幾個西安
names.clear()#清空列表
names.reverse()#反轉列表順序
names.sort()#排序,特殊字元>數字>大寫字母>小寫字母
names2=[1,2,3,4]#定義一個新列表
names.extend(names2)#擴充、合并
names2=names.copy()#淺copy
names2=copy.deepcopy(names)#深copy
跳著切print(names[:-1:2])#表示不取最後一個值,每隔一個取一個值
元組唯讀列表names=(“”,””,””,””)只有count和index
購物車作業
要求:1啟動程式後,輸入工資,然後列印商品列表
2 允許使用者根據商品編號列印商品列表
3.使用者選擇商品,檢測餘額是否足夠,如果夠直接買下商品,如果不夠,退出程式
4.可以隨時退出,退出時候,列印已經購買的商品餘額
#! /user/bin/python3
# -*- coding:utf-8 -*-
product_list=[("red bine",88),("apples",10),("table",25),("bike",325),("computer",4555)]
shopping_list=[""]
salary=input("請輸入你的工資:")
if salary.isdigit():
salary=int(salary)
while True:
for index,item in enumerate(product_list):
print(index,item)
user_choice=input("請輸入你要買什嗎?")
if user_choice.isdigit():
user_choice=int(user_choice)
if user_choice>=0 and user_choice<len(product_list):
p_item=product_list[user_choice]
if salary>=p_item[1]:
shopping_list.append(p_item)
salary=salary-p_item[1]
print("added %s into your shopping cart and your balance is \033[40;1m%s\033[0m"%(p_item,salary))
else:
for p in shopping_list:
print(p)
print("\033[21;1myour balance is %s\033[0m"%salary)
exit()
else:
print("these have not product")
elif user_choice=="q":
print("exit....")
else:
print("invailed option")
python資料類型以及模組的含義