新手必看的Python整型的实例操作

在这里插入图片描述

说到Python整型,对于初学者可能有点陌生,今天就给大家推荐Python整型的实例操作。
1、整型、

【例子】通过 print() 可看出 a 的值,以及类 (class) 是int。
a = 1031print(a, type(a))# 1031 <class ‘int’>
Python 里面万物皆对象(object),整型也不例外,只要是对象,就有相应的属性 (attributes) 和方法(methods)。

【例子】
b = dir(int)print(b)# [‘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’,# ‘bit_length’, ‘conjugate’, ‘denominator’, ‘from_bytes’, ‘imag’,# ‘numerator’, ‘real’, ‘to_bytes’]
对它们有个大概印象就可以了,具体怎么用,需要哪些参数 (argument),还需要查文档。看个bit_length()的例子。

【例子】找到一个整数的二进制表示,再返回其长度。
a = 1031print(bin(a)) # 0b10000000111print(a.bit_length()) # 11

以上就是关于Python整型的实例操作的全部介绍了,大家可以通过以上方法进行学习操作。
文章部分内容源于网络,联系侵删*
文章转自:http://h.jiguangdaili.com/news/92652.html

猜你喜欢

转载自blog.csdn.net/zhimaHTTP/article/details/113744418