第2章-8 转换函数使用

题目描述:
输入一个整数和进制,转换成十进制输出

输入格式:
在一行输入整数和进制

输出格式:
在一行十进制输出结果

输入样例:
在这里给出一组输入。例如:
45,8

输出样例:
在这里给出相应的输出。例如:
37
在这里插入图片描述

题解

a,b = input().split(",")
b = int(b)
print(int(a,b))

做了这个题才发现python的强大好用,而且int函数居然还可以这么玩,真的是惊到我了。查看python源代码了解int函数的使用,

源代码注释如下:

def __init__(self, x, base=10): # known special case of int.__init__
    """
    int([x]) -> integer
    int(x, base=10) -> integer
    
    Convert a number or string to an integer, 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 the
    given base.  The literal can be preceded by '+' or '-' and be surrounded
    by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
    Base 0 means to interpret the base from the string as an integer literal.
    >>> int('0b100', base=0)
    4
    # (copied from class doc)
    """
    pass

注意这一段话:
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 the given base. The literal can be preceded by ‘+’ or ‘-’ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.

大概意思如下:(本人英语水平有限,大概意思了解就行)
如果x不是一个数字,或者如果给定了base值,那么x必须是整数字面值的base进制的一个一个字符串、字节或者字节数组实例。x前面可以“+”或“-”(即x可以是正负),并且被空格包围。base默认为10。base范围是0和2-36。0表示将base进制的字符串解释为十进制整数。
注释也给了个例子:
int(‘0b100’, base=0)
结果是4

即base为0时候,将标准的其他进制的整数(字符串型)转换为对应的十进制整数。

人生苦短,我学Python!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值