Python基础学习-09

Python基础学习-09

# Python dict
dictionary = {
    "name": "Jone",
    "age": 15,
    "numbers": "138123456"
}
print(dictionary)
# 字典是有序的、可变的,并且不允许重复。字典项以键值对的形式呈现,可以使用键名进行引用
print(dictionary["name"])
# 字典内的重复值将覆盖现有值

# the dict len
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在字典内")
# change
test = {
    "name": "七七",
    "age": 19,
    "address": "Inner Mongolia"
}
print(test)
test['age'] = 20
print(test)
test.update({"age": 25})
print(test)
# add
test = {
    "name": "七七",
    "age": 19,
    "address": "Inner Mongolia"
}
print(test)
test["sex"] = "男"
print(test)
test.update({"weight": 53})
print(test)
# Del
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)

# traverse
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)

# copy
temp = test.copy()
print(temp)

tmp = dict(test)
print(tmp)

# nest (嵌套)
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)
# test
# 1-使用gCet方法打印汽车字典的“model”键的值。
car ={
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(car["model"])
# 2-将“year”值从 1964 更改为 2020。
car ={
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(car)
car["year"] = 2020
print(car)
# 3-将键/值对 “color” : “red” 添加到汽车字典中。
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.update({"color": "red"})
print(car)
# 4-使用 pop 方法从汽车字典中删除“model”。
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.pop("model")
print(car)
# 5-使用clear方法清空car字典。
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.clear()
print(car)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七七高7777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值