python中各种数据类型

时间:2023-03-09 09:10:02
python中各种数据类型

数字类型

整型int

  作用:年纪,等级,身份证号,qq号等与整型数字有关

  定义: 

 age=10 #本质age=int(10)

浮点型float

  作用:薪资,身高,体重等与浮点数相关

salary=3.1#本质salary=float(3.1)

该类型总结

只能存一个值

不可变类型

x=10.3
print(id(x))
x=10.4
print(id(x))

字符串类型

1.用途:记录描述性的状态,比如人的名字、地址、性别

2.定义方式:在“”,‘’,“”“”“”内包含一系列的字符

msg='hello'#msg=str(hello)

3.常用操作+内置的方法

优先掌握的操作:

1)按索引取值(正向取+反向取):只能取

msg='hello world'
print(msg[4])
print(msg[-1])

2)切片(顾头不顾尾,即前闭后开区间)

msg='hello world'
print(msg[0:2])
print(msg)#没有改变原值
print(msg[0:5:2])
print(msg[0:-1:2])

3)长度len

msg='hello world'
print(len(msg))

4)成员运算in和not in:判断一个子字符串是否存在一个大的字符串中

msg='hello world'
print('he' in msg)
print('alex' in msg)

5)去掉字符串左右两边的字符strip,不管中间的

user='    egon     '
print(user.strip())
user="*******egon********"
print(user.strip('/*'))

user=input('>>:').strip()

6)切分split:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值

msg="root:123456:0:0::/root:/bin/bash"
res=msg.split(':')
print(res[0])

7)循环

msg='hello'
for i in msg:
    print(i)

需要掌握的

1)strip,lstrip,rstrip

print('*****egon*****'.lstrip('*'))
print('*****egon*****'.rstrip('*'))
print('*****egon*****'.strip('*'))

2)lower,upper

msg='aABBBBb'
res=msg.lower()
print(res)
print(msg)

3)startwith,endwith

msg='alex is dsb'
print(msg.startswith('alex'))
print(msg.startswith('xuxu'))
print(msg.endswith('sb'))
print(msg.endswith('gua'))

4)format的三种玩法

print('my name is {name} my age is {age}'.format(name='alex',age=18))
print('my name is {} my age is {}'.format('alex',18))
print('my name is {0}{0} my age is {1}'.format('alex',18))

5)split,rsplit

msg='get|a.txt|333331231'
# print(msg.split('|',1))
print(msg.split('|',1))
print(msg.rsplit('|',1))

6)join

msg='get|a.txt|333331231'
l=msg.split('|')
print(l)
src='|'.join(l)
print(src)
l=['a','b','c']
l2=('d','e','f')
print('|'.join(l))
print('>'.join(l2))

7)replace替换

msg='alex say i have one tesla,alex is alex hahaha'
print(msg.replace('alex','sb'))

8)isdigit判断是否为纯数字

print('10.1'.isdigit())
'.isdigit())

需要了解的内置方法

1)find,rfind,index,rindex,count

msg='hello alex is sb'
print(msg.find('alex'))
print(msg.find('alex',0,3))
print(msg.index('alex'))
print(msg.index('alex',0,3))
res='alex love alex'
print(res.count('alex'))

2)center,ljust,rjust,zfill

print('egon'.center(50,"*"))
print('egon'.ljust(50,"*"))
print('egon'.rjust(50,"*"))
print('egon'.zfill(50))

3)capatalize,swapcase,title

print('hello'.capitalize())#首字母大写
print('hElLo'.swapcase())#大小写互换
print('egon is nb'.title())#每个单词首字母大写

4)is系列

'.isdigit())#字符串是否为纯数字
print('10.1'.isdigit())
print('abc你'.isalpha())# 字符串中包含的是字母或者中文字符
print('abc12'.isalnum())# 字符串中包含的是字母(中文字符)或数字

强调:

1)字符串之间可以相加,但是必须是字符串与字符串相加,其他类型数据则报错

msg1='hello'
msg2='world'
print(msg1+msg2)

2)字符串还可以做乘法

msg1='hello'
print(msg1*10)

3)字符串相加是新申请内存空间然后拷贝相加的字符串到新的内存中,效率不高,尽量避免这么使用

该类型总结

1.存一个值

2.有序

3.不可变

列表类型

1.作用:记录/存多个值,可以方便的取出指定位置的值

2.定义:在[]内用逗号分隔开多个任意类型的值

l=[10,3.1,'egon',['a','b'],{'name':'lxx','age':18},None]# l=list([10,3.1,'egon',['a','b']])
print(l[0])
print(l[3][1])

3.常用操作+内置方法

优先掌握的操作

1)按索引取值(正向存取+反向存取):既可存也可以取

l=['egon','lxx','yxx']
print(l[0])
print(l[-2])

2)切片(顾头不顾尾)

l=['egon','lxx','yxx',444,555,66666]
print(l[0:2])
print(l[::2])

3)长度

l=['egon','lxx','yxx',444,555,66666,[1,2,3]]
print(len(l))

4)成员运算in和not in

l=['egon','lxx','yxx',444,555,66666,[1,2,3]]
print('egon' in l)
print(88 in l)

5)追加

l=['egon','lxx','yxx']#只能追加一项
l.append('alex')
print(l)

6)往指定索引前插入值

l=['egon','lxx','yxx']
l.insert(1,'dsb')
print(l)

7)删除

方式1:

l=['egon','lxx','yxx']#单纯的删除值,没有返回值
del l[0]
print(l)

方式2:

l=['egon','lxx','yxx']
res=l.remove('lxx')#指定要删除的值,返回的值是None
print(res)

方式3:

l=['egon','lxx','yxx',11,22]
res1=l.pop(0)#从列表中拿走一个值,返回值是被删除的值
res2=l.pop()#默认是从末尾开始删
print(res1,res2,l)

8)循环

l=['egon','lxx','yxx']
for item in l:
    print(item)

需要掌握的操作

1)extend添加多项

items=['a','b','c']
s=['alex','lxx']
items.extend(s)
print(items)

2)reverse倒序

l=['egon','egon','lxx','yxx',444,555,66666]
l.reverse()
print(l)
print()

3)sort排序,不能用于不同类型的数据,否则报错

items=[1,5,2]
items.sort()
print(items)

4)

l=['egon','egon','lxx','yxx',444,555,66666]
print(l.count('egon'))
print(l.index('egon'))#查找不到则会报错
l.clear()#清空列表内容
print(l)

该类型总结

1.存多个值

2.有序

3.可变

l=['a','b','c']
print(id(l))
l.append('d')
print(id(l))

元组类型

1.用途:记录多个值,当多个值没有改的需求,此时用元组更合适(元组中的元素的内存,即id值不能改变)

2.定义方式:在()内用逗号分隔开多个任意类型的值

t=(1,1.3,'xx',('a','b'),[1,2]) 

3.常用操作+内置方法

优先掌握的操作:

基本和列表类似

该类型总结:

1.存多个值

2有序

3不可变

补充:不可变类型==》可hash类型

可变类型===》不可hash类型

字典类型

1.用途:记录多个值,每一个值都对应的key用来描述value的作用

2.定义方式:在{}内用逗号分隔开多个key:value,其中value可以是任意类型,但是key必须是不可变类型,通常情况下应该是str类型

dic={0:'aaa',1:'bbb',2:'cccc'} #dic=dict({0:'aaa',1:'bbb',2:'cccc'})
print(dic,type(dic))
print(dic[0])

用法

#用法一:
dic=dict(x=1,y=2,z=3)
print(dic)

#用法二:
userinfo=[
    ['name','egon'],
    ['age',18],
    ['sex','male']
]

3.常用操作+内置方法

优先掌握的操作

1)按key存取值

dic={'name':'egon'}
print(dic['name'])

2)长度len

dic={'name':'egon','age':18,'name':'EGON','name':'XXXX'}
print(dic)
print(len(dic))

3)成员运算in和not in:字典的成员运算判断的是key

dic={'name':'egon','age':18,}
print(18 in dic)
print('age' in dic)

4)删除

dic={'name':'egon','age':18,}
# 通用
del dic['name']#没有返回值
print(dic)
del dic['xxx'] ##key不存在则报错

使用pop删除

dic={'name':'egon','age':18,}
# res=dic.pop('aa')#不存在的key则报错
res=dic.pop('name')#返回值为对应的value
print(res,dic)

使用popitem删除

di={'name':'alex','age':18,'addr':'beijing'}
res=di.popitem()#随机删除一项,返回值为key:value字典
print(res,di)#工作时如果遇到需要逐一删除项的工作,用popitem方法效率很高

5)键keys(),值values(),键值对items()

dic={'name':'egon','age':18,}
print(dic.keys(),dic.values(),dic.items())

6)循环

dic={'name':'egon','age':18,'sex':'male'}
for k in dic.keys():
    print(k,dic[k])

7)get

dic={'name':'egon','age':18,'sex':'male'}
print(dic.get('name'))#返回name对应的value
print(dic.get('aa'))#没有对应的value,返回None

需要掌握的操作

1)fromkeys的用法

l=['name','age','sex']
dic = {}
dic=dic.fromkeys(l)
print(dic)

2)update

old_dic={'name':'egon','age':18,'sex':'male'}
new_dic={'name':'EGON','x':1,'y':2}
old_dic.update(new_dic)
print(old_dic)

4)setdefault:有则不动/返回原值,无则添加/返回新值

dic={'name':'egon','age':18}
res1=dic.setdefault('name','EGON') # 字典中已经存在key则不修改,返回已经存在的key对应的value
print(res1,dic)
res2=dic.setdefault('addr','shanghai')
print(res2,dic)

该类型总结

1.存多个值

2.无序

3.可变

集合类型

1.用途:关系运算,去重

2.定义方式:在{}内用逗号分开的多个值

集合的三大特性:

2.1每一个值都必须是不可变类型

2.2元素不能重复

2.3集合内元素无序

3.常用操作+内置方法

pythons={'李二丫','张金蛋','李银弹','赵铜蛋','张锡蛋','alex','oldboy'}
linuxs={'lxx','egon','张金蛋','张锡蛋','alex','陈独秀'}
# 取及报名python课程又报名linux课程的学员:交集
print(pythons & linuxs)
# print(pythons.intersection(linuxs))

# 取所有报名老男孩课程的学员:并集
print(pythons | linuxs)
# print(pythons.union(linuxs))

# 取只报名python课程的学员: 差集
print(pythons - linuxs)
# print(pythons.difference(linuxs))

# 取只报名linux课程的学员: 差集
print(linuxs - pythons)
# print(linuxs.difference(pythons))

# 取没有同时报名两门课程的学员:对称差集
print(pythons ^ linuxs)
# print(pythons.symmetric_difference(linuxs))

是否相等

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

子集

# 子集
s1={1,2,3}
s2={1,2}
print(s2 <= s1)
print(s2.issubset(s1))

需要掌握的操作

1)update有则不变,无则添加

s1={1,2,3}
s1.update({3,4,5})
print(s1)

2)删除

pop

s1={1,2,3}
res=s1.pop()#无序删除,返回值为删除的值
print(res)

remove

s1={1,2,3}
res=s1.remove(3) #单纯的删除,返回值为None
print(res,s1)

discard

s1={1,2,3}
res=s1.discard(3) ##单纯的删除,返回值为None
print(s1)
print(res)

remove在移除一个不存在的元素时会报错,但是discard不会报错

3)添加add

s1={1,2,3}
s1.add(4)
print(s1)

4)diffrence

s1={1,2,3}
s2={1,2}
s1.difference_update(s2) #s1=s1.difference(s2)
print(s1)

5)isdisjoint判断是否有交集

s1={1,2,3}
s2={1,2,4}
s3={'a','b'}
print(s1.isdisjoint(s2)) #如果两个集合没有交集则返回True
print(s1.isdisjoint(s3))

该类型总结

1.存多个值

2.无序

3.可变

集合去重的局限性

1)无法保证数据类型的顺序

2)里面的值必须全部是不可变类型