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 = 0def __int__(self):print('__int__ function called')return self.iddef __trunc__(self):print('__trunc__ function called')return self.idx = 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

python int_Python int()相关推荐

  1. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  2. Python字符串isdigit()

    Python String isdigit() function returns True if all the characters in the string are digits, otherw ...

  3. Python字符串格式()

    Python String format() function is used to create a formatted string from the template string and th ...

  4. python中map()函数使用,数据类型转换

    python中map()函数进行数据转换 用法: map(function, iterable, -), 返回的是map型,(ps:python2中返回的是list型可以直接显示,但在python3中 ...

  5. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  6. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  7. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  8. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  9. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

最新文章

  1. .NET Core微服务之基于Consul实现服务治理(续)
  2. 《CLR Via C# 第3版》笔记之(十九) - 任务(Task)
  3. Netty方法误解ChannelHandlerContext.writeAndFlush(Object msg)
  4. (转)双系统卸载Ubuntu
  5. vc中ASSERT()和VERIFY()区别
  6. linux培训机构 网络班,Linux基础教程之网络基础知识与Linux网络配置
  7. tooctalstring_Java Integer类toOctalString()方法的示例
  8. CF888E Maximum Subsequence(meet in the middle)
  9. 在斯坦福,做 Manning 的 phd 要有多强?
  10. python colorama模块
  11. 异常处理:try-catch-finally与throws的区别及使用情况
  12. Apache Zookeeper入门1
  13. Dart学习笔记01:环境搭建与开发环境配置
  14. Linux下最快速共享目录的方法
  15. RQNOJ 34 紧急援救
  16. Vue3项目运行时报错,提示Use // eslint-disable-next-line to ignore the next line.
  17. TensorFlow练习6: 基于WiFi指纹的室内定位(autoencoder)
  18. 第九章 python 字典(Dict)
  19. 20190121——不羡神仙 Java抽象工厂模式
  20. 和python高级知识分子的风骨_侯伯宇 一个高级知识分子的风骨

热门文章

  1. Split的使用(C#)
  2. [转载] python下载安装教程
  3. Oracle:Authid Current_User使用
  4. NumPy 简介及安装
  5. SignalR 跨域解决方案全面
  6. Charles抓包工具过滤网络请求
  7. hadoop深入学习之SequenceFile
  8. 最新出炉程序猿使用说明书
  9. 在mysql的操作界面中,如何清屏幕
  10. 编程之美 4.6桶中取黑白球