Learning summary on September 23
1, Tuple
1. Definition of tuple
1. Tuple is container type data (sequence). Take () as the flag of the container, and multiple elements in it are separated by commas: (element 1, element 2,...)
2. Tuples are immutable (addition, deletion and modification are not supported); Tuples are ordered (subscript operation is supported)
3. Element: any type of data
t1 = (10, 20, 30) print(t1, type(t1)) t2 = (10, 'abc', False, [10, 230]) print(t2) t3 = () # Empty tuple print(t3, type(t3)) # (10, 20, 30) <class 'tuple'> # (10, 'abc', False, [10, 230]) # () <class 'tuple'>
2. Tuples are immutable lists
1. All elements in the list support operations other than those related to addition, deletion and modification
For example, query, related operations, related methods (except those related to addition, deletion and modification), and related functions
# Determine whether a data is one of a certain type num = 23 t_num = type(num) if t_num == int or t_num == float or t_num == str: print('Is it a number or a string') if t_num in (int, float, str): print('Is it a number or a string')
3. Special or common operations
1. Tuple with only one element: (element,)
list1 = [10] print(list1, type(list1)) t1 = (10,) print(t1, type(t1)) # [10] <class 'list'> # (10,) <class 'tuple'>
2. Tuples can be omitted () without ambiguity. Separate multiple data directly with commas, which also represents a tuple
t2 = 10, 20, 30, 40, 50 print(t2, type(t2)) # (10, 20, 30, 40, 50) <class 'tuple'>
3. Use multiple variables to obtain tuple elements at the same time (the list also supports)
t3 = ('Xiao Ming', 18, 90) print(t3[0])
a. Keep the number of variables consistent with the number of elements in the tuple
t3 = ('Xiao Ming', 18, 90) name, age, score = t3 print(name, age, score) point = (100, 200) x, y = point print(x, y)
b. Let the number of variables be less than the number of tuple elements, but it must be added before a variable*
Let the variables that do not replace * get the elements according to the position, and save the rest to the list corresponding to the variables with *
t3 = ('Xiao Ming', 18, 170, 80, 99, 80, 76) x, y, *z = t3 print(x, y, z) # Xiaoming 18 [170, 80, 99, 80, 76] x, *y, z = t3 # Xiao Ming [18, 170, 80, 99, 80] 76 print(x, y, z) *x, y, z = t3 # ['Xiao Ming', 18, 170, 80, 99] 80, 76 print(x, y, z) x, *y, z, t = t3 print(z) # 80 print(y) # [18, 170, 80, 99]
2, Dictionary
1. What is a dictionary
The dictionary is a container data type (sequence). It takes {} as the flag of the container. Multiple key value pairs are separated by commas: {key 1: value 1, key 2: value 2, key 3: value 3,...}
Key value pair: key value
2. The dictionary is changeable (adding, deleting and modifying are supported); The dictionary is out of order (subscript operation is not supported)
3. Element key value pair
Key - must be immutable data, such as tuples, numbers, strings. Key is unique.
Value (the data you really want to save) - no requirement
# 1) Empty dictionary dict1 = {} print(dict1, type(dict1)) # 2) Keys are immutable data dict2 ={'a': 10, 1: 20, (1, 2): 30} print(dict2) # dict2 ={'a': 10, 1: 20, [1, 2]: 30} # print(dict2) # report errors! # 3) The key is unique dict3 = {'a': 10, 1: 20, 'a': 30} print(dict3) # {'a': 30, 1: 20}
3, Gets the value of the dictionary
1. Check single
1. DICTIONARY [key] - get the value corresponding to the specified key in the dictionary. An error will be reported when the key does not exist
2. Dictionary. Get = = dictionary. Get (key, None) - get the corresponding value in the dictionary; When the key does not exist, no error will be reported and None will be returned
3. Dictionary. Get (key, default value) - get the corresponding value in the dictionary; When the key does not exist, no error will be reported, and the default value will be returned
dog = {'name': 'Xiao Hei', 'breed': 'Siberian Husky', 'age': 3, 'color': 'black'} print(dog['breed']) print(dog.get('name')) # print(dog['gender']) # report errors print(dog.get('gender')) # None student = {'name': 'Xiao Ming', 'age': 18, 'gender': 'male'} print(student['age']) print(student.get('name')) print(student.get('contacts', '110')) # Siberian Husky # Xiao Hei # None # 18 # Xiao Ming # 110
2. Traversal
# for variable in Dictionary: # Circulatory body # # Note: the variable gets the key dog = {'name': 'Xiao Hei', 'breed': 'Siberian Husky', 'age': 3, 'color': 'black'} for x in dog: print(x, dog[x])
# Exercise: use a dictionary to save class information class1 = { 'name': 'Python2106', 'address': '9 teach', 'lecturer': { 'name': 'Yu Ting', 'age': 18, 'tel': '13678192302', 'QQ': '726550822' }, 'head_teacher': { 'name': 'Rui Yan Zhang', 'tel': '110', 'QQ': '7283211' }, 'students': [ { 'name': 'Chen Lai', 'age': 20, 'gender': 'female', 'score': 98, 'contacts': { 'name': 'p2', 'tel': '120' } }, { 'name': 'Ge Yilei', 'age': 25, 'gender': 'male', 'score': 80, 'contacts': { 'name': 'p1', 'tel': '119' } } ] } # 1. Get the class name print(class1['name']) # 2. Get the instructor's name and age print(class1['lecturer']['name'], class1['lecturer']['age']) # 3. Get the name and telephone number of the head teacher print(class1['head_teacher']['name'], class1['head_teacher']['tel']) # 4. Get the name of the first student print(class1['students'][0]['name']) # 5. Get the contact names of all students for x in class1['students']: print(x['contacts']['name']) # 6. Calculate the average score of all students scores = [] for x in class1['students']: print(x['score']) scores.append(x['score']) print('The average score is:', sum(scores) / len(scores))
4, Addition, deletion and modification of dictionary
1. Add - add key value pair
2. Value corresponding to modify key
Syntax: Dictionary [key] = value - when the key exists, it is modified, and if the key does not exist, it is added
goods = {'name': 'Instant noodles', 'price': 3.5} print(goods) goods['count'] = 10 print(goods) goods['price'] = 4 print(goods) # {'name': 'instant noodles',' price': 3.5} # {'name': 'instant noodles',' price ': 3.5,' count ': 10} # {'name': 'instant noodles',' price ': 4,' count ': 10}
3. Delete
(1) del DICTIONARY [key] - delete the key value pair corresponding to the specified key in the dictionary
goods = {'name': 'Instant noodles', 'price': 4, 'count': 10} del goods['price'] print(goods) # {'name': 'instant noodles',' count': 10}
(2) Dictionary. Pop (key) - retrieves the value corresponding to the specified key in the dictionary
goods = {'name': 'Instant noodles', 'price': 4, 'count': 10} result = goods.pop('price') print(goods) # {'name': 'instant noodles',' count': 10} print(result) # 4 # {'name': 'instant noodles',' count': 10} # 4