英文文档:

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. SharePoint修改左上角文字的命令行

    $webapp = Get-SPWebApplication “http://test-spweb1” --需要修改的站点$webapp.SuiteNavBrandingText = “XXXXXX” ...

  2. Jmeter中基本操作

    Jmeter中基本操作包括 1:线程组 2:HTTP信息头管理器 3:HTTP请求默认值 4:HTTP请求 5:查看结果树 操作步骤如下: 1.创建一个线程组 通俗的讲一个线程组,,可以看做一个虚拟用 ...

  3. 把我坑惨的一个update语句

    最近好几次有开发同学在问我,比如下图: 问题归纳起来就是:在MySQL里面update一条记录,语法都正确的,但记录并没有被更新... 刚遇到这个问题的时候,我拿到这条语句直接在测试库里面执行了一把, ...

  4. Java是值传递还是引用传递?

    Java的值传递和引用传递在面试中一般都会都被涉及到,今天我们就来聊聊这个问题.这个问题一般是相对函数而言的,也就是Java中所说的方法参数,那么我们先来回顾一下在程序设计语言中有关参数传递给方法的两 ...

  5. python可视化库 Matplotlib 01 figure的详细用法

    1.上一章绘制一幅最简单的图像,这一章介绍figure的详细用法,figure用于生成图像窗口的方法,并可以设置一些参数 2.先看此次生成的图像: 3.代码(代码中有详细的注释) # -*- enco ...

  6. 操作redis数据库 & 操作Excel & 开发接口

    操作redis数据库: string类型 1. 增 set,传俩个参数 key value(只要是字符串就行)2. 删 delete 传一个参数 key3. 修改 set 在目标key重新传参 key ...

  7. 将普通用户添加到sudo

    将普通用户添加到sudo组 可以编辑/etc/sudoers文件将普通用户加入sudo组.要注意的是修改该文件只能使用visudo命令:1.首先切换到root #su - (注意有 “-” ,这和su ...

  8. Linux正则表达式练习

    练习一 1.生成30位的随机口令 [root@centos7 ~]#cat /dev/urandom | tr -dc "[:alnum:]" | head -c30 RJL5qc ...

  9. VMware vsphere client报错问题

    今天出现一种情况,搞了很久,重装client都不行,原来很简单,防止再犯.     异常信息:"VirtualInfrastructure.Utils.ClientsXml"的类型 ...

  10. Vue(三十二)SSR服务端渲染Nuxt.js

    初始化Nuxt.js项目步骤 1.使用脚手架工具 create-nuxt-app 创建Nuxt项目 使用yarn或者npm $ yarn create nuxt-app <项目名> 注:根 ...