python学习笔记 Day2
1.鸡汤咕嘟咕嘟~
Be a new gentleman
外在美:
老师说买衣服要多去优衣库,简洁大方还提供办事的试衣间!!真棒!!
某些国产大牌,外国地摊货(饥渴穷死啥的)就算了吧。
内在美:
多去旅行,多看书
学好英语,保持正能量
换位思考,积极影响身边的人
多看电影
2.import一个py程序
auth.py 代码如下:
1 user = input("user:") 2 print(user)
import_auth.py 代码如下:
1 import auth
这样运行两个py程序都是一样的效果!
3.找出一个列表中的某个元素,并且把它修改成新的值。
1 name = [1,2,34,34,9,8,7,9,6,9,3,34,9,10] 2 3 count = name.count(9) 4 for i in range(count): 5 count = name.count(9) 6 print("there are %d in name" % count) 7 print(name) 8 name[name.index(9)] = 9999 9 print(name)

4.while loop类代码
count=0 while True: count = count + 1 if 60 > count > 50: continue print("%04d,-------"%count) if count == 100: print("||||||||||||") break
5.列表的操作
练习题:
#写一个列表,包含本组所有成员
#往中间插入两个临组成员名字
#取出第3-8的人列表
#删除第七个人
#把刚才加入的那2个其他组的人一次性删除
#把组长的名字加上组长备注
#要求隔一个人打印一个人
1 list2 = ["2-1","2-2","2-3","2-4","2-5","2-6","2-7","2-8"] 2 list3 = ["3-1","3-2"] 3 4 for i in list3: 5 6 list2.insert(4,i) 7 8 print(list2) 9 print(list2[2:8]) 10 11 print(list2[6]) 12 list2.remove(list2[6]) 13 print(list2) 14 15 del list2[4:6] 16 print (list2) 17 18 list2[list2.index("2-1")] = list2[list2.index("2-1")] + "组长" 19 print(list2) 20 21 print(list2[0::2])
列表的其他用法
1 name = "1,2,3" 2 name2 = name.split(',') 3 name3 = "|".join(name) 4 #print(name2) 5 print("|".join(name2)) 6 7 name = "lu shuai" 8 print(name[2:4]) 9 print(name.center(20,'-')) 10 11 print(name.find('s')) 12 13 age = input("your name:") 14 if age.isdigit(): 15 age = int(age) 16 17 name='lussys' 18 print(name.isalnum()) 19 print(name.endswith('ys')) 20 print(name.startswith('lu')) 21 print(name.upper().lower())
6.字典的练习
1 name = [1,2,3,4,5,6] 2 3 id_db = { 4 19990101:{ 5 'name':"lus", 6 'age':22, 7 'add':"Hebei" 8 }, 9 19990102:{ 10 'name':"luu", 11 'age':22, 12 'add':"Shanxi" 13 }, 14 19990103:{ 15 'name':"lul", 16 'age':22, 17 'add':"Beijing" 18 } 19 } 20 21 print(id_db) 22 23 print (id_db[19990102]) 24 id_db[19990102]['name'] = "newname" 25 print (id_db[19990102]) 26 id_db[19990102]['new'] = "newnew" 27 print (id_db[19990102]) 28 id_db[19990102].pop("add") 29 print (id_db[19990102]) 30 31 v = id_db.get(19990102) 32 #v = id_db[19990102] 33 print(v) 34 35 dic2 = { 36 'name':'test', 37 19990102: { 38 'name':"dic2", 39 }, 40 } 41 42 id_db.update(dic2) 43 print(id_db) 44 print(id_db.items()) 45 print(id_db.keys()) 46 print(id_db.values()) 47 #19990102 in id_db 判断这个key 是否在id_db中 48 print(id_db.setdefault(19990105,"hahaha")) 49 print(id_db) 50 51 for key in id_db: 52 print(key,id_db[key])
7.课堂总结
- 列表用法:
copy
reverse
pop
sort
extend
index
count
list[::2]
del - 运算符
% 取模 奇偶数判断
** 幂 a**b
// 商的整数部分 - 逻辑运算
and a > 1 and b > 1
or
not if a is not in list: print
is not type(a) is list - 二进制运算
<< 左移
>> 右移
8.购物小程序
#用户启动时先输入财产
#用户启动程序后打印商品列表
#允许用户选择购买商品
#允许用户不断的购买各种商品
#购买时检测余额是否足够,如果足够直接付款,否则打印余额不足
#允许用户主动退出程序,退出时打印已购商品列表
1 salary = input("Input your salary:") 2 if salary.isdigit(): 3 salary = int(salary) 4 else: 5 exit("the salary is not int type") 6 7 welcome_msg = "welcome to go shopping".center(50,'-') 8 print (welcome_msg) 9 10 exit_flag = False 11 product_list = [ 12 ('Iphone',5), 13 ('Mac Air',8), 14 ('Xiao Mi',9), 15 ('Coffee',4), 16 ('Bike',2), 17 ('Cloth',3),] 18 shop_car = [] 19 20 while exit_flag is not True: 21 print("produce list".center(50,'-')) 22 for item in enumerate(product_list): 23 index = item[0] 24 p_name = item[1][0] 25 p_price = item [1][1] 26 print(index,'.',p_name,p_price) 27 28 user_choice = input("[q=quit,c=check]what do you want to buy?") 29 if user_choice.isdigit(): 30 user_choice = int(user_choice) 31 if user_choice < len(product_list): 32 p_item = product_list[user_choice] 33 if p_item[1] <= salary: 34 shop_car.append(p_item) 35 salary -= p_item[1] 36 print("Added [%s] into shop car,you balance is \033[31;1m[%s]\033[31;0m" % 37 (p_item,salary)) 38 else: 39 print("Your balance is [%s],cannot afford thie..." % salary) 40 else: 41 if user_choice == 'q' or user_choice == 'quit': 42 print("purchased products as below".center(40,'*')) 43 for item in shop_car: 44 print(item) 45 print("END".center(40,'*')) 46 print("Your balance is [%s]" % salary) 47 print("Bye") 48 exit_flag = True 49 elif user_choice == 'c' or user_choice == 'check': 50 print("purchased products as below".center(40,'*')) 51 for item in shop_car: 52 print(item) 53 print("END".center(40,'*')) 54 print("Your balance is \033[41;1m[%s]\033[0m" % salary )
虚怀若谷,尽善尽美

浙公网安备 33010602011771号