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

yipeiwu_com5年前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设计】网站的支持!

相关文章

Python实现统计文本文件字数的方法

本文实例讲述了Python实现统计文本文件字数的方法。分享给大家供大家参考,具体如下: 统计文本文件的字数,从当前目录下的file.txt取文件 # -*- coding: GBK...

改进Django中的表单的简单方法

首先,search()视图对于空字符串的处理相当薄弱——仅显示一条”Please submit a search term.”的提示信息。 若用户要重新填写表单必须自行点击“后退”按钮,...

通过PHP与Python代码对比的语法差异详解

一、背景 人工智能这几年一直都比较火,笔者一直想去学习一番;因为一直是从事PHP开发工作,对于Python接触并不算多,总是在关键时候面临着基础不牢,地动山摇的尴尬,比如在遇到稍微深入...

Python实现自定义读写分离代码实例

这篇文章主要介绍了Python实现自定义读写分离代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 思路 自定义Sessio...

Python SqlAlchemy动态添加数据表字段实例解析

Python SqlAlchemy动态添加数据表字段实例解析

本文研究的主要是Python SqlAlchemy动态添加数据表字段,具体如下。 我们知道使用SqlAlchemy创建类来映射数据表,类属性等于数据库字段,但有时候要在我们创建表的时候,...