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使用xauth方式登录饭否网然后发消息
Apr 11 Python
Python类的多重继承问题深入分析
Nov 09 Python
python实现简单socket程序在两台电脑之间传输消息的方法
Mar 13 Python
python 通过xml获取测试节点和属性的实例
Mar 31 Python
pytorch cnn 识别手写的字实现自建图片数据
May 20 Python
python中字符串的操作方法大全
Jun 03 Python
python脚本监控Tomcat服务器的方法
Jul 06 Python
对python中矩阵相加函数sum()的使用详解
Jan 28 Python
python切片的步进、添加、连接简单操作示例
Jul 11 Python
python2.7使用scapy发送syn实例
May 05 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
Jul 03 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
在IIS上安装PHP4.0正式版
2006/10/09 PHP
PHP实现获取文件后缀名的几种常用方法
2015/08/08 PHP
php二维码生成
2015/10/19 PHP
实例介绍PHP中zip_open()函数用法
2019/02/15 PHP
使用js完成节点的增删改复制等的操作
2014/01/02 Javascript
jquery实现省市select下拉框的替换(示例代码)
2014/02/22 Javascript
javascript圆盘抽奖程序实现原理和完整代码例子
2014/06/03 Javascript
JS仿Windows开机启动Loading进度条的方法
2015/02/26 Javascript
[原创]javascript typeof id==='string'?document.getElementById(id):id解释
2016/11/02 Javascript
ES6扩展运算符的用途实例详解
2017/08/20 Javascript
浅谈VUE单页应用首屏加载速度优化方案
2018/08/28 Javascript
vue:el-input输入时限制输入的类型操作
2020/08/05 Javascript
Vue3 实现双盒子定位Overlay的示例
2020/12/22 Vue.js
python用ConfigObj读写配置文件的实现代码
2013/03/04 Python
Python中变量交换的例子
2014/08/25 Python
Python安装使用命令行交互模块pexpect的基础教程
2016/05/12 Python
Python从使用线程到使用async/await的深入讲解
2018/09/16 Python
python 把列表转化为字符串的方法
2018/10/23 Python
Django ManyToManyField 跨越中间表查询的方法
2018/12/18 Python
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
2019/03/11 Python
numpy.random.shuffle打乱顺序函数的实现
2019/09/10 Python
python使用matplotlib:subplot绘制多个子图的示例
2020/09/24 Python
canvas画布实现手写签名效果的示例代码
2019/04/23 HTML / CSS
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
营销人才自我鉴定范文
2013/12/25 职场文书
外贸采购员岗位职责
2014/03/08 职场文书
六个一活动实施方案
2014/03/21 职场文书
公司承诺书怎么写
2014/05/24 职场文书
公司户外活动总结
2014/07/04 职场文书
计生办班子群众路线教育实践活动个人对照检查材料思想汇报
2014/10/04 职场文书
公司感恩节活动策划书
2014/10/11 职场文书
导游欢迎词范文
2015/01/23 职场文书
读完《骆驼祥子》的观后感!
2019/07/05 职场文书
Kubernetes控制节点的部署
2022/04/01 Servers
如何使用python包中的sched事件调度器
2022/04/30 Python
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers