Python 函数 int(x, base=10)
最新推荐文章于 2024-06-03 01:48:57 发布

YKenan 于 2019-03-05 17:34:36 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
Python 函数 int(x, base=10)
由下面文档给出的可以看出 int([x]) 与 int(x, base=10) 都是 int.__init__ 的一种special case
def __init__(self, x, base=10): # known special case of int.__init__ """ int([x]) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4 # (copied from class doc) """ pass
12345678910111213141516171819'有两个重要的部分如果x不是数字,或者如果给定了基数,那么x必须是表示整数文本的字符串、字节或字节组实例
基本值默认为10。有效基数为0和2-36。基数0表示将字符串中的基解释为整型文本。
x 默认 base = 10base = 0 表示根据自行给出的 x(系统自己识别)得出十进制的结果x 必须是字符串、字节或字节组int(x, base) -> integer 无论 base 等于几,得出来的都是十进制的整数,也就是base 中的几进制是限制 x 的,通过此运算得出对应的十进制例如上面的例子:
>>> int('0b100', base=0) 4 12
0b 代表二进制的意思,base = 0,由于有 0b 系统自己可以识别出是二进制,故输出的是 4
若没有 0b 哪
>>> int('100', base=0) 4 123

这样就一目了然了
相关知识
Python学习第一期
Python学习笔记(3)——input函数
python中 sort() 函数
Python小练习
太原理工大学python考试题总结
python学习总结day2
#define add(x) x*x main(){int a=4,b=6,c=7,d=add(a+b)*c;printf(“d=%d ”, d);}怎样分析?
Python之函数
设三个变量x=1,y=2,z=3,则表达式y+=z
Python笔试题
原文链接: Python 函数 int(x, base=10) https://m.mcbbbk.com/newsview759409.html
| 上一篇: C/C++中的结束输入条件判断 ... | 下一篇: 设有一个线性表E = { e1,... |




