def decimal(n):
two = bin(int(n))
ten = int(two, 2)
return two, ten
def Octal(n):
eight = oct(int(n))
ten = int(eight, 8)
return eight, ten
def hexadecimal(n):
sixteen = hex(int(n))
ten = int(sixteen, 16)
return sixteen, ten
number = input()
print('二进制转换:{}\n八进制转换:{}\n十六进制转换:{}\n'.format(decimal(number), Octal(number), hexadecimal(number)))
输入:
13
输出结果:
二进制转换:('0b1101', 13)
八进制转换:('0o15', 13)
十六进制转换:('0xd', 13)
注意bin(),oct(),hex()函数返回str类型,如果不包含前缀,使用如下方法进行提取:
print(bin(int(10))[2:])
#1010