最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Python内置int函数详细介绍

来源:动视网 责编:小采 时间:2020-11-27 14:25:12
文档

Python内置int函数详细介绍

Python内置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_
推荐度:
导读Python内置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_


英文文档:

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')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内置int函数详细介绍

Python内置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_
推荐度:
标签: 介绍 简介 函数
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top