元祖操作

##################元组#################
# age=(11,22,33,44,55)本质age=tuple((11,22,33,44,55))

#索引
# t=(1,2,3,4,5,6)
# print(t[1:4])
# print(t[0])
# print(t[1:3])
# print(t[2::-1])


#len 取列表长度
# t=('alex','egon','www','aaa','ladasd')
# print(len(t))

#成员运算 in和not in
# stus=('alex','egon','wxx','yxx','lxx')
# print('alex' in stus) 
# print('alex' not in stus) #判断元素是否在列表里面

#循环
# l=(1,2,3,4,5,6)
# for i in l:
# print(i)

#索引 
# t=('a','b','c','c')
# print(t.index('a'))


#统计
# t=('a','b','c','c')
# print(t.count('c'))


#############字典####################
#索引取值
# dic={'name':'egon','zhang':123}
# print(dic['zhang'])

#修改值vlaue
# dic={'zhang':'123','egon':456}
# print(dic)
# dic['zhang']='789'
# print(dic)

#小写转换大写
# dic={'name':'egon'}
# dic['name']=dic['name'].upper()
# print(dic)

#长度
# dic={'name':'egon','age':18}
# print(len(dic))

#删除
# dic={'name':'egon','age':18}
# del dic['name']
# print(dic)

#删除一个元素 取到返回值 没有值 就会返回None
# res=dic.pop('name')
# print(res)

#循环取值key
# dic={'name':'egon','age':18}
# print(list(dic.keys())[1])

#循环取多个key
# for x in dic.keys():
# print (x)

#循环取值values
# dic={'name':'egon','age':18}
# print(list(dic.values())[1]) #取vlaue值
# print(dic.values()) #字典转化为列表

#取多个值
# for value in dic.values():
# print(value)

#字典转化元组
# print(dic.items()) #字典转化为元组
# for k,v in dic.items(): #k,v=('name','egon')
# print(k,v)
# for i in dic.items():
# print(i[0])

#根据key取value 判断这个值在不在key里面 不在返回none
# dic={'name':'egon','age':18}
# print(dic.get('name'))

#随机产生一个元祖键值对
# print(dic.popitem())

#更新元素
# dic.update({'x':1,'age':19}) #有就不添加 没有就添加
# print(dic)


#造字典
# print({}.fromkeys(['name','age','sex'],None))

# list1=['name','age','sex']
# dic={}
# for x in list1:
# dic[x]=None
# print(dic)

# setdefault 用法
# dic={'name':'egon','age':18}
# 1.字典有key,则不修改,返回原key对于的原值
# res=dic.setdefault('name','EGON')
# print('返回值',res)


#2.没有对应的key,则添加,返回添加的key对应的value
# dic={'age':18}
# res=dic.setdefault('name','EGON')
# print('返回值',res)
# print(dic)

  

posted @ 2018-08-25 15:35  人生信条~~  阅读(154)  评论(0编辑  收藏  举报