Today is the second day of leaning Python.
1.For Loop
for i in range():
print()
2.break and continue
break : To jump out of this loop
continue : To jump out of the current loop
3.list []
a = [1,2,3,4,5] a[1:4] Everying from the first to the fourth,but not the fourth.
a[1:] from the first to the last
a[1: :2] This 2 represents the step size.(2,4) if it's -2 represents in the opposite direction
a.append('data') a.insert(index,'data')
a.remove('data') a.pop(index) If no input index ,it means that the last item is deleted.
a.count('data') a.extend(b) Add b to a a.index('data') Find index
a.sort() Sort lists from samll to large a.reverse() Reverse the contents of the list
4.tuple ()
a=(1,2,3,4,5) Tuples are readable and unchangeable.
5.Format output
print ("----%s----" % variable)
6.Determine whether the user input is a number?
if variable.digit(): If it's number , return True Else return False
7.dictionary
dic={"name":"Mr","age":22,"sex":"man"} Key-value pairs
Key is Immutable Changeless . It could be a string , a number or a tuple
Value is Varibale . it could be a list , a string , a number
The contents of the dictionary are disordered. Access accordng to Key.
8.Shopping test (source code)
salary = input("请输入您的工资:")
if salary.isdigit():
salary = int(salary)
shopping_car = []
shop = [['iphone6', 6000],
['mac book', 80],
['coffee', 35],
['bicycle', 1500],
['computer', 4000, ]]
while True:
for i, v in enumerate(shop, 1):
print(i, v)
choose = input("请输入你要购买的物品编号(退出q):")
if choose.isdigit():
choose = int(choose)
if choose>0 and choose<len(shop):
p_item = shop[choose-1]
if p_item[1] < salary:
salary -= p_item[1]
shopping_car.append(p_item)
else:
print("余额不足,还剩%s元钱" % salary)
print(p_item)
else:
print("商品编码不存在")
elif choose == 'q':
print("---------购买的商品--------")
for i in shopping_car:
print(i)
print("您还剩%s元钱" % salary)
break
else:
print("金钱不足")
浙公网安备 33010602011771号