1 List questions
l1 = [11, 22, 33] l2 = [22, 33, 44] # a. Obtain l1 Yes, there are. l2 Elements not in for i in l1: if i not in l2: print(i) # 11 # b. Obtain l2 Yes, there are. l1 List of elements not in for i in l2: if i not in l1: print(i) # 44 # c. Obtain l1 and l2 Elements with the same content in for i in l1: if i in l2: print(i, end=" ") # 22 33 # d. Obtain l1 and l2 Elements that don't exist in each other for i in l2: if i not in l1: print(i, end=" ") # 44 for i in l1: if i not in l2: print(i, end=" ") # 11
2. There are 1,2,3,4,5,6,7,8, 8 digits, and the number of output two digit elements are different from each other
count = 0 a = [1, 2, 3, 4, 5, 6, 7, 8] b = [1, 2, 3, 4, 5, 6, 7, 8] for i in a: for j in b: if i != j: count += 1 print(count) # 56 # 2_1.There are 1,2,3,4,5,6,7,8, 8 Digit number,The number of output two bit elements that are different from each other and the number is not repeated count = 0 a = [1, 2, 3, 4, 5, 6, 7, 8] b = [1, 2, 3, 4, 5, 6, 7, 8] for i in range(1, len(a) + 1): for j in range(i + 1, len(b) + 1): count += 1 print(count) # 28
3. Positive triangle of multiplication table
for i in range(1,10): for j in range(1,10): if i>= j: print("%s * %s = %s"%(j,i,i*j),end='\t') print()
4 99 multiplication table inverted triangle
for i in range(1,10): for j in range(1,10): if j >= i: print("%s * %s = %s"%(i,j,j*i),end="\t") else: print("%s"%" "*12,end="") print()
5 automatic calculation scheme of developing program with Python
# Five Wen for a rooster, three Wen for a hen, three Wen for a chick, and 100 Wen for a rooster. Among them, all roosters, hens and chicks must have one. How many roosters, hens, chicks and chicks do you want to buy just enough for 100 Wen? for i in range(1, 100 // 5): for j in range(1, 100 // 3): for z in range(1, 100): if i + j + z == 100 and 5*i + 3*j + z/3.0 == 100: print(i,j,z) """ 4 18 78 8 11 81 12 4 84 """
6. Use underscores to splice each element of the list into strings ['tang','lao','er ']
# Note: this method only applies to elements that are str type li = ['tang','lao','er'] v = "_".join(li) print(v) # tang_lao_er # 6_1 The list with numbers is spliced into a string li = ['tang','lao','er',123] v = "_".join("%s" %i for i in li) print(v) # tang_lao_er_123 # 6_2 There are many data types spliced into strings li = ['tang','lao','er',[11,22],True] v = "_".join("%s" %i for i in li) print(v) # tang_lao_er_[11, 22]_True
There is a tuple ('tang','lao','er ') to write code, which realizes the following functions
tu = ('tang','lao','er',) # a. Calculated length print(len(tu)) # b. Get the second of the tuple-3 Tuples, and output print(tu[1:3]) # ('lao', 'er') # Be careful: The slice index exceeds the boundary size without error print(tu[1:10]) # ('lao', 'er') print(tu[10:]) # () # c. Please use for Element of output tuple for i in tu: print(i,end=" ") # tang lao er print() # d. Please use for, len ,range Index of output tuples for i in range(len(tu)): print(i,end=" ") # 0 1 2 print() # f. Please use enumrate Output tuple element and sequence number(Serial number starts from 10) for key, value in enumerate(tu,10): print((key,value),end=" ") # (10, 'tang') (11, 'lao') (12, 'er')
8. There are the following variables, please implement the required functions
tu = ("tang",[11,22,{"k1":"v1","k2":["age","name"],"k3":(11,22,33)},44]) # a.Properties of tuples #Tuple immutable type cannot be added, deleted or modified #b. First element"tang", Modifiable or not # no # c "k2"What is the corresponding value type? Is it modifiable? If yes, please add an element to it"san" # List type can be changed tu[1][2]["k2"].append("san") print(tu[1][2]["k2"]) # ['age', 'name', 'san'] # d "k2"What is the corresponding value type? Is it modifiable? If yes, please add an element to it"san" # Tuple type cannot be changed
9. The bool value is False
Notation 6+1 6 = 3 + 3: 3 brackets {} [] () 3 basic type integers = 0, string = "", bool= False 1: None
10. Find a set of elements whose sum of any two values in a list is equal to 9
nums = [2,7,11,25,1,8,7] a = [] for i in nums: for j in nums: if i+j == 9: a.append((i,j)) print(a) # [(2, 7), (2, 7), (7, 2), (1, 8), (8, 1), (7, 2)] # 10_1 Find a set of index elements whose sum of any two values in a list is equal to 9 nums = [2,7,11,25,1,8,7] a = [] for i in range(len(nums)): for j in range(len(nums)): if nums[i]+nums[j] == 9: a.append((i,j)) print(a) # [(0, 1), (0, 6), (1, 0), (4, 5), (5, 4), (6, 0)]
11 list reversal
li = [11,22,33,44] li.reverse() print(li) # [44, 33, 22, 11]
12 page display content
# a adopt for Loop to create 302 pieces of data, with unlimited data types, such as: user_list = [ {'name':'tang1','email':'tang@qq.com','pwd':'tangpwd1'} ] for i in range(1,302): temp = {'name':"tang"+str(i),'email':'tang@qq.com'+str(i),'pwd':'pwd'+str(i)} user_list.append(temp) # print(user_list) #b Prompt user to enter page number, 10 lines of data per page,Return data according to the page number entered by the user while True: s = input('Please input 1,2..30 Page number:') s = int(s) start = (s-1) * 10 end = s * 10 result = user_list[start:end] for item in result: print(item)
13 three level menu
db ={ "Beijing":{ "Changping":{"Shahe":{}}, "Haidian":{}, "Chaoyang":{}, }, "Guangzhou":{ "Tianhe River":{"Tianhe City"}, "Haizhu":{"Lentu Village"}, } } path = [] while True: temp = db for item in path: temp = temp[item] # temp Point to last node path = ["Beijing", "Changping","Shahe"] print("All children of the current node:",list(temp.keys())) choice = input('1:Add node 2:View node (Q Sign out | B Go back to the previous level)\n >>>') if choice == "1": name = input("Please enter the name of the node to insert>>>") if name in temp: print("Node already exists") else: temp[name] = {} elif choice == "2": name = input("Please enter the node name to view>>>") if name in temp: path.append(name) else: print("This node name does not exist") elif choice.lower() == "b": if path: path.pop() elif choice.lower() == 'q': break else: print('Input error,Please re-enter:')