港服(Server.HK)Python教程:Python教程:int()函数的用法解析

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

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电脑。)

为您推荐

港服(Server.HK)Python教程:如何实现对Python中列表的排序?

对List进行排序,Python提供了两个方法 方法1.用List的内建函数list.sort进行排序 list.sor...

港服(Server.HK)Python教程:python迭代器中的函数整理

1、可以连接迭代器的函数 chain:按顺序将多个迭代器连接成一个迭代器。 Cycle:重复迭代器的所有元素。 Tee:...

港服(Server.HK)Python教程:用Python举例实现逆波兰表达式

逆波兰表达式是编译原理中的一种基本表达式,利用Python语言也可以实现逆波兰表达式的输出,这里举例实践说明: 什么是逆...

Python 程序:检查给定字符串是否为回文

港服(Server.HK)Python教程: 用一个实例写一个 Python 程序来检查给定的字符串是不是回文。在 Py...

港服(Server.HK)Python教程:python3判断字典中key是否存在

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实...
返回顶部