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学习资料
Feb 08 Python
python获取糗百图片代码实例
Dec 18 Python
python中的sort方法使用详解
Jul 25 Python
详解Python中类的定义与使用
Apr 11 Python
python用户管理系统的实例讲解
Dec 23 Python
Python 单元测试(unittest)的使用小结
Nov 14 Python
Python中xml和json格式相互转换操作示例
Dec 05 Python
解决Python中list里的中文输出到html模板里的问题
Dec 17 Python
Python os.access()用法实例
Feb 18 Python
Python计算一个点到所有点的欧式距离实现方法
Jul 04 Python
python如何实现不可变字典inmutabledict
Jan 08 Python
详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式
Jan 24 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
php4的session功能评述(二)
2006/10/09 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
2017/07/10 PHP
laravel邮件发送的实现代码示例
2020/01/31 PHP
jQuery powerFloat万能浮动层下拉层插件使用介绍
2010/12/27 Javascript
基于jQuery的公告无限循环滚动实现代码
2012/05/11 Javascript
基于JQuery实现的图片自动进行缩放和裁剪处理
2014/01/31 Javascript
js 操作符汇总
2014/11/08 Javascript
超漂亮的Bootstrap 富文本编辑器summernote
2016/04/05 Javascript
js本地图片预览实现代码
2016/10/09 Javascript
解析微信JS-SDK配置授权,实现分享接口
2016/12/09 Javascript
angularjs点击图片放大实现上传图片预览
2017/02/24 Javascript
浅析Javascript中双等号(==)隐性转换机制
2017/10/27 Javascript
js保留两位小数方法总结
2018/01/31 Javascript
nodejs的路径问题的解决
2018/06/30 NodeJs
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
vue中created和mounted的区别浅析
2019/08/13 Javascript
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
python文件的md5加密方法
2016/04/06 Python
python数字图像处理实现直方图与均衡化
2018/05/04 Python
基于Python在MacOS上安装robotframework-ride
2018/12/28 Python
Python设计模式之备忘录模式原理与用法详解
2019/01/15 Python
通过python爬虫赚钱的方法
2019/01/29 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
2020/02/25 Python
python字符串常用方法及文件简单读写的操作方法
2020/03/04 Python
通过Python实现Payload分离免杀过程详解
2020/07/13 Python
HTML5仿微信聊天界面、微信朋友圈实例代码
2018/01/29 HTML / CSS
夏威夷咖啡公司:Hawaii Coffee Company
2019/09/19 全球购物
用Python写一个for循环的例子
2016/07/19 面试题
高中考试作弊检讨书
2014/01/14 职场文书
大学同学聚会邀请函
2014/01/19 职场文书
小学端午节活动方案
2014/03/13 职场文书
法人授权委托书格式
2014/04/08 职场文书
党校培训自我鉴定范文
2014/04/10 职场文书
《美丽的南沙群岛》教学反思
2014/04/27 职场文书
科学发展观演讲稿
2014/09/11 职场文书
好员工观后感
2015/06/17 职场文书