Python 数值类型方法|内建函数的对比汇总 (int bool float complex bytes str)

函数 dir(object) 用于查找对象的属性和方法:

>>> dir(int)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', 
'__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', 
'__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', 
'__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', 
'__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', 
'__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', 
'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', 
'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', 
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 
'__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 
'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

各数值类型的方法之对比异同

>>> iList = dir(int)
>>> bList = dir(bool)
>>> fList = dir(float)
>>> iList == bList
True
>>> iList == fList
False

注:其中int和bool的dir()返回值列表完全相同 

列印各数值类型的方法列表

>>> typeList = ['int','bool','float','complex','bytes','str']
>>> for t in typeList:
	print(t+':')
	for i in range(len(dir(eval(t)))):
		print(dir(eval(t))[i])
	print()

注:比较结果:int 和 bool 的列表完全一致,float、complex与int比较接近;bytes与str比较接近。

类型 int、float、complex 方法的比较

intfloatcomplex注释
__pos____pos____pos__一元运算正号 +
__neg____neg____neg__一元运算负号 -
__abs____abs____abs__abs() 绝对值
__add____add____add__加法 +
__sub____sub____sub__减法 -
__mul____mul____mul__乘法 *
__truediv____truediv____truediv__除法 /
__floordiv____floordiv____floordiv__整除 //
__mod____mod____mod__取余 %
__pow____pow____pow__pow() 即**
__rmod____rmod____rmod__当右操作数支持此运算,而左操作数不支持此运算时使用
__divmod____divmod____divmod__同上
__rdivmod____rdivmod____rdivmod__同上
__radd____radd____radd__同上
__rsub____rsub____rsub__同上
__rmul____rmul____rmul__同上
__rpow____rpow____rpow__同上
__rtruediv____rtruediv____rtruediv__同上
__rfloordiv____rfloordiv____rfloordiv__同上
__eq____eq____eq__x==y
__ne____ne____ne__x!=y
__ge____ge____ge__x>=y
__le____le____le__x<=y
__gt____gt____gt__x>y
__lt____lt____lt__x<y
__int____int____int__强制转换类型
__float____float____float__强制转换类型
__bool____bool____bool__强制转换类型
__str____str____str__强制转换类型
__repr____repr____repr__同__str__ (在老版本中转换浮点数时精度不同)
__format____format____format__ 
__init____init____init__ 
__class____class____class__返回数据类型的初始空值
__init_subclass____init_subclass____init_subclass__ 
__subclasshook____subclasshook____subclasshook__ 
__new____new____new__创建实例
__hash____hash____hash__ 
__doc____doc____doc__ 
__sizeof____sizeof____sizeof__ 
__dir____dir____dir__dir()的子集
__reduce____reduce____reduce__ 
__reduce_ex____reduce_ex____reduce_ex__ 
__getattribute____getattribute____getattribute__ 
__setattr____setattr____setattr__ 
__delattr____delattr____delattr__ 
conjugateconjugateconjugate共轭复数
realrealreal实部
imagimagimag虚部
as_integer_ratioas_integer_ratio  
__round____round__ 四舍五入取整
__trunc____trunc__ 舍尾取整
__getnewargs__ __getnewargs__ 
__index__   
__rand__  随机数
__ceil__  向下取整
__floor__  向上取整
__invert__  一元运算~
__or__  或运算 |
__and__  与运算 &
__xor__  异或运算 ^
__lshift__  左移运算 <<
__rshift__  右移运算 >>
__ror__  当右操作数支持此运算,而左操作数不支持此运算时使用
__rxor__  同上
__rlshift__  同上
__rrshift__  

同上

from_bytes   
to_bytes   
bit_length   
numerator   
denominator   
 hex  
 fromhex  
 is_integer  
 __set_format__  
 __getformat__ 

注:黑色字体部分的方法三者都有且类同,蓝色部分是其中两者共有的,红色则是一方所特有的方法。 __开头的方法为私有函数,一般情况下不使用。

类型 str、 bytes 方法的比较

strbytes注释或举列
capitalizecapitalize首字符大写
centercenter在串的两头填充字符,默认用空格
countcount返回子串的个数
endswithendswith是否存在后缀子串
expandtabsexpandtabs扩展 \t 字符的空格数,默认为8
findfind返回子串的位置索引
indexindex返回子串的位置索引
isalnumisalnum判断是否字母或数字
isalphaisalpha判断是否字母
isasciiisascii判断是否字母
isdigitisdigit判断是否字母
islowerislower判断是否小写
isspaceisspace判断是否空格
istitleistitle判断串中各单词(空格分隔的)是否首字母大写
isupperisupper判断是否大写
joinjoin--
ljustljust--
lowerlower全串的字母都转小写
lstriplstrip只消除串左边的空格
maketransmaketrans创建字符映射转换表,与translate搭配使用
partitionpartition--
replacereplace替换掉部分子串
rfindrfindr+方法名的都为右向操作
rindexrindex--
rjustrjust--
rpartitionrpartition--
rsplitrsplit--
rstriprstrip只消除串右边的空格
splitsplit用给定分隔符分割字符串
splitlinessplitlines用换行符(\r,\n,\r\n)分割字符串
startswithstartswith是否存在前缀子串
stripstrip消除串左右两边的空格
swapcaseswapcase串中各字母的大小写反转
titletitle标题化串,把串的所有单词(空格分隔的)首字母转大写
translatetranslate根据maketrans定义的转换表转换串中的字符
upperupper全串的字母都转大写
zfillzfill在串左边用0填充
casefold 
encode 
format 
format_map 
isdecimal 
isidentifier 
isnumeric 
isprintable 
 decode
 fromhex
 hex

注:黑色字体部分的方法与int类型的类同,蓝色部分是str和bytes两者共有但是int等类型没有的,红色则是str或bytes一方所特有的。

具体说明见下回分晓......

https://blog.csdn.net/boysoft2002/article/details/115427777

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值