It's been such a long time before I wrote Primer 2, I'm really embarrassed.
- list, tuple
First of all, lists and tuples are ordered, and tuples can be understood as read-only lists (const), using '[' for lists and '(' for tuples.
#! /usr/bin/env python # Author: JackyHou names = ["zhangsan", "lisi", "wangwu"] names.append("lijingyi")#Add after names.insert(1, "xiaolizi") # print(names[0:2])#Left closed and right open # # print(names[-1])#Start counting from the right # print(names[-3:])#Do not write anything after the colon, that is, the third from the right until the last # print(names[:3]) print(names) #delete #method 1 # names.remove("xiaolizi") # print(names) #Method 2 # del names[1] # print(names) #method 3 # names.pop()# can take parameters # print(names) #find print(names.index("xiaolizi")) print(names.pop(names.index("xiaolizi"))) print(names) #count names.append("lijingyi") print(names.count("lijingyi")) #reverse names.reverse() print(names) #sort names.sort() print(names) #extend names2 = [1, 2, 4, 5] names.extend(names2) of the names2 print(names)
One thing to pay attention to when using lists: the problem of shallow and deep copies
Shallow copy: copy
Deep copy: deepcopy
The difference between them is that when the list is a multi-level list, shallow copying and direct assignment will make the pointers of the secondary lists of the two objects point to the same object (that is, any operation will be reflected on the two objects, and both objects will be lost. One is prosperous), and deep copy is usually understood copy (full copy)
#! /usr/bin/env python # -*- coding:utf-8 -*- # Author: JackyHou import copy person = ['name', ['saving', 100]] # p1 = copy.copy(person) # p2 = person[:] # p3 = list(person) p1 = person[:] p2 = person[:] p1 [0] = 'hjq' p2 [0] = 'ljy' p1[1][1] = 50 print(p1) print(p2)
[1, ['equal', 2, 4], 4] [1, ['equal', 2, 4], 4] [1, ['equal', 'copy', 4], 4] [1, ['equal', 'copy', 4], 4] [1, ['equal', 'copy', 4], 4] [1, ['equal', 'copy', 'deepcopy'], 4]
- Basic operations on String
#! /usr/bin/env python # Author: JackyHou name = "my name is {name}, I'm {year}." print(name.capitalize()) print(name.count('a')) print(name.center(50, '*')) print(name.endswith("ou")) print(name.expandtabs(30)) print(name.find('is')) print(name.format(name='JackyHou', year=22)) print(name.format_map({'name': 'JackyHou', 'year': 22})) print(name.isalnum()) print('abc123'.isalnum()) print('abcABC'.isalpha()) print('1A'.isdecimal()) print('_1a'.isidentifier())#Determine whether it is a valid identifier print('abc'.islower())#Is it lowercase? print('22.11'.isnumeric())#Pure numbers print(name.isspace()) print('My Name Is'.istitle()) print('My Name is'.isprintable())#tty file, drive file print("My Name is".isupper()) print(':'.join(['1', '2', '3'])) print (name.ljust (50, '*')) print(name.rjust(50, '*')) print("Jacky".lower()) print("Jacky".upper()) print(' \nhaha'.lstrip()) print('haha \n'.rstrip()) print ('hha' .strip ()) la = str.maketrans("abcdef", '123456') print("JackyHou".translate(la)) print(name.replace('m', '1', 2)) print('hahahxhahahxha'.rfind('x'))#Start to find from the right print ('hah hah haha ah'.split ('')) #split print ('hahah + dkfjal \ njldafjlad'.splitlines ()) #Split line print('JackyHou'.swapcase())#Reverse case print('hah hah ha'.title()) print('jacky hou'.zfill(50))
- dictionary
#! /usr/bin/env python # Author: JackyHou #key-value info = { 'stu1101': "TengLan Wu", 'stu1102': "LongZe Luola", 'stu1103': "XiaoZe Maliya", } print(info) #print(info["stu1101"]) info["stu1101"] = "武藤兰" info["stu1104"] = "CangJingkong" print(info) #del info["stu1101"] # info.pop("stu1101") # info.popitem()# Delete one randomly # print(info) #find print(info.get('stu1105')) print('stu1105' in info) b = { 'stu1101': "Rihana", 1: 3, 4: 5, } info.update(b) print(info) c = dict.fromkeys([6, 7, 8], [1, {"name": "alex"}, 444]) print(c) c[7][1]['name'] = "Jack Chen"#This is a pit!!!!!!!!!!!! The fromkeys function will treat the latter as a whole, so if you change one, it will be all changed print(c) print(info.items()) #efficient for i in info: print(i, info[i]) #inefficient for k, v in info.items(): print (k, v)
- tertiary menu
#! /usr/bin/env python # Author: JackyHou # data = { # 'Beijing': { # "沙河": ["oldboy", "test"], # "Tiantongyuan": ["Lianjia Real Estate", "I love my home"] # }, # "Chaoyang": { # "Wangjing": ["Benz", "Momo"], # "国贸": ["CICC", "HP"], # "Dongzhimen": ["Advent", "Fetion"], # }, # # } data = { 'Beijing': { 'Haidian': { 'Wudaokou': { 'soho': {}, 'NetEase': {}, 'google': {} }, 'Zhongguancun': { 'IQIYI': {}, 'Autohome': {}, 'youku': {}, }, 'Uechi': { 'Baidu': {}, }, }, 'Changping': { 'Shahe': { 'old boys': {}, 'Beihang': {}, }, 'Tiantongyuan': {}, 'Huilongguan': {}, }, 'Chaoyang': {}, 'Dongcheng': {}, }, 'Shanghai': { 'Minhang': { "People's Square": { 'Fried Chicken': {} } }, 'Zhabei': { 'train battle': { 'Ctrip': {} } }, 'Pudong': {}, }, 'Shandong': {}, } exit_flag = False while not exit_flag: #first level menu for i in data: print(i) choice = input("SELECT>>>>>:") if choice in data: while not exit_flag: #Second level menu for j in data[choice]: print("\t", j) choice2 = input("SELECT>>>>>:") if choice2 in data[choice]: while not exit_flag: #third level menu for k in data[choice][choice2]: print("\t\t", k) choice3 = input("SELECT>>>>>:") if choice3 in data[choice][choice2]: for n in data[choice][choice2][choice3]: print("\t\t\t", n) choice4 = input("INPUT 'b' TO BACK") if choice4 == 'b': pass elif choice4 == 'q': exit_flag = True if choice3 == "b": break elif choice3 == 'q': exit_flag = True if choice2 == 'b': break elif choice2 == 'q': exit_flag = True if choice == 'b': break elif choice == 'q': exit_flag = True
Pay attention to the usage of pass:
It's not really useful. pass is the empty statement, in order to maintain the integrity of the program structure. do nothing,
Half are used as placeholder statements.
Half are used as placeholder statements.