英文文档:

class int(x=0class int(xbase=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/0B0o/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')
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('01',2)
1
>>> int('02',3)
2
>>> int('07',8)
7
>>> int('0f',16)
15

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

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

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

  2. Python内置函数之int()

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

  3. python内置函数

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

  4. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  5. Python内置函数进制转换的用法

    使用Python内置函数:bin().oct().int().hex()可实现进制转换. 先看Python官方文档中对这几个内置函数的描述: bin(x)Convert an integer numb ...

  6. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  7. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

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

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

  9. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

随机推荐

  1. Java仪器数据文件解析-PDF文件

    一.概述 使用pdfbox可生成Pdf文件,同样可以解析PDF文本内容. pdfbox链接:https://pdfbox.apache.org/ 二.PDF文本内容解析 File file = new ...

  2. php做的一个简易爬虫

    对于爬虫的好奇好像由来已久,一直在研究python的爬虫,今天得空研究研究php的爬虫 index.php <?php header("Content-Type:text/html;c ...

  3. jQuery中的ajax的相关方法

    JQuery对Ajax操作进行了封装,$.ajax()方法属于最底层的方法,第2层是load().$.get().$.post()方法,第3层是$.getScript()和$.getJSON()方法. ...

  4. SignalR Self Host+MVC等多端消息推送服务(4)

    由于工作太忙,一直没时间更新博客,之前有很多朋友一直问我什么时候将后续的代码发上来,一直没时间,今天就长话短说,不写文章了,直接上demo,里面将正式项目中用到的一些敏感信息修改了,要使用的话下载后自 ...

  5. Beego 框架学习(一)

    Beego官网本身已经整理的非常详细了,但是作为一个学习者,我还是决定自己好好整理一下,这样在后面使用的时候自己对每部分才能非常熟悉,及时忘记了,也可以迅速定位自己要用的知识在哪里.当然也是对官网的一 ...

  6. Linux档案权限与目录配置

    一.档案权限: Linux 最优秀的地方之一,就在于他的多人多任务环境.而为了让各个使用者具有较保密的档案数据,因此档案的权限管理就变的很重要了. Linux 一般将档案可存取的身份分为三个类别,分别 ...

  7. Bootatrap常用样式

    1. 使div固定定位在页面的底部: // 当然, 需要加上自己的样式稍加修饰<div class="navbar navbar-fixed-bottom mobile-view-bo ...

  8. js对象系列【一】深层理解对象与原型

    我们先从盘古开天辟地时捋一捋对象: 从宏观内容来讲,javascript是一个属性的集合,包括值,函数,而整个集合也可以类比为一个对象. js = { a的变量名: a的值, ... 函数b: fun ...

  9. Algorithm --> 小于N的正整数含有1的个数

    //https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number of d ...

  10. 0x02 译文:Windows桌面应用Win32第一个程序

    本节课我们将用C++ 写一个最简单的Windows 程序. 目录: 创建一个窗口 窗口消息 编写窗口过程 绘制窗口 关闭窗口 管理应用程序状态 代码如下: #ifndef UNICODE #defin ...