python怎么定义一个整型_新手必看的Python整型的实例操作

a7a0a3f2f9624eb31602e840d73b7b05.png

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

1、整型、

【例子】通过 print() 可看出 a 的值,以及类 (class) 是int。

a = 1031print(a, type(a))# 1031

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

原文链接:https://blog.csdn.net/zhimaHTTP/article/details/113744418

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值