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之玩转字符串(2)更新篇
Sep 28 Python
Python中处理字符串之endswith()方法的使用简介
May 18 Python
python运行其他程序的实现方法
Jul 14 Python
python中的字典操作及字典函数
Jan 03 Python
python for 循环获取index索引的方法
Feb 01 Python
python自定义函数实现最大值的输出方法
Jul 09 Python
python pygame实现滚动横版射击游戏城市之战
Nov 25 Python
解决Django no such table: django_session的问题
Apr 07 Python
python爬虫用mongodb的理由
Jul 28 Python
Python配置pip国内镜像源的实现
Aug 20 Python
Python爬虫爬取ts碎片视频+验证码登录功能
Feb 22 Python
Pytorch 中的optimizer使用说明
Mar 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
PHP与javascript实现变量交互的示例代码
2013/07/23 PHP
php引用传值实例详解学习
2013/11/06 PHP
php创建和删除目录函数介绍和递归删除目录函数分享
2014/11/18 PHP
php生成图片缩略图的方法
2015/04/07 PHP
PHP实现简易blog的制作
2016/10/24 PHP
php实现的二分查找算法示例
2017/06/20 PHP
jQuery中:checkbox选择器用法实例
2015/01/03 Javascript
JS获取iframe中marginHeight和marginWidth属性的方法
2015/04/01 Javascript
JavaScript使用addEventListener添加事件监听用法实例
2015/06/01 Javascript
手机端转盘抽奖代码分享
2015/09/10 Javascript
阻止表单提交按钮多次提交的完美解决方法
2016/05/16 Javascript
JavaScript模块化之使用requireJS按需加载
2017/04/12 Javascript
基于zepto.js实现手机相册功能
2017/07/11 Javascript
jQuery实现的弹幕效果完整实例
2017/09/06 jQuery
Angular2中监听数据更新的方法
2018/08/31 Javascript
Vue中用props给data赋初始值遇到的问题解决
2018/11/27 Javascript
JavaScript中的"=、==、==="区别讲解
2019/01/22 Javascript
实现vuex与组件data之间的数据同步更新方式
2019/11/12 Javascript
[02:57]DOTA2英雄基础教程 风行者
2014/01/16 DOTA
Windows和Linux下使用Python访问SqlServer的方法介绍
2015/03/10 Python
python2和python3的输入和输出区别介绍
2018/11/20 Python
Flask框架单例模式实现方法详解
2019/07/31 Python
2020年10款优秀的Python第三方库,看看有你中意的吗?
2021/01/12 Python
CSS3选择器新增问题的实现
2021/01/21 HTML / CSS
全球最大的在线旅游公司:Expedia
2017/11/16 全球购物
德国高端单身人士交友网站:ElitePartner
2018/12/02 全球购物
班主任工作年限证明
2014/01/12 职场文书
2014年公司庆元旦活动方案
2014/03/05 职场文书
竞聘书怎么写,如何写?
2014/03/31 职场文书
工作保证书范文
2014/04/29 职场文书
武夷山导游词
2015/02/03 职场文书
大学班干部竞选稿
2015/11/20 职场文书
仅用一句SQL更新整张表的涨跌幅、涨跌率的解决方案
2021/05/06 MySQL
python spilt()分隔字符串的实现示例
2021/05/21 Python
Mysql 用户权限管理实现
2021/05/25 MySQL
十大必看国产动漫排名,魁拔上线,第二曾在日本播出
2022/03/18 国漫