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中有趣在__call__函数
Jun 21 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
Nov 07 Python
利用Anaconda完美解决Python 2与python 3的共存问题
May 25 Python
python 设置文件编码格式的实现方法
Dec 21 Python
python采集微信公众号文章
Dec 20 Python
python实现计数排序与桶排序实例代码
Mar 28 Python
Python定时任务随机时间执行的实现方法
Aug 14 Python
利用PyCharm操作Github(仓库新建、更新,代码回滚)
Dec 18 Python
python使用Geany编辑器配置方法
Feb 21 Python
基于Python计算圆周率pi代码实例
Mar 25 Python
Python urllib.request对象案例解析
May 11 Python
Pytorch distributed 多卡并行载入模型操作
Jun 05 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
WML,Apache,和 PHP 的介绍
2006/10/09 PHP
php excel类 phpExcel使用方法介绍
2010/08/21 PHP
php变量作用域的深入解析
2013/06/03 PHP
浅析PHP原理之变量(Variables inside PHP)
2013/08/09 PHP
php使用百度翻译api示例分享
2014/01/31 PHP
php中mail函数发送邮件失败的解决方法
2014/12/24 PHP
在js中单选框和复选框获取值的方式
2009/11/06 Javascript
jQuery.Autocomplete实现自动完成功能(详解)
2010/07/13 Javascript
jquery $("#variable") 循环改变variable的值示例
2014/02/23 Javascript
CSS图片响应式 垂直水平居中
2015/08/14 Javascript
jQuery实现动态表单验证时文本框抖动效果完整实例
2015/08/21 Javascript
javascript日期格式化方法汇总
2015/10/04 Javascript
jquery获取url参数及url加参数的方法
2015/10/26 Javascript
JavaScript动态插入CSS的方法
2015/12/10 Javascript
jquery实现右侧栏菜单选择操作
2016/03/04 Javascript
jQuery插件jqGrid动态获取列和列字段的方法
2017/03/03 Javascript
ECMAScript6 新特性范例大全
2017/03/24 Javascript
在js中做数字字符串补0(js补零)
2017/03/25 Javascript
js动态添加表格逐行添加、删除、遍历取值的实例代码
2018/01/25 Javascript
js实现input密码框显示/隐藏功能
2020/09/10 Javascript
微信小程序实现联动选择器
2019/02/15 Javascript
layui表格数据复选框回显设置方法
2019/09/13 Javascript
使用VUE实现在table中文字信息超过5个隐藏鼠标移到时弹窗显示全部
2019/09/16 Javascript
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
零基础写python爬虫之抓取百度贴吧并存储到本地txt文件改进版
2014/11/06 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
python文件处理fileinput使用方法详解
2020/01/02 Python
Python实现计算长方形面积(带参数函数demo)
2020/01/18 Python
html5小程序飞入购物车(抛物线绘制运动轨迹点)
2020/10/19 HTML / CSS
高中生学习生活的自我评价
2013/10/09 职场文书
《姥姥的剪纸》教学反思
2014/02/25 职场文书
高三学生评语大全
2014/04/25 职场文书
解除劳动合同协议书
2014/09/17 职场文书
房屋租赁授权委托书范本
2014/09/20 职场文书
2015年全民创业工作总结
2015/07/23 职场文书
Python数据分析之pandas函数详解
2021/04/21 Python