python 基础数据类型汇总

数据类型小结(各数据类型常用操作) 一.数字/整型int

int()强行转化数字

二.bool类型False&True

bool()强行转化布尔类型.

0,None,及各个空的字符类型为False.其余均为Ture.

三.字符串str

str()强行转化字符串

#列表转化字符换 nums = [11,22,33,44] for a in range(0,len(nums)): nums[a] = str(nums[a]) result = \'\'.join(nums) print(result)

.upper()转化大写

name = \'abc\' new_name = name.upper() print(new_name)

.lower()转化小写

name = \'ABC\' new_name = name.lower() print(new_name)

.replace()替换

message = input(\'请输入"大傻逼"\') new_mes = message.replace(\'傻逼\',\'**\') print(new_mes) #.replace(\'原字符\',\'替换字符\',\'替换个数\')

.strip()去首尾空格

name = \' abc \' new_name = name.strip() print(new_name) #.rstrip()去除右侧空格 .lstrip()去除左侧空格

.split()分割

name = \'abcdefg\' new_name = name.split(\'d\') print(new_name) #.split(\'分割点\',分割次数)

.isdecimal()判断是否可以转化位数字

while True: num = input(\'请输入内容:\') num1= num.isdigit() #print(num1) 数字的话输出True,非数字输出FALSE if num1: print(\'你输入正确\') break else: print(\'请输入数字\')

.startswith() 判断是否以()开头,输出值为bool类型

name = \'bigox\' print(name.startswith(\'big\'))

endswith() 判断是否以()结尾,输出值为bool类型 同.startswith()用法相同

.format()格式(同字符串格式化)

name = \'我叫:{0},年龄:{1}\'.format(\'bigox\',24) print(name)

.encode() :编码转换

name = \'刘\' #解释器读取到内存后,按照unicode编码存储:8字节. print(name.encode(\'utf-8\')) #转化为utf-8编码

.join()循环每个元素,并在元素之间加入连接符.

name = \'bigox\' new_name = \'_\'.join(name) print(new_name) #输出结果为 b_i_g_o_x

四.列表list

列表转换list()

#列表转化字符换 nums = [11,22,33,44] for a in range(0,len(nums)): nums[a] = str(nums[a]) result = \'\'.join(nums) print(result)

.pop(索引)

a = li.pop(2) #在列表中删除,并将删除的此数据赋值给a

name = [\'bigox\',\'xo\',\'ox\'] name.pop(1) print(name)

del 列表 [索引]

name = [\'bigox\',\'xo\',\'ox\'] del name[0:2] print(name)

.append()列表最后追加元素

lst = ["麻花藤", "林俊杰", "周润发", "周芷若"] print(lst) lst.append("wusir") print(lst)

.insert()在指定的索引位置插入元素

lst = ["麻花藤", "林俊杰", "周润发", "周芷若"] print(lst) lst.insert(1,"wusir") print(lst)

remove()**指定元素删除

name = [\'bigox\',\'xo\',\'ox\'] name.remove(xo) print(name)

.clear()**清空

.extend**()添加

li = ["alex", "WuSir", "ritian", "barry", "wenzhou"] s = \'qwert\' li.extend(s) print(li) #--------------------------- lst = ["王志文", "张一山", "苦海无涯"] lst.extend(["麻花藤", "麻花不疼"]) print(lst)

.reverse()反转

v = [1,2,3,4,5,6] v.reverse() print() #[6, 5, 4, 3, 2, 1]

.sort排序

v = [1,3,7,4,5,6] v.sort() print() #[1, 3, 4, 5, 6, 7] #v.sort() 从小到大排序(默认) #v.sort(reverse=True) 从大到小排序

五.元组tuple

强制转换:

tuple(\'adfadfasdfasdfasdfafd\')

v1 = tuple(\'adfadfasdfasdfasdfafd\') print(v1) # (\'a\', \'d\', \'f\', \'a\', \'d\', \'f\', \'a\', \'s\', \'d\', \'f\', \'a\', \'s\', \'d\', \'f\', \'a\', \'s\', \'d\', \'f\', \'a\', \'f\', \'d\')

tuple([11,22,33,44])

v1 = tuple([11,22,33,44]) print(v1) # (11, 22, 33, 44)

元组子元素不可变,而子元素内部的子元素是可以变的,取决于元素是否为可变对象

元组中如果只有一个元素,一定要添加一个逗号,否者不是元组

六.字典dict

字典键的数据类型不能为list和tuple,值可以为任何类型.

.keys()取键

for i in info.keys(): print(i)

.values()取值

for i in info.values(): print(i)

.items()取键值对

for i in info.items(): print(i)

.get()以键取值,如果键不存在返回原定结果

info = {\'k1\':\'v1\',\'k2\':\'v2\'} a = info.get(\'k1\') print(a) #v1 info2 = [\'11111\'] b = info.get(\'11111\',22222) print(b) #22222

.update()更新_存在覆盖更新,不存在添加

info = {\'k1\':\'v1\',\'k2\':\'v2\'} info.update({\'k1\':\'v0\',\'k3\':\'v3\'}) print(info) #{\'k1\': \'v0\', \'k2\': \'v2\', \'k3\': \'v3\'}

七.集合set

无序,不可重复

在集合中True与数字1重复,False与数字0重复

.add() 添加

info = {\'k1\',\'k2\'} info.add(\'k3\') print(info)

.discard()删除

info = {\'k1\',\'k2\',\'k3\'} info.discard(\'k3\') print(info)

要修改,需要先删除再添加

.clear()清空

.update()

info = {\'k1\',\'k2\'} info.update({\'k1\',\'k3\',\'v3\'}) print(info) #{\'k1\',\'k2\',\'k3\',\'v3\'}

.intersection() 交集

命令后的 () 可以是集合,也可以是列表.

info = {1,2,3} print(info.intersection({1,3,4})) #{1,3}

.union()并集

info = {1,2,3} print(info.union({1,3,4})) #{1,2,3,4}

.difference()差集

info = {1,2,3} print(info.union({1,3,4})) #{2,4}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzdxgj.html