python123平台作业答案进制转换_各种进制转换详解-python

(1)各个进制的符号:b:二进制;o:八进制;d:十进制;x:十六进制

在python中,bin(),oct(),hex()返回值均为字符串而且会带有0b,0o,0o前缀

70

(2)各个进制相互转换

a)十进制转换二进制:

70

十进制转换二进制:

#coding=utf-8

s = 10

list_one = []

if s >= 0 and s <= 1:

print "二进制:%d"%(s)

else:

while s >= 1:

list_one.append(str(s % 2))

s = s / 2

#list_one.append(str(s))

list_one.reverse()

print ''.join(list_one)

b)十进制转换八进制:

70

十进制转换到八进制

#coding=utf-8

s = 10

list_one = []

if s >= 0 and s <= 1:

print "二进制:%d"%(s)

else:

while s >= 1:

list_one.append(str(s % 8))

s = s / 8

#list_one.append(str(s))

list_one.reverse()

print ''.join(list_one)

c)十进制到十六进制:

70

(3)从二,八,十六进制到转换到十进制

a)二进制到十进制:

70

b)八进制到十进制

70

c)十六进制到十进制

70

总结:这里用到格式化字符串函数format;还有eval函数。各个进制之间转换比较灵活不要只局限上面的方法哦;还可以利用我们的公式来转换,或者借用十进制作为中介。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值