python_day2
01、多级菜单
3 data = {
4 '北京':{
5 "昌平":{
6 "沙河":["oldboy","test"],
7 "天通苑":["链家地产","我爱我家"]
8 },
9 "朝阳":{
10 "望京":["奔驰","陌陌"],
11 "国贸":{"CICC","HP"},
12 "东直门":{"Advent","飞信"},
13 },
14 "海淀":{},
15 },
16 '山东':{
17 "德州":{},
18 "青岛":{},
19 "济南":{}
20 },
21 '广东':{
22 "东莞":{},
23 "常熟":{},
24 "佛山":{},
25 },
26 }
27 exit_flag = False
28
29 while not exit_flag:
30 for i in data:
31 print(i)
32 choice = input("选择进入1>>:")
33 if choice in data:
34 while not exit_flag:
35 for i2 in data[choice]:
36 print("\t",i2)
37 choice2 = input("选择进入2>>:")
38 if choice2 in data[choice]:
39 while not exit_flag:
40 for i3 in data[choice][choice2]:
41 print("\t\t", i3)
42 choice3 = input("选择进入3>>:")
43 if choice3 in data[choice][choice2]:
44 for i4 in data[choice][choice2][choice3]:
45 print("\t\t",i4)
46 choice4 = input("最后一层,按b返回>>:")
47 if choice4 == "b":
48 pass
49 elif choice4 == "q":
50 exit_flag = True
51 if choice3 == "b":
52 break
53 elif choice3 == "q":
54 exit_flag = True
55 if choice2 == "b":
56 break
57 elif choice2 == "q":
58 exit_flag = True
02、字典
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
for i in info:
print(i,info[i])
for k,v in info.items():
print(k,v)
03、列表
names = "ZhangYang Guyun Xiangpeng XuLiangChen"
names = ["4ZhangYang", "#!Guyun","xXiangPeng",["alex","jack"],"ChenRonghua","XuLiangchen"]
print(names[0:-1:2])
print(names[::2])
print(names[:])
range(1,10,2)
for i in names:
print(i)
name2 = copy.deepcopy(names)
print(names)
print(name2)
names[2] = "向鹏"
names[3][0] ="ALEXANDER"
print(names)
print(name2)
names.append("LeiHaidong")
names.insert(1,"ChenRonghua")
names.insert(3,"Xinzhiyu")
names[2] ="XieDi"
print(names[0],names[2])
print(names[1:3]) #切片
print(names[3]) #切片
print(names[-2:]) #切片
print(names[0:3]) #切片
print(names[:3]) #切片
delete
names.remove("ChenRonghua")
del names[1] =names.pop(1)
names.pop(1)
print(names)
print(names.index("XieDi"))
print( names[names.index("XieDi")] )
print(names.count("ChenRonghua"))
names.clear()
names.reverse()
names.sort()
print(names)
names2 = [1,2,3,4]
names
names.extend(names2)#合并names 和names2
del names2
print(names,names2)
04、列表应用
1 product_list = [
2 ('Iphone',5800),
3 ('Mac Pro',9800),
4 ('Bike',800),
5 ('Watch',10600),
6 ('Coffee',31),
7 ('Alex Python',120),
8 ]
9 shopping_list = []
10 salary = input("Input your salary:")
11 if salary.isdigit():
12 salary = int(salary)
13 while True:
14 for index,item in enumerate(product_list):
15 #print(product_list.index(item),item)
16 print(index,item)
17 user_choice = input("选择要买嘛?>>>:")
18 if user_choice.isdigit():
19 user_choice = int(user_choice)
20 if user_choice < len(product_list) and user_choice >=0:
21 p_item = product_list[user_choice]
22 if p_item[1] <= salary: #买的起
23 shopping_list.append(p_item)
24 salary -= p_item[1]
25 print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
26 else:
27 print("\033[41;1m你的余额只剩[%s]啦,还买个毛线\033[0m" % salary)
28 else:
29 print("product code [%s] is not exist!"% user_choice)
30 elif user_choice == 'q':
31 print("--------shopping list------")
32 for p in shopping_list:
33 print(p)
34 print("Your current balance:",salary)
35 exit()
36 else:
37 print("invalid option")
05、字符串操作
1 name = "my \tname is {name} and i am {year} old"
2
3 print(name.capitalize())
4 print(name.count("a"))
5 print(name.center(50,"-"))
6 print(name.endswith("ex"))
7 print(name.expandtabs(tabsize=30))
8 print(name[name.find("name"):])
9 print(name.format(name='alex',year=23))
10 print(name.format_map( {'name':'alex','year':12} ))
11 print('ab23'.isalnum())
12 print('abA'.isalpha())
13 print('1A'.isdecimal())
14 print('1A'.isdigit())
15 print('a 1A'.isidentifier()) #判读是不是一个合法的标识符
16 print('33A'.isnumeric())
17 print('My Name Is '.istitle())
18 print('My Name Is '.isprintable()) #tty file ,drive file
19 print('My Name Is '.isupper())
20 print('+'.join( ['1','2','3']) )
21 print( name.ljust(50,'*') )
22 print( name.rjust(50,'-') )
23 print( 'Alex'.lower() )
24 print( 'Alex'.upper() )
25 print( '\nAlex'.lstrip() )
26 print( 'Alex\n'.rstrip() )
27 print( ' Alex\n'.strip() )
28 p = str.maketrans("abcdefli",'123$@456')
29 print("alex li".translate(p) )
30
31 print('alex li'.replace('l','L',1))
32 print('alex lil'.rfind('l'))
33 print('1+2+3+4'.split('\n'))
34 print('1+2\n+3+4'.splitlines())
35 print('Alex Li'.swapcase())
36 print('lex li'.title())
37 print('lex li'.zfill(50))
06、列表操作
1 person=['name',['saving',100]]
2 '''
3 p1=copy.copy(person)
4 p2=person[:]
5 p3=list(person)
6 '''
7 p1=person[:]
8 p2=person[:]
9
10 p1[0]='alex'
11 p2[0]='fengjie'
12
13 p1[1][1]=50
14
15 print(p1)
16 print(p2)
浙公网安备 33010602011771号