英文文档:

class int(x=0) class int(x, base=10)

Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).

说明:

  1. 不传入参数时,得到结果0。

>>> int()
0

  2. 传入数值时,调用其__int__()方法,浮点数将向下取整。

>>> int(3)
3
>>> int(3.6)
3

  3. 传入字符串时,默认以10进制进行转换。

>>> int('')
36 >>> int('3.6')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
int('3.6')
ValueError: invalid literal for int() with base 10: '3.6'

  4. 字符串中允许包含"+"、"-"号,但是加减号与数值间不能有空格,数值后、符号前可出现空格。

>>> int('+36')
36
>>> int('-36')
-36
>>> int(' -36 ')
-36
>>> int(' - 36 ')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
int(' - 36 ')
ValueError: invalid literal for int() with base 10: ' - 36

  5. 传入字符串,并指定了进制,则按对应进制将字符串转换成10进制整数。

>>> int('',2)
1
>>> int('',3)
2
>>> int('',8)
7
>>> int('0f',16)
15

Python内置函数(33)——int的更多相关文章

  1. Python内置函数(33)——any

    英文文档: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, re ...

  2. Python内置函数(9)——int

    英文文档: class int(x=0) class int(x, base=10) Return an integer object constructed from a number or str ...

  3. Python内置函数之int()

    class int(x, base=10) 返回一个整型对象.默认返回0. 参数x可以是字符串,也可以是浮点数. base指x的进制形式,比如2表示2进制,10表示10进制.特别需要注意的是,0表示任 ...

  4. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  5. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  6. lambda 表达式+python内置函数

    #函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...

  7. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  8. python 内置函数input/eval(22)

    python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...

  9. python内置函数简单归纳

    做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...

随机推荐

  1. 工具系列-idea破解

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  2. 基于docker/dockerfile实现redis主从复制

    今天我们来搭建基于docker实现redis主从复制集群 为什么要使用redis集群模式? Redis可以说是内存数据库,mysql的数据库是真实存储在硬盘里的,因此,redis的读取速度要比mysq ...

  3. ImCash:币圈英文术语大全

    近年来随着数字货币的火热,在全世界范围内涌现出了一群数字货币的“发烧友”和忠实投资者,他们形成了自己的圈子“币圈”,并且有了自己的文化和语言,今天就让我们一起来了解一下外国币圈有哪些有意思的英文“专用 ...

  4. JS与CSS那些特别小的知识点区别

    1:target与currentTarget的区别 currentTarget指向的事件绑定的元素,target指向的是你点击的元素 2:attr与jprop在jQuery在API当中的区别 2.1: ...

  5. Metasploit中aggregator插件无法使用

    Metasploit中aggregator插件无法使用   aggregator是Metasploit自带的一个插件,用来管理会话Session.该插件使用metasploit-aggreator库. ...

  6. PostgreSQL>窗口函数的用法

    PostgreSQL之窗口函数的用法 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9311281.html PostgreSQL的高级特性本准备三篇的(递归. ...

  7. 黑洞版视频裂变程序【接口版】全新上线,全新UI,支持分享数据统计

    黑洞版视频裂变程序[接口版]全新上线,全新UI,支持分享数据统计!   后台效果   程序统一售价:1899/套(包安装,包更新) 注:本程序不属于之前视频程序的更新版,展现形式和广告位设置均不同,是 ...

  8. 2019年3月2日-小雨.md

    2019年3月2日, 星期六 开学已经一周了,时间好像限制了自己进步的脚步,一个人的精力有限,想做好方方面面实在是太难了,有很多事儿最后都没做的完美.相反,自己应该放下繁琐的包袱,简简单单的干一件事儿 ...

  9. Exp2 后门原理与实践 20164302 王一帆

    1 实验内容 1.1实验主要内容 (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, 任务计划启动 (0.5分) (3)使用MSF ...

  10. TypeScript 函数-重载

    function attr(name:string):string; function attr(age:number):string; function attr(nameorage:any):an ...