Python内置函数(33)——int
英文文档:
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的更多相关文章
- Python内置函数(33)——any
英文文档: any(iterable) Return True if any element of the iterable is true. If the iterable is empty, re ...
- Python内置函数(9)——int
英文文档: class int(x=0) class int(x, base=10) Return an integer object constructed from a number or str ...
- Python内置函数之int()
class int(x, base=10) 返回一个整型对象.默认返回0. 参数x可以是字符串,也可以是浮点数. base指x的进制形式,比如2表示2进制,10表示10进制.特别需要注意的是,0表示任 ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
- Python学习:6.python内置函数
Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...
- python 内置函数input/eval(22)
python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...
- python内置函数简单归纳
做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...
随机推荐
- 使用Nexus搭建私有Nuget仓库
前言 Nuget是ASP .NET Gallery的一员,是免费.开源的包管理工具,专注于在.Net / .Net Core应用开发过程中第三方组件库的管理,相对于传统单纯的dll引用要方便.科学得多 ...
- 这篇通俗实用的Vlookup函数教程,5分钟就可以包你一学就会
如何利用Vlookup函数获取学号中的班级信息.换言之,咱们源数据中放着姓名性别学号班级等信息,而在另一张表格中一定有学号信息,但其他信息就未必有,这需要我们将缺失的信息自动同步过去.使用vlooku ...
- Centos7安装jdk-12的详细过程
Centos7安装jdk-12的详细过程 2019-04-12 21:23:24 一.下载JDK-12版本 链接地址:官方地址 下载:jdk-12_liunx-x64_bin.tar.gz 二.检 ...
- 2018-2019网络对抗技术 20165220 Exp4 恶意代码分析
实践目标 监控你自己系统的运行状态,看有没有可疑的程序在运行. 分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sysinternals,systracer套件. ...
- 微服务框架——SpringCloud
1.SpringCloud微服务框架 a.概念:SpringCloud是基于SpringBoot的微服务框架 b.五大神兽:Eureka(服务发现).Ribbon(客服端负载均衡).Hystrix(断 ...
- linux系统,关于Python多版本共存
http://www.cnblogs.com/Yiutto/p/5962906.html 给个地址直接看八~
- Centos给文件设置了777权限仍不能访问解决方案
Centos给文件设置了777权限仍不能访问: 开启了SELinux导致 1.查看SELinux状态:/usr/sbin/sestatus -v ##如果SELinux status参数为enable ...
- 【安全性测试】利用反编译查看对应activity的方法采用hook技术绑定劫持_入门
本次主要为了研究手机端的安全性而写的一篇文章,在基于自己对手机安全性的研究下,想到了这些工具之间的结合,当然这也算是第一次对手机安全研究勇敢地踏出一步,也不知道是否成功,还是准备撞南墙撞到底吧! 使用 ...
- emoji 表情: MySQL如何支持 emoji 表情
https://www.cnblogs.com/jentary/p/6655471.html 修改数据库字符集: ALTER DATABASE database_name CHARACTER SET ...
- js jq 字符串数组对象
数组是有序的,对象是无序,数组是特殊的对象 数组 声明数组 var arr=new Array('red','blue','yellow'); //["red", "bl ...