帝吃藕和 发表于 2020-4-7 10:52:43

python 23 24 讲 进制

def Dec2Bin(dec):
    result = ''

    if dec:
      result = Dec2Bin(dec//2)
      return result + str(dec%2)
    else:
      return result

print(Dec2Bin(62))

小白想问一下 if dec: 是指 if dec >= 0 吗
                     else 满足的条件又是什么呢?

qiuyouzhi 发表于 2020-4-7 10:54:13

1,准确来讲是dec != 0,也可以理解为dec == True
2,else就是dec等于0(不能再除了),就直接返回result(之前处理好的结果)
页: [1]
查看完整版本: python 23 24 讲 进制