You can probably use the builtin bin function:

bin(8) #'0b1000'

to get the list:

[int(x) for x in bin(8)[2:]]

Although it seems like there's probably a better way...

Answer from mgilson on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-decimal-to-binary-list-conversion
Python | Decimal to binary list conversion - GeeksforGeeks
July 11, 2025 - Method #1 : Using list comprehension + format() In this method, the conversion of the decimal to binary is handled by the format function. The logic of conversion to the list is done by the list comprehension function.
Discussions

Convert decimal to binary in python - Stack Overflow
Is there any module or function in python I can use to convert a decimal number to its binary equivalent? I am able to convert binary to decimal using int('[binary_value]',2), so any way to do the More on stackoverflow.com
๐ŸŒ stackoverflow.com
Converting from decimal to binary in Python
So this is a pretty complicated topic for a beginner called recursion. I'll try my best to explain what's going on. The last line print(n % 2, end="") simply prints the remainder after n is divided by 2. This will be either 1 or 0 since 2 goes into all larger numbers atleast once. By default python puts a new line character after every print and the 'end=""' simply says 'print nothing at the end' instead of the normal 'print \n at the end' (\n indicates a newline) Now for the fun part. Let's step through what happens with n=10. In binary we know this is represented by '1010'. When n is 10 we see it is greater than 1, so we execute line 3 (dec2bin(n//2)). This basically pauses what we're currently doing for now and starts over with a different value. You can think of this like a stack of boxes. Originally we just have our one box, when we call dec2bin again we put another box on top. We can't keep working on the bottom box anymore because it's covered. We need to finish the top box so we can remove it and get to the bottom box again. I should mention that // means integer division. 3//2 is 1 because 2 goes into 3 one whole time. So in our stack of boxes (aka our call stack) we will have: dec2bin(5) dec2bin(10) So let's work through what happens when we call dec2bin(5). It's important to note that 5 is '101' in binary which is the same as 10 ('1010') except the last 0 disappears because of the division. 5 is greater than 1 so we execute line 3 and call dec2bin again! Gotta put another box on top of our call stack: dec2bin(2) dec2bin(5) dec2bin(10) Note that 2 in binary is '10', which is the same as ten in binary except drop the last two digits because we did division twice. Working through dec2bin(2) we get another call to dec2bin: dec2bin(1) dec2bin(2) dec2bin(5) dec2bin(10) Printed: nothing Now this is where it gets interesting. Working through dec2bin(1) we have n=1. In this case n is NOT greater than 1 (line 2). So we continue on and print 1%2 which is 1. Now the function finishes and we're done with this call of dec2bin(1) so we can remove it from the call stack: dec2bin(2) dec2bin(5) dec2bin(10) Printed: '1' Now dec2bin(2) we paused on line 3, but line 3 just finished up so we can continue onto line 4 and print 2%2 which is 0. We've printed '10' so far. Again the function finishes up and we remove it from the call stack. dec2bin(5) dec2bin(10) Printed: '10' dec2bin(5) was paused on line 3, it continues now. We print 5%2 which is 1. The function finishes. It gets removed from the call stack. dec2bin(10) Printed: '101' We've finished up most of the pile back down to our original call. It (finally) runs its line 4 and prints 10%2, which is 0. We've printed '1010'. The function finishes up and we're all done. Printed: '1010' I hope that I haven't just confused you further, recursion is a tough topic to understand especially if not explained in a way that makes sense to you. More on reddit.com
๐ŸŒ r/learnpython
9
15
July 24, 2019
converting a list consist of decimal numbers to binary in python - Stack Overflow
I have a list of decimal numbers as integers, and I want to convert them into binary. The output must be without 0b prefix and also it must be 4 digits like: lista = [1,2,3,4,5,6,7,8,9,10] listres... More on stackoverflow.com
๐ŸŒ stackoverflow.com
October 30, 2018
How to Convert list of binary number to decimal in python without using any inbuilt function?
Treat them as true and false or multiply it by its appropriate counterpart. Ex. [1,0,0,1] => 1ร—23 + 0ร—22 + 0ร—21 + 1ร—20 More on reddit.com
๐ŸŒ r/learnpython
3
1
May 1, 2021
๐ŸŒ
YouTube
youtube.com โ€บ watch
Decimal To Binary Conversion In Python With Multiple Ways - YouTube
In this tutorial you will see how to convert decimal numbers into binary numbers using logical implementation with string and List. Also, we will use built i...
Published ย  July 4, 2023
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ decimal-to-binary-list-conversion-in-python
Decimal to binary list conversion in Python
May 4, 2020 - We can Use the letter in the formatter to indicate which number base: decimal, hex, octal, or binary we want our number to be formatted. In the below example we take the formatter as 0:0b then supply the integer to the format function which needs to get converted to binary.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
By joining the binary list into a string and using the built-in ยท 3 min read Python program to convert Base 4 system to binary number ยท Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1: ... We use cookies to ensure you have the best browsing experience on our website.
Published ย  March 19, 2025
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ math โ€บ python-math-exercise-31.php
Python Math: Convert a binary number to decimal number - w3resource
... b_num = list(input("Input a binary number: ")) value = 0 for i in range(len(b_num)): digit = b_num.pop() if digit == '1': value = value + pow(2, i) print("The decimal value of the number is", value)
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting from decimal to binary in python
r/learnpython on Reddit: Converting from decimal to binary in Python
July 24, 2019 -

Would someone be willing to help me (total beginner) understand how this converts decimal values to binary? Maybe just by explaining what happens line by line?

def dec2bin(n):
    if n > 1:
        dec2bin(n//2)
    print(n % 2, end="")

It's just not intuitive at all for me :/

Any help would be greatly appreciated!

Top answer
1 of 4
19
So this is a pretty complicated topic for a beginner called recursion. I'll try my best to explain what's going on. The last line print(n % 2, end="") simply prints the remainder after n is divided by 2. This will be either 1 or 0 since 2 goes into all larger numbers atleast once. By default python puts a new line character after every print and the 'end=""' simply says 'print nothing at the end' instead of the normal 'print \n at the end' (\n indicates a newline) Now for the fun part. Let's step through what happens with n=10. In binary we know this is represented by '1010'. When n is 10 we see it is greater than 1, so we execute line 3 (dec2bin(n//2)). This basically pauses what we're currently doing for now and starts over with a different value. You can think of this like a stack of boxes. Originally we just have our one box, when we call dec2bin again we put another box on top. We can't keep working on the bottom box anymore because it's covered. We need to finish the top box so we can remove it and get to the bottom box again. I should mention that // means integer division. 3//2 is 1 because 2 goes into 3 one whole time. So in our stack of boxes (aka our call stack) we will have: dec2bin(5) dec2bin(10) So let's work through what happens when we call dec2bin(5). It's important to note that 5 is '101' in binary which is the same as 10 ('1010') except the last 0 disappears because of the division. 5 is greater than 1 so we execute line 3 and call dec2bin again! Gotta put another box on top of our call stack: dec2bin(2) dec2bin(5) dec2bin(10) Note that 2 in binary is '10', which is the same as ten in binary except drop the last two digits because we did division twice. Working through dec2bin(2) we get another call to dec2bin: dec2bin(1) dec2bin(2) dec2bin(5) dec2bin(10) Printed: nothing Now this is where it gets interesting. Working through dec2bin(1) we have n=1. In this case n is NOT greater than 1 (line 2). So we continue on and print 1%2 which is 1. Now the function finishes and we're done with this call of dec2bin(1) so we can remove it from the call stack: dec2bin(2) dec2bin(5) dec2bin(10) Printed: '1' Now dec2bin(2) we paused on line 3, but line 3 just finished up so we can continue onto line 4 and print 2%2 which is 0. We've printed '10' so far. Again the function finishes up and we remove it from the call stack. dec2bin(5) dec2bin(10) Printed: '10' dec2bin(5) was paused on line 3, it continues now. We print 5%2 which is 1. The function finishes. It gets removed from the call stack. dec2bin(10) Printed: '101' We've finished up most of the pile back down to our original call. It (finally) runs its line 4 and prints 10%2, which is 0. We've printed '1010'. The function finishes up and we're all done. Printed: '1010' I hope that I haven't just confused you further, recursion is a tough topic to understand especially if not explained in a way that makes sense to you.
2 of 4
1
by the case you just didnยดt figure it out with the answers already posted. Try using Python Tutor http://pythontutor.com/visualize.html#mode=edit paste the code there and visualize what happens
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ list โ€บ python-data-type-list-exercise-185.php
Python: Convert a given decimal number to binary list - w3resource
June 28, 2025 - return result # Set the value of ... the 'decimal_to_binary_list' function with 'n' and print the result. print("Decimal number (", n, ") to binary list:") # Print the binary representation of the number 8 as a list...
๐ŸŒ
CodeScracker
codescracker.com โ€บ python โ€บ program โ€บ python-program-convert-decimal-to-binary.htm
Python Program to Convert Decimal to Binary
Now print the value of bnum[] list from its last to 0th index. Therefore, the binary equivalent of given decimal number (50) is 110010 ยท This is the modified version of previous program. The end= is used to skip printing of an automatic newline using print().
๐ŸŒ
StudyMite
studymite.com โ€บ python โ€บ examples โ€บ program-to-convert-decimal-to-binary-in-python
Decimal to Binary in Python | StudyMite
11 hours ago - Here we'll write a program to convert decimal to binary in Python with explanation, code, and algorithm.
๐ŸŒ
Bhavyasree
bhavyasree.github.io โ€บ PythonClass โ€บ Notebooks โ€บ 07.binary,decimal
Binary and Decimal Conversions - Python Class
binary = list(input("Enter a binary number:")) binaryToDecimal(binary) Enter a binary number:10 The decimal value of the number is 2 ยท We can use the built-in int() function to convert binary numbers into decimals.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ binary-decimal-vice-versa-python
Binary to Decimal and vice-versa in Python - GeeksforGeeks
3 weeks ago - bin() is a built in Python function that converts a decimal number directly to its binary string. ... bin(n) converts a decimal number to a binary string, but the result has a prefix "0b" (e.g., 0b1000).
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert decimal to binary in python
Python Program to Convert Decimal to Binary Number - Scaler Topics
May 16, 2024 - Decimal to Binary conversion is achieved using the Division By 2 technique. Some of the ways to convert Decimal to Binary in Python are by using a custom recursive function, built-in functionbin(<value>) or using โ€œ{0}โ€.format(int(
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ conversion-binary-octal-hexadecimal
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
A number with the prefix 0b is considered binary, 0o is considered octal and 0x as hexadecimal. For example: ... # Python program to convert decimal into other number systems dec = 344 print("The decimal value of", dec, "is:") print(bin(dec), "in binary.") print(oct(dec), "in octal.") ...
๐ŸŒ
NareshIT
nareshit.com โ€บ blogs โ€บ how-to-convert-decimal-to-binary-in-python
How to Convert Decimal to Binary in Python | Step-by-Step Guide
To implement the code in Python we can proceed as follows. # Function to convert decimal number to binary using recursion technique. ... Approach-2: Decimal to binary using in-built function. Here we can make the use of bin() function.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-binary-to-decimal-using-simple-Python-commands
How to convert binary to decimal using simple Python commands - Quora
The only Python IDE you need to build data models and AI agents. Free forever, plus one month of Pro. ... This involves in the conversion of binary into decimal value.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ decimal-binary-recursion
Python Program to Convert Decimal to Binary Using Recursion
# Function to print binary number using recursion def convertToBinary(n): if n > 1: convertToBinary(n//2) print(n % 2,end = '') # decimal number dec = 34 convertToBinary(dec) print() ... You can change the variable dec in the above program and run it to test out for other values.