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 相关文章推荐
下载安装setuptool和pip linux安装pip    
Jan 24 Python
python实现得到一个给定类的虚函数
Sep 28 Python
python中的__slots__使用示例
Feb 26 Python
简单的编程0基础下Python入门指引
Apr 01 Python
Python类的继承和多态代码详解
Dec 27 Python
Django 创建后台,配置sqlite3教程
Nov 18 Python
Python如何使用Gitlab API实现批量的合并分支
Nov 27 Python
Django框架models使用group by详解
Mar 11 Python
Django中使用Json返回数据的实现方法
Jun 03 Python
Python 串口通信的实现
Sep 29 Python
python 下载文件的多种方法汇总
Nov 17 Python
python3中apply函数和lambda函数的使用详解
Feb 28 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做下载文件的实现代码及文件名中乱码解决方法
2011/02/03 PHP
php语言流程控制中的主动与被动
2012/11/05 PHP
thinkphp修改配置进入默认首页的方法
2017/02/07 PHP
Alliance vs AM BO3 第二场2.13
2021/03/10 DOTA
firefox浏览器下javascript 拖动层效果与原理分析代码
2007/12/04 Javascript
Javascript类库的顶层对象名用户体验分析
2010/10/24 Javascript
点弹代码 点击页面任何位置都可以弹出页面效果代码
2012/09/17 Javascript
Nodejs的express使用教程
2015/11/23 NodeJs
详解能在多种前端框架下使用的表格控件
2017/01/11 Javascript
Bootstrap jquery.twbsPagination.js动态页码分页实例代码
2017/02/20 Javascript
Bootstrap3下拉菜单的实现
2017/02/22 Javascript
基于Vue的ajax公共方法(详解)
2018/01/20 Javascript
微信小程序form表单组件示例代码
2018/07/15 Javascript
node.js调用C++函数的方法示例
2018/09/21 Javascript
JavaScript canvas实现文字时钟
2021/01/10 Javascript
在Python的gevent框架下执行异步的Solr查询的教程
2015/04/16 Python
Python语言实现机器学习的K-近邻算法
2015/06/11 Python
Python设计模式之MVC模式简单示例
2018/01/10 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
centos+nginx+uwsgi+Django实现IP+port访问服务器
2019/11/15 Python
python实现五子棋游戏(pygame版)
2020/01/19 Python
Django多数据库配置及逆向生成model教程
2020/03/28 Python
Python闭包与装饰器原理及实例解析
2020/04/30 Python
Python学习之路之pycharm的第一个项目搭建过程
2020/06/18 Python
如何使用Pytorch搭建模型
2020/10/26 Python
CSS3中的Media Queries学习笔记
2016/05/23 HTML / CSS
KEETSA环保床垫:更好的睡眠,更好的生活!
2016/11/24 全球购物
Python的两道面试题
2013/06/29 面试题
Ruby中的保护方法和私有方法与一般面向对象程序设计语言的一样吗
2013/05/01 面试题
公务员职业生涯规划书范文  
2014/01/19 职场文书
小学生常见病防治方案
2014/06/06 职场文书
体育运动口号
2014/06/09 职场文书
2014入党积极分子批评与自我批评思想汇报
2014/09/20 职场文书
2014年法务工作总结
2014/12/11 职场文书
天谕手游15杯全调酒配方和调酒券的获得方式
2022/04/06 其他游戏
电脑只能进入安全模式无法正常启动的解决办法
2022/04/08 数码科技