python之数据类型
一、数据类型
数字(整型,长整型,浮点型,复数)
字符窜
列表
元祖
字典
集合
bytes类型
1、数字类型(int float,conmplex(负数))
十进制转二进制的函数是bin(age) ob1010
十进制转八进制oct(age) 0o12
十进制转十六进制 hex(age) Oxa
注意:python3 里已经废弃long类型
2、字符窜
定义:在单引号\双引号\三引号内,由一串字符组成
常用操作:
name='*egon**'
移除空白strip
例子:name.strip()
切分split
name='a|b|c'
print(name.rsplit('|',1)) #从右开始切分
长度len
name='a|b|c'
len(name)
索引
name='abc'
name[1]
切片
name=' aleX'
name[-2:]
name[:-1]
其他操作
#startswith,endswith
name='alex_SB'
print(name.endswith('SB'))
print(name.startswith('alex'))
#replace
name='alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))
#format的三种玩法
res='{} {} {}'.format('egon',18,'male')
res='{1} {0} {1}'.format('egon',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)
#find,rfind,index,rindex,count
name='egon say hello'
print(name.find('o',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
#split
name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #默认分隔符为空格
name='C:/a/b/c/d.txt' #只想拿到顶级目录
print(name.split('/',1))
print(name.count('e',1,3)) #顾头不顾尾,如果不指定范围则查找所有
#join tag=' ' print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串
#center,ljust,rjust,zfill name='egon' print(name.center(30,'-')) print(name.ljust(30,'*')) print(name.rjust(30,'*')) print(name.zfill(50)) #用0填充
#expandtabs name='egon\thello' print(name) print(name.expandtabs(1))
#captalize,swapcase,title print(name.capitalize()) #首字母大写 print(name.swapcase()) #大小写翻转 msg='egon say hi' print(msg.title()) #每个单词的首字母大写
#is数字系列 #在python3中 num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='四' #中文数字 num4='Ⅳ' #罗马数字 #isdigt:bytes,unicode print(num1.isdigit()) #True print(num2.isdigit()) #True print(num3.isdigit()) #False print(num4.isdigit()) #False #isdecimal:uncicode #bytes类型无isdecimal方法 print(num2.isdecimal()) #True print(num3.isdecimal()) #False print(num4.isdecimal()) #False #isnumberic:unicode,中文数字,罗马数字 #bytes类型无isnumberic方法 print(num2.isnumeric()) #True print(num3.isnumeric()) #True print(num4.isnumeric()) #True #三者不能判断浮点数 num5='4.3' print(num5.isdigit()) print(num5.isdecimal()) print(num5.isnumeric()) ''' 总结: 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 如果要判断中文数字或罗马数字,则需要用到isnumeric '''
#is其他
print('===>')
name='egon123'
print(name.isalnum()) #字符串由字母和数字组成
print(name.isalpha()) #字符串只由字母组成
print(name.isidentifier())
print(name.islower())
print(name.isupper())
print(name.isspace())
print(name.istitle())
3、列表
定义:[]内可以有多个任意类型的值,逗号分隔
my_girl_friends=['alex','wupeiqi','yuanhao',4,5] #本质my_girl_friends=list([...])
或
l=list('abc')
常用操作:
索引
list[1]
切片
list[1:2]
追加
list.append()
删除
list.pop()
list.remove()
长度
len(list)
包含in
'alex' in list
其他操作:
insert
my_girl_friends=['alex','wupeiqi','alex','yuanhao',4,10,30]
my_girl_friends.insert(1,'Sb')
clear
my_girl_friends.clear()
copy()
l=my_girl_friends.copy()
count()
my_girl_friends.count('alex')
extend()
my_girl_friends.extend(['oldboy1','oldboy2','oldboy3'])
index()
my_girl_friends.index('alex')
reverse()
my_girl_friends.reverse()
sort()
l.sort(reverse=True)
4、元组
定义:与列表类型,只不过[]换成()
可以当做字典的key
age=(11,22,33,44,55)本质age=tuple((11,22,33,44,55))
常用操作:
索引
age[1]
切片
age[1:2]
长度
len(age)
包含in
11 in age
5、字典
定义:key必须是不可变类型,value可以是任意类型
info={'name':'egon','age':18,'sex':'male'} #本质info=dict({....})
或
info=dict(name='egon',age=18,sex='male')
或
info=dict([['name','egon'],('age',18)])
或
{}.fromkeys(('name','age','sex'),None)
常用操作:
存/取
info[name]
info.get('name1')
info.get('nameasdfasdfasdfasdf','not key')
删除
info.pop('name')
info.pop('asdfsadfasdfasfasdfasdfasdf',None)
长度
len(info)
包含in
'name' in info
popitem()
info.popitem()
clear()
info.clear()
6、集合
作用:去重,关系运算,
定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可以作为字典的key
集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中单个值
常用方法:
in 和 not in
|合集
&交集
-差集
^对称差集
==
>,>= ,<,<= 父集,子集
7、数据类型的总结:
按存值个数区分
| 标量/原子类型 | 数字,字符串 |
| 容器类型 | 列表,元组,字典 |
按可变不可变区分
| 可变 | 列表,字典 |
| 不可变 | 数字,字符串,元组 |
按访问顺序区分
| 直接访问 | 数字 |
| 顺序访问(序列类型) | 字符串,列表,元组 |
| key值访问(映射类型) | 字典 |
浙公网安备 33010602011771号