基本的python内置函数int(x)_Python3.6内置函数(33)——int()

英文文档

class int(x=0)

class int(x, base=10)

Return an integer object constructed from a number or string x, or return0 if no arguments are given.  If x defines __int__(),int(x) returns x.__int__().  If x defines __trunc__(), it returns x.__trunc__(). For floating point numbers, this truncates towards zero.

int()

class int(x=0) class int(x, base=10) 根据传入的参数创建一个新的整数。

1、不传入参数时,得到结果0。

2、传入数值时,调用其__int__()方法,浮点数将向下取整。

3、传入字符串时,默认以10进制进行转换。

>>> int()0

>>> int(3)3>>> int(3.6)3

>>> int('36')36>>> int('3.6')Traceback (most recent call last): File " " , line 1, in 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 " " , line 1, in 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

小结

希望通过上面的操作能帮助大家。如果你有什么好的意见,建议,或者有不同的看法,希望你留言和我进行交流、讨论。

数据之魅。

数据之魅

Airy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值