Python中int()函数的用法浅析

yipeiwu_com6年前Python基础

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 

总结

以上所述是小编给大家介绍Python中int()函数的用法浅析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

Flask之flask-script模块使用

Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用...

python基础教程之while循环

python基础教程之while循环

前言 今天来说下python中的循环。循环的意思是什么,以环形、回路或轨道运行;沿曲折的路线运行;特指运行一周而回到原处。这是百度给出的循环的意思。在python中,就是重复执行你给的指...

Python Numpy库datetime类型的处理详解

Python Numpy库datetime类型的处理详解

前言 关于时间的处理,Python中自带的处理时间的模块就有time 、datetime、calendar,另外还有扩展的第三方库,如dateutil等等。通过这些途径可以随心所欲地用P...

python 定时修改数据库的示例代码

当需要定时修改数据库时,一般我们都选择起一个定时进程去改库。如果将这种定时任务写入业务中,写成一个接口呢,定时进程显得有些不太合适?如果需要定时修改100次数据库,常规做法会启动100个...

获取python的list中含有重复值的index方法

关于怎么获得,我想其实网上有很多答案。 list.index( )获得值的索引值,但是如果list中含有的值一样,例如含有两个11,22,这样每次获得的都是第一个值的位置。 那么怎么去解...