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中使用next()方法操作文件的教程

 next()方法当一个文件被用作迭代器,典型例子是在一个循环中被使用,next()方法被反复调用。此方法返回下一个输入行,或引发StopIteration异常EOF时被命中。...

Python标准库inspect的具体使用方法

inspect模块用于收集python对象的信息,可以获取类或函数的参数的信息,源码,解析堆栈,对对象进行类型检查等等,有几个好用的方法: Doc:这样写到 The inspect m...

python openCV获取人脸部分并存储功能

本文实例为大家分享了python openCV获取人脸部分并存储的具体代码,供大家参考,具体内容如下 #-*- coding:utf-8 -*- import cv2 import...

pycharm运行出现ImportError:No module named的解决方法

pycharm运行出现ImportError:No module named的解决方法

今天用实验室的pycharm运行程序的时候发现出现了已安装的模块无法导入的情况,但实际上这个模块我已经在notebook中使用多次了,所以不可能是未安装模块的原因 查了一下原因,应该是...

Python图像处理之简单画板实现方法示例

Python图像处理之简单画板实现方法示例

本文实例讲述了Python图像处理之简单画板实现方法。分享给大家供大家参考,具体如下: Python图像处理也是依赖opencv的Python接口实现的,Python语言简单易懂,简洁明...