python int_Python int()

python int

Python int() function returns an integer object from the specified input. The returned int object will always be in base 10.

Python int()函数从指定的输入返回一个整数对象。 返回的int对象将始终以10为底。

Python int() (Python int())

Python int() function syntax is:

Python int()函数语法为:

class int(x=0, base=10)

Some important points for int() function are:

int()函数的一些重要点是:

  • If no argument is provided, 0 will be returned by the int() function.

    如果未提供任何参数,则int()函数将返回0。
  • If integer literal argument is provided, base should not be provided. The returned int object will be in decimal even if the argument is in hexadecimal, binary or any other base.

    如果提供整数文字参数,则不应提供base。 即使参数为十六进制,二进制或任何其他基数,返回的int对象也将为十进制。
  • For floating point argument, decimal point will be truncated and int value will be returned. There will be no rounding performed.

    对于浮点参数,小数点将被截断,并且将返回int值。 不会进行四舍五入。
  • If the argument is string, it will be converted to int. If the string argument is not in base 10, then base must be provided.

    如果参数是string ,它将被转换为int。 如果字符串参数不在10以底,则必须提供base。
  • Base argument can be provided for string, bytes and bytearray argument only. The allowed values are 0 and 2 to 36.

    只能为字符串, 字节字节 数组参数提供基本参数。 允许值为0和2到36。
  • We can use int() with custom object too, in that case object __int__() function will be called. If __int__() function is not defined, then __trunc__() function will be called. If none of them are defined, then TypeError will be thrown.

    我们也可以将int()与自定义对象一起使用,在这种情况下,将调用对象__int __()函数。 如果未定义__int __()函数,则将调用__trunc __()函数。 如果未定义它们,则将TypeError

Let’s look into int() function examples with different types of input arguments.

让我们看一下具有不同类型输入参数的int()函数示例。

Python int()与数字 (Python int() with numbers)

x = int()
print(type(x))
print(x)

print(int(0xF))
print(int(0b111))

Output:

输出:

<class 'int'>
0
15
7

带有浮点数的Python int() (Python int() with float)

x = int(10.043)
print(x)

x = int(10.8901)
print(x)

Output:

输出:

10
10

Notice that integer part of floating point number is returned, no rounding is performed.

请注意,返回浮点数的整数部分,不进行舍入。

带有字符串的Python int() (Python int() with string)

x = int("5")
print(x)

x = int("-0xf", base=16)
print(x)

x = int("0b111", base=2)
print(x)

Output:

输出:

5
-15
7

带有字节和字节数组的Python int() (Python int() with bytes and bytearray)

x = int(bytes("-0xf", "utf-8"), 16)
print(x)

x = int(bytearray("-20", "utf-8"))
print(x)

Output:

输出:

-15
-20

带有自定义对象的Python int() (Python int() with custom object)

class Emp:
    id = 0

    def __int__(self):
        print('__int__ function called')
        return self.id

    def __trunc__(self):
        print('__trunc__ function called')
        return self.id


x = Emp()
x.id = 100
print(int(x))

Output:

输出:

__int__ function called
100

If we comment __int__ function, then the output will be:

如果我们注释__int__函数,那么输出将是:

__trunc__ function called
100

If we comment both __int__ and __trunc__ functions, then we get the following error.

如果我们同时评论__int__和__trunc__函数,则会收到以下错误。

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Emp'

摘要 (Summary)

Python int() function is used to convert string, bytes, bytearray and objects to an int object. The integer is always returned in base 10. We can get the same value by directly calling object.__int__() function.

Python int()函数用于将字符串,字节,字节数组和对象转换为int对象。 整数总是以10为底返回。我们可以通过直接调用object.__int__()函数获得相同的值。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22929/python-int

python int

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值