Tuple
It is an immutable list, supports search, does not support addition, deletion, and
modification. Tuples are in order, so subscript operations are supported. Tuples are container data types-(Element 1, Element 2...)
A tuple of elements: (element,)
Omit parentheses.
If there is no ambiguity, you can omit (), and directly separate multiple elements with commas to represent a tuple
Variable 1, Variable 2… = Tuple The
number of variables is the same as the number of tuple elements, and the values of all elements of the tuple are obtained
When multiple variables are used to obtain tuple elements at the same time, the number of variables can be less than the number of elements in the tuple, but there must be one and only one variable before *
Dictionary (dict)
It is a container data type, using {} as the symbol of the container. Multiple elements inside are separated by commas (the elements must be key pairs)-{key1:
value1, key2: value2 ...} The dictionary is variable and supports additions, deletions, and modifications
The dictionary is unordered and does not support subscript operations.
Element: must be a key-value pair.
Key: must be immutable data (numbers, strings, tuples, variable data such as lists cannot be used), generally use strings; keys are used To describe and distinguish the unique
value of the value: there is no requirement that the
key is unique
Add, delete, check and modify
the dictionary Check-dictionary[key] or dictionary.get(key)
dictionary.get(key, default value)
for variable in dictionary:
loop body
variable gets the key in the dictionary
Add and change:
dictionary [key] = value depends on whether the key exists to decide whether to add or change
Delete
del dictionary [key]
dictionary.pop(key)
Dictionary application
Exercise: Define a variable to save the information of a class. Class information mainly includes: class name, location, capacity, class teacher, lecturer, all students.
Class teacher: name, gender, age, phone number, QQ
lecturer: name, gender, age, phone number, QQ, level
All students: multiple students, each student includes: name, gender, age, education, major, telephone
class1 = {
'name': 'python2004',
'location': '9教室',
'capacity': 50,
'class_teacher': {
'name': '张老师',
'gender': '女',
'age': 30,
'tel': '110',
'qq': '7282233'
},
'teacher': {
'name': '骆昊',
'gender': '男',
'age': 35,
'tel': '172623283',
'qq': '2828332',
'rank': '特级讲师'
},
'students': [
{
'name': '小明', 'gender': '男', 'age': 19},
{
'name': '小花', 'gender': '女', 'age': 18},
{
'name': '张三', 'gender': '男', 'age': 23},
{
'name': 'Tom', 'gender': '男', 'age': 22},
{
'name': 'Bob', 'gender': '男', 'age': 30}
]
}
print(class1['teacher']['name'], class1['teacher']['rank']) #查询教师名字和等级
sum1 = 0
for x in class1['students']:
sum1 += x['age'] #遍历学生年纪并加和
print(sum1 / len(class1['students'])) #求学生年纪的平均值
Dictionary related operations
The dictionary does not support addition, multiplication and size comparison
Dictionary support:
1.in and not in-judgment key
2.len
3.dict-data requirements: a. The data itself is a sequence; b. Each element in the sequence must be a small sequence of length 2; c. Small sequence The first element in is immutable.
4.list (dictionary)-dictionary key as element
Dictionary related methods:
1. Dictionary.clear(), which is more efficient than = {}
2. Dictionary.copy()
3.dict.fromkeys(sequence, value)-The default value is None, and the key is the sequence
4. Dictionary.keys( )
5. Dictionary .values ()
6. The dictionary .items ()
7. The dictionary .setdefault (key, value) - only added without modifying
8. dictionary 1.update (Dictionary 2) - is not necessarily dictionary dictionary 2, may be Is a sequence that can be converted into a dictionary