元组和字典

1 元组是不可以修改的,但是列表是可以修改数据的

t1=(1,2,3)#可以逗号隔开不同的类型
type(t1)
t2=(10,)#定义单个数据元组
t3=(10)#返回的是int型
t4=('hello')#返回的是字符串类型
#元组数据的修改
t=(10,20,['aa','bb','cc'],50)
t[2][0]='aa'#元组数据的第一层不支持修改,里面的列表嵌套还是支持修改的
#查找操作
t1=(1,2,3)
print(t1.index(1))  #返回1所在下标。否则报错
print(t1.count(1))#计数
print(len(t1))#长度

字典

#字典的数据是以键值对的形式出现,和下标没有关系,没有顺序,没有下标,是可变类型
#字典的创建
dict1={'name':'tom','age':20,'gender':'男'}
#空字典的创建方法
dict1={}
dict3=dict()
dict1={'name':'tom','age':20,'gender':'男'}
dict1['name']='rose'#键值对的修改
print(dict1)
#增加一个键值对
dict1['id']=110
#删除数据 gender
del dict1['gender']     
dict1.clear()#全部清空

#新增
dict1={'name':'tom','age':20,'gender':'男'}
dict1['name']='rose'#完成修改
dict1['id']=110
#查找
dict1={'name':'tom','age':20,'gender':'男'}
print(dict1['name'])#返回tom
#函数查找
#get   字典序列.get(key,默认值)  查找参数不存在就返回默认值
dict1={'name':'tom','age':20,'gender':'男'}
print(dict1.get('name'))  #返回tom
print(dict1.get('id'110))  #返回110,因为id这个key不存在
print(dict1.get('id'))#没有设置参数返回none

#keys函数,查找所有的key,然后返回可以迭代的对象
dict1={'name':'tom','age':20,'gender':'男'}
print(dict1.keys()) #返回的是dict_keys(['name','age','gender'])  可以循环遍历的字典形式

#values函数  返回可以迭代的内容
dict1={'name':'tom','age':20,'gender':'男'}
print(dict1.values())#返回的是dict_keys(['tom','20','男'])  可以循环遍历的字典形式



#items函数返回的是可以迭代的对象,返回的是键值对(元组形式)
dict1={'name':'tom','age':20,'gender':'男'}
print(dict1.items())#返回可以迭代的对象dict_keys([('name','tom'),('age',20),('gender','男')])





2遍历字典的key


#遍历字典key
dict1={'name':'tom','age':20,'gender':'男'}
for key in dict1.keys():
	print(key)
#遍历字典value
dict1={'name':'tom','age':20,'gender':'男'}
for value in dict1.values():
	print(value)
#遍历键值对
dict1={'name':'tom','age':20,'gender':'男'}
for item in dict1.items():
	print(item)
#遍历键值对拆开
dict1={'name':'tom','age':20,'gender':'男'}
for key,value in dict1.items():
	print(f'{key}={value}')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值