1. What is the difference between list and common variable
Lists are data types, and common variables are used to store data
You can assign a list to a normal variable
2. There is a list a = [11, 22, 33], how to add (add) new elements to the list 44
a.append(44)
Or
a.insert(3,44) ා index number is 3
3. Sort the list
a = [11,22,33,2]
b = sorted(a) ා a new list is created, and a.sort() modifies a list
print(b) # [2, 11, 22, 33, 44]
b = a.sort()
print(b) # None
print(a) # [2, 11, 22, 33, 44]
4. Dictionary info = {'name': 'Li Si'}, delete element name
del info["name"]
Or
info.pop("name")
5. Differences between dictionaries and lists and the purpose of using dictionaries
Dictionaries store data in the form of key value pairs. Dictionaries are unordered. Corresponding values are obtained through keys
The list is ordered and the value is obtained by subscript
The dictionary can be used to store some identifying data and give a specific name to the data.
Add elements to list: list.append(s),list.extend(list2), list.insert(index,s),
1 a = [11,22,33,2] 2 b= [22,44] 3 str = '2' 4 a.extend(str) # [11, 22, 33, 2, '2'] 5 print(a) 6 a.extend(b) # [11, 22, 33, 2, '2', 22, 44] 7 print(a)
Dictionary: dict[key] = value,dict.update(dict2),
Set set (hash store): set.add(s),set.update(iterable)
1 set1 = {10,3.13,20,"hello"} 2 set2 = {10,18} 3 set1.update(set2) # 10 Not added, 18 added 4 print(set1) 5 6 list = [1,[2,3]] 7 set1.update(list) # TypeError: unhashable type: 'list'
del list[index],list.pop(index),list.remove(s)
Delete dictionary by del dict[key], dict.pop(key)
set.pop(),set.remove(s)
1 set1.pop() # Delete the last one, but the last one is random, so it can be considered as random deletion 2 print(set1) 3 4 set1.remove("15") 5 print(set1)
User registration login system
1 user_dict = {} 2 user_list = [] 3 log = True 4 5 def prelog(): 6 active = True 7 while active: 8 user_name = input("Please enter your nickname") 9 len_username = len(user_name) 10 if len_username > 6 and len_username < 20: 11 while active: 12 user_pass = input("Please enter your password") 13 len_userpass = len(user_pass) 14 if len_userpass > 8 and len_userpass < 20: 15 while active: 16 user_age = input("Please enter your age") 17 if user_age.isdigit(): 18 user_dict['Nickname?'] = user_name 19 user_dict['Password'] = user_pass 20 user_dict['Age'] = user_age 21 user_list.append(user_dict) 22 active = False 23 else: 24 print("Please enter a pure number") 25 continue 26 else: 27 print("Illegal password length") 28 continue 29 else: 30 print("Illegal nickname length") 31 continue 32 33 def login(): 34 signal2 = True 35 while True: 36 if signal2: 37 global log 38 log = False 39 signal = False 40 user_name = input("Please enter your user name") 41 user_pass = input("Please enter your password") 42 for e in user_list: 43 if e.get("Nickname?") == user_name: 44 real_pass = e["Password"] 45 if real_pass == user_pass: 46 signal = True 47 print("Login successfully") 48 print("Successful operation") 49 ask = input("Exit? y/n") 50 if ask == 'y': 51 signal2 = False 52 log = True 53 break # immediate withdrawal for loop 54 else: 55 signal2 = False 56 break #immediate withdrawal for loop 57 if not signal: 58 print("Wrong user name or password") 59 continue 60 else: 61 break # immediate withdrawal while loop 62 63 while True: 64 choice = input("""1.register 65 2.Sign in 66 3.Cancellation 67 4.Sign out 68 """) 69 if choice == '1': 70 prelog() 71 elif choice == '2': 72 if log == True: 73 login() 74 # continue 75 else: 76 print("User interface is occupied") 77 elif choice == '3': 78 if log == True: 79 print("No user login") 80 else: 81 log = True 82 elif choice == '4': 83 if log == True: 84 break 85 else: 86 print("Please write off first.") 87 else: 88 print("Please input 1-4")