w2 16、第二周-第02章節-Python3.5-模塊初識 sys模塊 sys.path sys.argv os模塊 os.system os.popen os.mkdir 17、第二周-第03章節-Python3.5-模塊初識2 18、第二周-第04章節-Python3.5-pyc是什麼 19、 ...
w2_sys_os_list_tuple_dict
- 16、第二周-第02章節-Python3.5-模塊初識
- 17、第二周-第03章節-Python3.5-模塊初識2
- 18、第二周-第04章節-Python3.5-pyc是什麼
- 19、第二周-第05章節-Python3.5-python數據類型
- 20、第二周-第06章節-Python3.5-bytes數據類型
- 21、第二周-第07章節-Python3.5-列表的使用
- 22、第二周-第08章節-Python3.5-列表的使用2
- 23、第二周-第09章節-Python3.5-元組與購物車程式練習
- 24、第二周-第10章節-Python3.5-購物車程式練習實例
- 25、第二周-第11章節-Python3.5-字元串常用操作
16、第二周-第02章節-Python3.5-模塊初識
sys模塊
import sys
sys.path
print(sys.path)
列印環境變數
sys.argv
print(sys.argv)
返回列表:腳本相對路徑,[以及傳入參數]
print(sys.argv[1]) 表示列印傳入的第一個參數值,依次類推
os模塊
import os
os.system
result = os.system("ls")
print(result)
result可能的值為0或1,代表執行成功與否,不保存命令執行返回數據
os.popen
cmd_res = os.popen("dir").read()
cmd_res可以讀取到命令執行的返回數據
os.mkdir
os.mkdir("new_dir")
創建一個目錄
17、第二周-第03章節-Python3.5-模塊初識2
18、第二周-第04章節-Python3.5-pyc是什麼
檢查源文件與pyc的時間,如果源文件更新,則預編譯一次再執行
19、第二周-第05章節-Python3.5-python數據類型
int float complex(複數)
bool
20、第二周-第06章節-Python3.5-bytes數據類型
數據運算(略)
三元運算:
result = 值1 if 條件 else 值2
if 條件為真,則為值1
if 條件為假,則為值2
a,b,c = 1,3,5
d = a if a>b else c
print(d)
str與bytes
21、第二周-第07章節-Python3.5-列表的使用
列表與元組
列表
list_A = []
list_A.append("string")
list_A.insert(0,"string_A")
list_A[1] = "stringC"
list_A.remove("stringc")
del list_A[1]
list_A.pop()
list_A.pop(index)
list_A.clear() #清空列表
list_A.reverse() #順序反轉
list_A.sort() #排序
註:排序規則按accssic碼規則排列
元組
元組可以看作是只讀的列表。
只有count,index屬性
練習:購物車程式
程式:購物車程式
需求:
1.啟動程式後,讓用戶輸入工資,然後列印商品列表
2.允許用戶根據商品編號購買商品
3.用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒
4.可隨時退出,退出時,列印已購買商品和餘額
#!/usr/bin/env python
#-*-coding:utf-8-*-
#Author:wu.
products = ['手機','電腦','滑鼠','微波爐']
price = [1000,3000,50,300]
shop_cart = []
total = []
find_minimum_price = price.copy()
find_minimum_price.sort()
while True:
#提示用戶輸入帳戶中存入的錢的數額
temp_input = input("please input your balance:")
#判斷是否輸入的為數字,若是數字,則進入選購流程
if temp_input.isdigit():
balance= int(temp_input)
print("your balance :",balance)
break
#如果不為數學,則提示用戶輸入數字,返回輸入框
else:
print("Please input digit numbers!")
continue
while True:
#展示商品序號,商品名稱,商品價格
for index in range(len(products)):
product_info = '''
{index}.{product} {price}
'''.format(index=index+1,product=products[index],price=price[index])
print(product_info)
#用戶輸入菜單選項
user_input = input("please input product number or press q to quit:").strip()
#如果用戶輸入的選項為字母q,則列印用戶選購的商品列表,及所選商品總金額,然後退出
if user_input == "q":
print(shop_cart)
print(total)
print("選擇商品的總額為:",sum(total))
break
#如果用戶輸入的選項不是q,則判斷是否輸入的是數字
elif user_input.isdigit():
user_input = int(user_input)
#判斷用戶輸入的數字是否是菜單中的選項
if user_input in range(1,len(products)+1,1):
#判斷用戶的帳戶是否大於等於用戶所選購的商品價格,是就將商品放入購物車
if balance >= price[user_input-1]:
shop_cart.append(products[user_input - 1])
total.append(price[user_input-1])
print(shop_cart)
print(total)
balance = balance - price[user_input - 1]
print("your balance:" ,balance)
#判斷用戶的帳戶餘額是否小於商品中價格最少的數額,如果小於則退出程式
if balance < find_minimum_price[0]:
print("you are broken!")
break
#如果用戶選購商品的價格大於用戶帳戶,則列印用戶沒有足哆的錢,返回選購菜單
else:
print("you don't have enough money!")
continue
else:
print("Please make sure input right number!")
else:
print("Invalid input,please check!")
參考alex blog:http://www.cnblogs.com/alex3714/articles/5717620.html
22、第二周-第08章節-Python3.5-列表的使用2
list_A.extend(list_B) #將list_B的元素擴展到list_A
嘗試:列表的深copy,淺copy的區別
import copy
list_B = copy.deepcopy(list_A)
列表按步長取元素
print(list_A[0: -1:2]) #按步長為2,從0個元素取值到最後一個
print(list_A[::2]) #其中0和-1可以省略不寫,與上一種等價
23、第二周-第09章節-Python3.5-元組與購物車程式練習
24、第二周-第10章節-Python3.5-購物車程式練習實例
25、第二周-第11章節-Python3.5-字元串常用操作
字元串操作
strings = 'string'
strings = 'Alex li'
strings.capitalize()
print(strings.count("l"))
strings.encode()
print(strings.endswith(' li'))
strings = 'Alex \ li'
print(strings.expandtabs(tabsize=100))
strings.format()
strings = 'Alex name li'
print(strings[strings.find("name"):9])
print(strings[-1])
!# strings.format_map()
strings.isdigit()
print(strings.index('le'))
strings.isalnum() #包含所有的英文字元和阿拉伯數字
strings.isalpha() #包含純英文字元
strings.isdecimal() #16進位
strings.isidentifier() #是否合格的變數名
strings.islower()
strings.isupper()
strings.isnumeric()
strings.isprintable()
strings.isspace()
strings.istitle()
strings = "+"
list_s = ["a","b","c"]
print(strings.join(list_s))
print(strings.ljust(50,"*"))
!# strings.rjust()
strings = 'AAbbCC1ccbb'
print(strings.lower())
strings.upper()
strings.lstrip()
p = str.maketrans('abcdefg','1234567')
print("abcggg".translate(p))
en_string = "abcggg".translate(p)
d = str.maketrans('1234567','abcdefg')
print(en_string.translate(d))
print(strings.replace('bb','ZZ',1))
35:54