Python教程:int()函数的用法解析

2025-12-01 0 92,143

Python中的函数有很多,之前我们有讲过int()函数,今天就具体用法给大家进行讲解。

int()是Python的一个内部函数 ,Python系统帮助里面是这么说的。

>>> help(int)  
Help on class int in module __builtin__:  
class int(object)  
 | int(x[, base]) -> integer  
 |   
 | Convert a string or number to an integer, if possible. A floating point  
 | argument will be truncated towards zero (this does not include a string  
 | representation of a floating point number!) When converting a string, use  
 | the optional base. It is an error to supply a base when converting a  
 | non-string. If base is zero, the proper base is guessed based on the  
 | string content. If the argument is outside the integer range a  
 | long object will be returned instead.  
>>> int(12.0)  
12


int()函数可以将一个数转化为整数 

>>> int('12',16)  
18


这里有两个地方要注意:

1)12要以字符串的形式进行输入,如果是带参数base的话

2)这里并不是将12转换为16进制的数,而是说12就是一个16进制的数,int()函数将其用十进制数表示,如下


>>> int('0xa',16)  
10 
>>> int('10',8)  
8

对int()函数有遗忘的,可以查询之前的文章进行知识回顾。更多Python学习推荐:PyThon学习网教学中心

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 Python教程:int()函数的用法解析 https://www.zhiup.top/4182.html

相关