常量定義規則:常量意義為不可做更改;常量定義名稱為全大寫;
如:MYSQL_CONNECTION= '192.168.1.1'
pyc:python生成的翻譯檔案,使計算機能夠識别python語言;
清單的文法和使用:
清單參數插入文法:name.insert(2,'minggou')
name[1] = "wangminglong" --->修改清單中對應字段的内容;
追加:name.append("alex")
删除:name.remove("minggou")
定義清單:name=["1","minghu",2,43,"minggou"]
步長:print(name[::2])每隔一位取值一次;
查詢清單:WHICH in name
if 9 in name:
num_of_ele = name.count(9)
position_of_ele = name.index(9)
name[position_of_ele] = 999
print("[%s] 9 is in name,postion:[%s]" % (num_of_ele,position_of_ele))
print(name)
for i in range(name.count(9)): #修改所有9
ele_index = name.index(9)
name[ele_index] = 9999999
print(name)
清單合并:
name.extend(name2)
清單反轉:
name.reverse()
清單排序:
name.sort() #字元串和數字結合排序,python3會報錯;單獨排不會報錯
輸入下标删除元素,預設删除最後一個:
name.pop()
拷貝清單,隻拷貝第一層,不拷貝第二層,第二層拷貝記憶體資料共享:
name.copy()
清單嵌套:
name = ["csd",12,"asvs",[2,3,56,7],"cdc",354]
檢視清單長度:
print("name",len(name))
隻讀清單:
r = (1,2,3,4,5) #不想被改動的清單,隻讀清單,元組;
字元串常用操作:
name = "qiu ye"
username = input("name:")
if username.strip() == 'qiuye': #移除空格;
print("welcome")
字元串分割:
names = "dwqd,ascd,frer"
name2 = names.split(",")
print(name2) #變為清單
print("|".join(name2)) #按| 進行分割輸出;
字元串索引:
name = "qiu ye"
print('' in name) #判斷字元串中是否有空格
print(name.capitalize()) #首個字母大寫
name.format() #字元串格式化
例:
msg = “hello,{name},it's the {age} last time..."
msg2 = msg.format(name='Minghu',age=333)
print(msg2)
msg2 = "hh{0},ddas{1}"
print(msg2.format("alex",33))
切片:同清單切片
name = "alex li"
print(name[2:4])
print(name.center(40,'-')) #40個字元居中列印,其他位元組用-代替;
print(name.find()) #指定字段查找并輸出;
判斷:
age = int(input("your age:"))
if age.isdigit(): #判斷是否為數字
age = int(age)
else:
print("invalid data type")
name.isalnum() #不能包含特殊字元
name.endswith() #判斷結束
name.startswith() #判斷開始
name.upper() #大寫
name.lower() #小寫
資料運算:
type("333") is str
type(a) is list
二進制表示:
256 128 64 32 16 8 4 2 1
0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1
計算機中能表示的最小機關,是一個二進制位;
計算機中能存儲的最小機關,是一個二進制位(bit)
8bit = byte(位元組)
1024byte = 1 kbyte
1204kbyte = 1mbyte
1024mb =1gb
運算符:
<<右移
>>左移
例:
64>>2 == 16
64<<1 == 128
python死循環:
例:
count = 0
while True:
print("this is true",count)
count +=1
字典:
id_db ={
1248357148965: { #key值必須是唯一的,不然隻輸出最後的一個;
'name' : "shanpao", #name:小字典的key,“shanpao”:key的value
'age' : "24"
'addr' : 'dongbei'
},
1248357448965: {
'name' : "shanpao",
'age' : "24"
'addr' : 'dongbei'
},
124835712345: {
'name' : "shanpao",
'age' : "24"
'addr' : 'dongbei'
},
}
#字典的基本格式,存儲資訊更靈活;
給key賦新值:
id_db[1248357448965]['name'] = "new value" #修改value;
id_db[1248357448965]['qq_of_wife'] = 123556675 #如果字典中沒有此value,則添加新值;
print(id_db.setdefault(234254364),"hhh")) #取一個預設值,如果不存在,則賦一個新的值;
print(dict.fromkeys([1,2,34,5,6,8],'ddd') ) #将清單中每個值拿出來當做場景使用 --->一個坑!
id_db.popitem() #随機删除字典中的一個key --->建議不使用任何随機性的語言;
高效率的循環:
for key in id_db:
print(key,id_db[key])
例:購物車
salary = input("Input your salary:")
if salary.isdigit():
salary = int(salary)
else:
exit("Invaild data type...")
welcome_msg = "Welcome to our Shopping mall".center(50,'-')
print(welcome_msg)
exit_flag =False
product_list = [
('iphone',5888),
('mac air',8000),
('mac pro',9000),
('xiaomi',199),
('coffee',30),
('bike',800),
('cloth',200),]
shop_car = [] #定義購物車
while exit_flag is not True:
#for product_item in product_list:
# p_name,p_price = product_item
print("product list".center(50,'-'))
for item in enumerate(product_list):
index = item[0]
p_name = item[1][0]
p_price = item[1][1]
print(index,'.',p_name,p_price)
user_choice = input("[q=quit,c=check]what do you want to buy")
if user_choice.isdigit(): #肯定是選擇商品
user_choice = int(user_choice)
if user_choice < len(product_list):
p_item = product_list[user_choice]
if p_item[1] <= salary: #工資夠用
shop_car.append(p_item) #放入購物車
salary -= p_item[1] #減錢
print("Added \033[41;1m[%s]\033[0m into shop car,your current balance is \033[41;1m[%s]\033[0m" %(p_item,salary))
else:
print("your balance is \033[41;1m[%s]\033[0m,can not afford this goods..." % salary)
else:
if user_choice == 'q' or user_choice == 'quit':
print("purchased products as below".center(40,'-'))
for item in shop_car:
print(item)
print("END".center(40,'-'))
print("your balance is \033[41;1m[%s]\033[0m" % salary)
exit_flag = True
elif user_choice == 'c' or user_choice == 'check':
print("purchased products as below".center(40,'-'))
for item in shop_car:
print(item)
print("END".center(40,'-'))
print("your balance is \033[41;1m[%s]\033[0m" % salary)