Python基础学习-09
dictionary = {
"name": "Jone",
"age": 15,
"numbers": "138123456"
}
print(dictionary)
print(dictionary["name"])
print(len(dictionary))
print(type(dictionary))
"""
1. List 是一个有序且可变的集合。允许重复成员。
2. 元组 是一个有序且不可更改的集合。允许重复成员。
3. Set 是一个无序且无索引的集合。没有重复的成员。
4. 字典 是一个有序且可变的集合。没有重复的成员。
"""
test = {
"name": "七七",
"age": 19,
"address": "Inner Mongolia"
}
x = test["name"]
print(x)
y = test.get('name')
print(x, y)
x = test.keys()
print(x)
test['age'] = 20
print(test)
x = test.values()
print(x)
test['name'] = "77"
print(test)
x = test.items()
print(x)
test = {
"name": 77,
"age": 19,
}
if 'name' in test:
print("name在字典内")
test = {
"name": "七七",
"age": 19,
"address": "Inner Mongolia"
}
print(test)
test['age'] = 20
print(test)
test.update({"age": 25})
print(test)
test = {
"name": "七七",
"age": 19,
"address": "Inner Mongolia"
}
print(test)
test["sex"] = "男"
print(test)
test.update({"weight": 53})
print(test)
test.pop("sex")
print(test)
test.popitem()
print(test)
del test['age']
print(test)
del test
test = {
"name": "七七",
"age": 19,
"address": "Inner Mongolia"
}
test.clear()
print(test)
test = {
"name": "七七",
"age": 19,
"address": "Inner Mongolia"
}
for x in test:
print(x)
for x in test:
print(test[x])
for x in test.values():
print(x)
for x in test.keys():
print(x)
for x, y in test.items():
print(x, y)
temp = test.copy()
print(temp)
tmp = dict(test)
print(tmp)
family = {
"father": {
"name": "Tom",
"age": "18",
"years": "2001"
},
"mother": {
"name": "Amin",
"age": "20",
"years": "2002"
},
"child": {
"name": "Jery",
"age": "15",
"years": "2015"
}
}
print(family)
child1 = {
"name": "Emil",
"year": 2004
}
child2 = {
"name": "Tobias",
"year": 2007
}
child3 = {
"name": "Linus",
"year": 2011
}
family = {
"child1": child1,
"child2": child2,
"child3": child3
}
print(family)
car ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car["model"])
car ={
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car)
car["year"] = 2020
print(car)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.update({"color": "red"})
print(car)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)
|