Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打印二进制数';什么是Python?_Python_Binary - Fatal编程技术网

如何打印二进制数';什么是Python?

如何打印二进制数';什么是Python?,python,binary,Python,Binary,我试图了解如何打印出一个二进制数字单独。例如,如果二进制数为1011,我想打印到控制台: 一, 0 一, 一, 基本上,我将把这些数字分别分配给Pi的不同GPIO引脚。这个如何: number = str(1011) for i in number: print(i) 试试这个: bin=str(1011) def print_to_console(agg, item): print(f'{item}\n') reduce(print_to_console, bin, bin[0

我试图了解如何打印出一个二进制数字单独。例如,如果二进制数为1011,我想打印到控制台:

一,

0

一,

一,

基本上,我将把这些数字分别分配给Pi的不同GPIO引脚。

这个如何:

number = str(1011)
for i in number:
    print(i)
试试这个:

bin=str(1011)
def print_to_console(agg, item):
  print(f'{item}\n')

reduce(print_to_console, bin, bin[0])

假设您将二进制数存储在变量中,就这么简单

a = 11000
for i in str(a):
    print(i)
输出:

1
1
0
0
0
0
1
0
0
但如果不打印最左边的零,即如果num为00100,则上述代码将打印1,0和0。 为了不丢失最左边的零,将二进制数字存储为字符串

a = "0100"
for i in a:
    print(i)
输出:

1
1
0
0
0
0
1
0
0

您正在使用哪个版本的python?