元组:元祖就是一个不可变的裂变
基本使用介绍:
1.用途: 当我们需要记录多个同种属性大的值,并非只有读的需求没有改的需求时,应该用元祖
2.定义方式:在()内用逗号分隔开多个任意类型的元素
t = ('egon') #注意当元祖内只有一个元素时,必须加逗号,t = tuple(('egon'))
print(t,type(t))
print(t[0])
数据类型的转换:
t = tuple('hello')
print(t,type(t))
3.常用操作+内置的方法
1.按索引取值(正向取+反向取):只能取
t = ('a','b','c')
t[0] = 'A'
2.切片(顾头不顾尾,步长)
t = ('a','b','c')
print(t[0:2])
3.长度:
print(len(t))
4.成员运算和in 和not in:
t = ('a','b','c',(1,2,3))
print((1 ,2,3)in t)
5.循环:
t = ('a','b','c',(1,2,3))
for item in t:
print(item)
t = ('a','b','c',(1,2,3))
print(t.count('b')) 记录'b'出现的次数
print(t.index('b',2,4)) index找出对应值在变量中的位置 2,4为索引对应的位置。
元组总结;
可以存多个值
有序
属于不可变类型
t = (1,'a',['m','n'])
print(id(t[0]),id(t[1]),id(t[2])
t[2][0] = 'M'
print(t)
print(id(t[0]),id(t[1]),id(t[2])
t[2] =333333
字典类型:
1.用途:记录多个不同属性的值
2.定义方式:在{}内用逗号分隔开多个元素,每一个元素都是key:value的形式,
其中value可以是任意类型,而kay必须是不可变的类型(通常应该是str类型)
dic = {1:'aaa',1.3:'bbc',(1,2,3):'ccc','xxx':'ddd'} #dic = dict(....)
dic = {'x':1,'x':2,'x':3}
print(dic)
数据类型转换:dict
l = (['name','egon'],['age':18],('gender','male'))
#dic = {}
for k, v in l: #k, v =('name','egon')
dic[k] = v
dic = dict(l)
print(dic)
dic = dict(x = 1, y = 2, z = 3)
print(dic)
3.常用操作+内置的方法:
1.按value存取值:可存可取
dic = {'name':'egon','age':18,'gender':'male'}
print(dic['name'])
print(di(dic))
dic['name'] = 'EGON'
print(dic,id(dic))
dic['height'] = 1.81 #如果key不存在则新增一组key:value
print(dic)
l = ['a']
l[1] = 'b'
2.长度len:
dic{'name':'egon','age':18,'gender':'male'}
print(len(dic))
3.成员运算 in 和not in #判断是字典的key
dic{'name':'egon','age':18,'gender':'male'}
print('name' in dic)
4.删除:
dic{'name':'egon','age':18,'gender':'male'}
del dic['name']
print(dic)
t = dic.popitem()
print(t,type(t))
v = dic.pop('name')
print(v)
5.键keys(),值values(),键值对items()
dic{'name':'egon','age':18,'gender':'male'}
dic.keys()
['gender','age','name']
dic.values()
['male',18,'egon']
dic.item()
[('gender','male'),('age':18)('name':'egon')]
keys = dic.keys()
vals = dic.values()
items = dic.items()
print(keys)
print(values)
print(items)
print(list(keys))
print(list(values))
print(list(items))
6.循环:
for k in dic.keys:
print(k)
for k in dic:
print(k)
for v in dic.values();
print(v)
for k,v in dic.iteams():
print(k,v)
7.get取值
dic{'name':'egon','age':18,'gender':'male'}
if 'xxx' in dic:
print(dic['xxx'])
else:
print(None)
v = dic.get('xxx')
print(v)
dic.fromkeys()
l = ['name','age','gender']
dic = {}
for k in l:
dikc[k] = None
print(dic)
dic = {}.formkeys(l,None)
print(dic)
dic ={'kl':1}.formkeys(l.None)
print(dic)
keys = ['name','age','gender']
vals = ['egon',18,'male']
dic = {}.formkeys(keys.vals)
print(dic)
dic = {}
for i in range(len(keys))
k = keys[i]
v = vals[i]
dic[k] = v
print(dic)
old_dic={'name': 'egon', 'age': 18, 'gender': 'male'}
new_dic={'x':1,'name':'EGON'}
old_dic.update(new_dic)
print(old_dic)
dic.setdefault(k,v):执行完毕后是为了保证字典中必须有这么一个k
dic={'name': 'egon', 'age': 18, 'gender': 'male'}
v=dic.setdefault('height',1.81) # 字典中没有'height'这个键,则新增#
print(dic)
print(v)
v=dic.setdefault('name','EGON') # 字典中有'name'这个键,不修改字典
print(dic)
print(v)
setdefault的原理分析:
k='name'
v='EGON'
if k not in dic:
dic[k]=v
print(dic)
字典类型总结:
可以存多个值
无序
属于可变类型
集合:
一:引入
x = [1,3,4,5,6,9]
y = [1,3,4,5]
l =[]
for item in x:
if item in y:
l.append(item)
print(l)
二:集合 set
1.用途:
做关系运算
去重
2.定义方式:在{}内用逗号隔开多个元素,多个元素必须满足一下条件
每一个元素必须是不可变类型
集合内元素不能重复
集合无序
s = {1,1,1,'aaa',1,1,1,1,}
print(s) 1,'aaa'
d = {} #默认定义为字典
print(type(d))
s = set
print(s,type(s))
数据类型转换
s = set('hello')
print(t)
3.常用操作+内置的方法
1.关系运算
交集: 共同元素
x = [1,3,4,5,6,9]
y = [1,3,4,5]
res = x & y
print(x) [1,2,3,4,5]
res1 = x.intersection(y)
print(res1)
并集: 所有元素
res = x | y
pring(res)
res1 = x.union(y)
print(res1)
差集 只有 x中的元素
res = x - y
print(res)
res1 = x.difference(y)
print(res1)
res = (x - y)| (y -x)
res = x ^ y
print(res)
print(x.symmetric_difference(y))
子集:包含与被包含关系
s1={1,2,3}
s2={3,1,2}
print(s1 == s2)
s1={1,2,3}
s2={1,2}
print(s1 >= s2)
print(s1.issuperset(s2))
print(s2 <= s1)
print(s2.issubset(s1))
知识补充:
s1={'a','b','cc',2,3}
print(id(s1))
s1.update({3,4,5})
print(s1,id(s1))
s1.add(4)
print(s1)
res=s1.pop()
print(res)
res=s1.remove('b')
print(s1)
print(res)
s1={1,2,3}
s2={3,4,5}
s1.difference_update(s2) # s1=s1-s2
print(s1)
s1.intersection_update(s2) # s1=s1 & s2
print(s1)
s1.symmetric_difference_update(s2) # s1=s1 ^ s2
print(s1)
s1={1,2,3}
s2={4,5,6}
print(s1.isdisjoint(s2))
集合总结:
可以存多个值
无序
是属于可变类型
set也是属于可边类型
去重 :如果去重的对象都是不可变类型并且不要求结果跟原来的顺序一致,那么可以用set来解决
names=['egon','egon','kevin','kevin','alex','alex']
l=[]
for name in names:
if name not in l:
l.append(name)
print(l)
res=set(names)
print(res)
l=list(res)
print(l)
info=[
{'name':'egon','age':18,'sex':'male'},
{'name':'alex','age':73,'sex':'male'},
{'name':'egon','age':20,'sex':'female'},
{'name':'egon','age':18,'sex':'male'},
{'name':'egon','age':18,'sex':'male'},
]
l=[]
for dic in info:
if dic not in l:
l.append(dic)
print(l)
布尔类型;
在条件判断中所有的数据都会转换布尔值
布尔值为假的数据类型即可:0,None,空
if '':
print('runl....')
else:
print('判断为假')
print(bool(0))
print(bool(None))
print(bool(''))
while 1:
print(111)
print(222)
print(3333)