python 中的int()函数怎么用


Posted in Python onOctober 17, 2017

int(x, [base])

功能:

函数的作用是将一个数字或base类型的字符串转换成整数。

函数原型:

int(x=0)
int(x, base=10),base缺省值为10,也就是说不指定base的值时,函数将x按十进制处理。

适用Python版本:

Python2.x
Python3.x

注意:

1. x 可以是数字或字符串,但是base被赋值后 x 只能是字符串
2. x 作为字符串时必须是 base 类型,也就是说 x 变成数字时必须能用 base 进制表示

Python英文文档解释:

class int(x=0)
class int(x, base=10)
Return an integer object constructed from a number or string x, 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 radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2?36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
The integer type is described in Numeric Types — int, float, complex.
Changed in version 3.4: If base is not an instance of int and the base object has a base.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.
Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.

代码实例:

1. x 是数字的情况:

int(3.14)      # 3
int(2e2)       # 200
int(100, 2)     # 出错,base 被赋值后函数只接收字符串

2. x 是字符串的情况:

int('23', 16)   # 35
int('Pythontab', 8)   # 出错,Pythontab不是个8进制数

3. base 可取值范围是 2~36,囊括了所有的英文字母(不区分大小写),十六进制中F表示15,那么G将在二十进制中表示16,依此类推....Z在三十六进制中表示35

int('FZ', 16)   # 出错,FZ不能用十六进制表示
int('FZ', 36)   # 575

4. 字符串 0x 可以出现在十六进制中,视作十六进制的符号,同理 0b 可以出现在二进制中,除此之外视作数字 0 和字母 x

int('0x10', 16) # 16,0x是十六进制的符号
int('0x10', 17) # 出错,'0x10'中的 x 被视作英文字母 x
int('0x10', 36) # 42804,36进制包含字母 x

总结

以上所述是小编给大家介绍python 中的int()函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
python封装对象实现时间效果
Apr 23 Python
python实现查找excel里某一列重复数据并且剔除后打印的方法
May 26 Python
python中PIL安装简单教程
Apr 21 Python
Python中字符串与编码示例代码
May 20 Python
python Tcp协议发送和接收信息的例子
Jul 22 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
Aug 13 Python
使用Python代码实现Linux中的ls遍历目录命令的实例代码
Sep 07 Python
python破解bilibili滑动验证码登录功能
Sep 11 Python
如何基于python操作json文件获取内容
Dec 24 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
Feb 15 Python
使用pygame实现垃圾分类小游戏功能(已获校级二等奖)
Jul 23 Python
OpenCV图片漫画效果的实现示例
Aug 18 Python
python遍历序列enumerate函数浅析
Oct 17 #Python
浅谈python中的正则表达式(re模块)
Oct 17 #Python
深入理解Django的自定义过滤器
Oct 17 #Python
Python引用类型和值类型的区别与使用解析
Oct 17 #Python
利用python批量修改word文件名的方法示例
Oct 17 #Python
Django内容增加富文本功能的实例
Oct 17 #Python
Python通过future处理并发问题
Oct 17 #Python
You might like
php生成百度sitemap站点地图类函数实例
2014/10/17 PHP
PHP实现将视频转成MP4并获取视频预览图的方法
2015/03/12 PHP
合格的PHP程序员必备技能
2015/11/13 PHP
php轻松实现文件上传功能
2016/03/03 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
2018/02/07 PHP
jquery 模拟类搜索框自动完成搜索提示功能(改进)
2010/05/24 Javascript
利用jQuery操作对象数组的实现代码
2011/04/27 Javascript
关于jQuery的inArray 方法介绍
2011/10/08 Javascript
AngularJS入门教程中SQL实例详解
2016/07/27 Javascript
JS实现页面数据无限加载
2016/09/13 Javascript
用js制作淘宝放大镜效果
2020/10/28 Javascript
JS日程管理插件FullCalendar简单实例
2017/02/07 Javascript
Vue.js学习教程之列表渲染详解
2017/05/17 Javascript
vue.js动态数据绑定学习笔记
2017/05/19 Javascript
让你彻底掌握es6 Promise的八段代码
2017/07/26 Javascript
让bootstrap的carousel支持滑动滚屏的实现代码
2017/11/27 Javascript
解决vue中使用swiper插件问题及swiper在vue中的用法
2018/04/04 Javascript
Node.js事件的正确使用方法
2019/04/05 Javascript
Layui数据表格 前后端json数据接收的方法
2019/09/19 Javascript
关于vue.js中实现方法内某些代码延时执行
2019/11/14 Javascript
JavaScript实现横版菜单栏
2020/03/17 Javascript
vue $mount 和 el的区别说明
2020/09/11 Javascript
举例详解Python中的split()函数的使用方法
2015/04/07 Python
Python编程中的异常处理教程
2015/08/21 Python
Python学习pygal绘制线图代码分享
2017/12/09 Python
Python数据分析:手把手教你用Pandas生成可视化图表的教程
2018/12/15 Python
PyCharm 2020.2 安装详细教程
2020/09/25 Python
加拿大领先的冒险和户外零售商:Atmosphere
2017/12/19 全球购物
Myprotein芬兰官网:欧洲第一运动营养品牌
2019/05/05 全球购物
Fabletics官网:美国运动服饰品牌,由好莱坞女演员凯特·哈德森创立
2019/10/19 全球购物
介绍一下linux的文件权限
2014/07/20 面试题
领导干部“四风”问题批评与自我批评材料
2014/09/24 职场文书
有限责任公司股东合作协议书
2014/12/02 职场文书
爱情保证书
2015/01/17 职场文书
小学教师见习总结
2015/06/23 职场文书
党风廉洁教育心得体会
2016/01/20 职场文书