0

I am trying to understand how to print off a binary numbers digits individually. For example, if the binary number is 1011, I want to print to the console:

1

0

1

1

I am basically going to assign these numbers individually to different GPIO pins of a Pi.

1
  • Which version of python you are using? Commented Nov 20, 2019 at 2:28

3 Answers 3

1

How about this one:

number = str(1011)
for i in number:
    print(i)
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

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

reduce(print_to_console, bin, bin[0])

Comments

0

Assuming you are storing binary number in variable, its as simple as this

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

Output:

1
1
0
0
0

but if wont print the left most zeros, i.e. if num is 00100, the above code will print 1,0 and 0. To not lose left most zeros, store binary digit as a string.

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

Output:

0
1
0
0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.