学习Python的第五天

                                                                       元组操作

输出

  

                                                元组的常见操作

 

                 

输出

                                                                     字典的创建

# 字典的定义  dict1表示字典的意思
dict1 = {}
dict2 = dict()
print(dict1, type(dict1))
print(dict2, type(dict2))

# 使用大括号 然后逗号分割 键值对之间 使用冒号
# 1.没有顺序 没有办法通过下标 进行查找 只能通过键 进行查找
dict3 = {"name": "秦晓添", "age": 20, "gender": "man"}
print(dict3, type(dict3))

输出:

 

                                                                   字典操作

dict1 = {"name": "顺顺", "age": 18}
print(dict1, type(dict1))

# 给字典添加一对键值对
dict1["gender"] = "man"
print(dict1, type(dict1))

# 删除键值对
del dict1["gender"]
print(dict1, type(dict1))

# 清空键值对 clear
dict1.clear()
print(dict1, type(dict1))

# 查找
dict2 = {"name": "豪豪", "age": 14}
print(dict2["name"])      # 查找指定键 对应的值
# print(dict2["gender"])    查找没有的键 则报错

print(dict2.get("age"))    # 查找年龄
print(dict2.get("id"))    # 查找没有的键 则返回None
print(dict2.get("id", 110))    # 查找没有的键 则返回默认值

# 查找所有的键
dict3 = {"name": "Simon", "age": 36, "height": 138}
print(dict3.keys(), type(dict3.keys()))  # 查找所有的键 组成一个列表
print(dict3.values(), type(dict3.keys()))  # 查找所有的值 组成一个列表
# 查找所有的键值对 组成一个列表 里面是每一对键值对都是元组
print(dict3.items(), type(dict3.items()))
print("==========================================")

# 字典的遍历
for key in dict3.keys():
    print(key)

print("======================")
for value in dict3.values():
    print(value)


print("=====================================")

# 遍历字典所有的键和值
for k, v in dict3.items():
    print(k, v)

 

 

                                                              集合的定义

 

# 集合是大括号 定义 是一个序列
# 定义一个空集合
set1 = set()
set3 = {}
print(set1, type(set1))
# {}不能作为 空集合的定义方式 他本质上是一个字典
print(set3, type(set3))

# 定义一个空集合 使用逗号 隔开
set2 = {1, 3, 4}
print(set2, type(set2))

                                                         集合操作

set1 = {1, 3, 5}
print(set1)


# 增加数据 到集合中
set1.add("张三")
print(set1)



# update表示更新数据 集合的特点之一 不能有重复数据 可以作为序列的去重工具
set1.update([1, 2])
print(set1)


# 会将字符串abc加入到 集合中 且发现 集合是没有顺序的
set1.update("abc")
print(set1)


set1.remove("张三")
print(set1)


# 删除不存在的值 则报错
# set1.remove("李四")
# print(set1)

# 删除数据
set2 = {1, 2, "张飞"}
set2.discard(1)
print(set2)


# 删除不存在的值不报错
set2.discard("陈浩")
print(set2)

# 随机弹出一个值 返回的是随机值
set3 = {1, 2, 3, 4, 5}
result = set3.pop()
print(result)

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值