英文文档:

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. scrapy_redis 相关: 多线程更新 score/request.priority

    0.背景 使用 scrapy_redis 爬虫, 忘记或错误设置 request.priority(Rule 也可以通过参数 process_request 设置 request.priority), ...

  2. 基于ASP.NET MVC 下的Extjs的Combbox加载速率问题,终于解决啦:)

    在实际的开发项目中,自己遇到一个让自己苦恼很久的问题,一直也未解决,先简单介绍一下这个问题吧.当在表单里有多个combbox的时候(表单中的combbox是经过封装的控件,从后台请求同一个方法获取数据 ...

  3. mac环境下jdk配置

    查看mac下jdk路径 当在Mac下安装完Java运行环境,而又没有添加JAVA_HOME变量的时候,我们如何得到JAVA_HOME变量的路径呢?直接在home目录下执行命令:/usr/libexec ...

  4. js中常用的正则表达式总结

    去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/g,"") ...

  5. mstsc的事 随笔

    当个备份吧, 记不得了,就翻一下自己的博客. MSTSC 设置, 平台:Windows 10 企业版 Windows 10 企业版,功能最全.

  6. BZOJ.4184.shallot(线段树分治 线性基)

    BZOJ 裸的线段树分治+线性基,就是跑的巨慢_(:з」∠)_ . 不知道他们都写的什么=-= //41652kb 11920ms #include <map> #include < ...

  7. rhel 7安装Mysql

    rhel7安装mysql服务 环境: 1)rhel 7虚拟机 2)配置完163网络yum源,并且保证网络通畅 安装过程: 1) 安装Mysql和Mysql-devel 命令:yum install m ...

  8. 资源验证(Modified)

    Last-Modified : 上次修改时间 配合 If-Modified-Since 或者 If-Unmodified-Since  (请求头携带) 对比上次修改时间对资源进行验证 Etag验证 数 ...

  9. 2018-2019-20175324实验一《Java开发环境的熟悉》实验报告

    2018-2019-20175324实验一<Java开发环境的熟悉>实验报告   实验内容与结果 一.Java开发环境的熟悉-1 1.实验要求: 0 参考实验要求 1 建立“自己学号exp ...

  10. Vue(MVVM)、React(MVVM)、Angular(MVC)对比

    前言 昨天阿里内推电面一面,面试官了解到项目中用过Vue,就问为什么前端框架使用Vue而不适用其他的框架,当时就懵了.因为只用过Vue,不了解其他两个框架,今天就赶紧去了解一下他们之间的区别.大家发现 ...