Tuple Tuple
Immutable lists are called tuples. The syntax is the same as a list.
tuple1=(1.2.3)
tuple1[0] =6
Copy the code
Results:
TypeError: 'tuple' object does not support item assignment
Copy the code
As you can see from the above code, elements in a tuple cannot be modified. However, you can associate an old tuple variable name with a newly defined tuple.
tuple1=(1.2.3)
tuple1=(6.2.3)
print(tuple1)
Copy the code
Results:
(6.2.3)
Copy the code
A. Dictionary B. Dictionary C. Dictionary D. Dictionary
Dictionaries in Python are similar to maps in Java. But it’s formatted like JSON… Dictionaries/lists can also be nested inside dictionaries.
dist={"class":"2-5"."student": [{"name":"xiaohua"."age":23}, {"name":"xiaohuang"."age":"28"}}]Copy the code
Increased 1.
dist={"class":"2-5"."student": [{"name":"xiaohua"."age":23}, {"name":"xiaohuang"."age":"28"}]}
dist["lead"] ="teacher_wang"
print(dist)
Copy the code
Results:
{
"class":"2-5"."student":[
{
"name":"xiaohua"."age":23
},
{
"name":"xiaohuang"."age":"28"}]."lead":"teacher_wang"
}
Copy the code
2. Delete
dist={"class":"2-5"."student": [{"name":"xiaohua"."age":23}, {"name":"xiaohuang"."age":"28"}]}
dist["lead"] ="teacher_wang"
Delete by key
del dist["lead"]
print(dist)
Copy the code
3. The traverse
Iterate over all the keys and values in the dictionary
dist={"class":"2-5"."student": [{"name":"xiaohua"."age":23}, {"name":"xiaohuang"."age":"28"}}]for key,value in dist.items():
print(f"key:{key}")
print(f"value:{value}")
Copy the code
Results:
key:class
value:2-5
key:student
value:[{'name': 'xiaohua'.'age': 23}, {'name': 'xiaohuang'.'age': '28'}]
Copy the code
Iterate over all the keys in the dictionary
for key in dist.keys():
print(f"key:{key}")
Copy the code
Iterate over all the values in the dictionary
for v in dist.values():
print(f"value:{v}")
Copy the code