def trans(x):
    if x == 0: return [0]
    bit = []
    while x:
        bit.append(x % 2)
        x >>= 1
    return bit[::-1]
Answer from Jun HU on Stack Overflow
๐ŸŒ
YouTube
youtube.com โ€บ computer teaching assistant
Write a Program to Convert Decimal to Binary without using bin function - YouTube
Write a Program to Convert Decimal to Binary without using bin function Program 065 Computer Teaching Assistant Saravanan Marimuthu Raja Python Program Amb...
Published ย  January 2, 2020
Views ย  9K
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
The task of converting a decimal number to binary in Python involves representing the number in base-2 format, where each digit is either 0 or 1.
Published ย  March 19, 2025
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ convert-the-decimal-number-to-binary-without-using-library-function.aspx
Python | Convert the decimal number to binary without using library function
January 4, 2024 - Decimal to binary in Python: Here, we are going to learn how to convert the given decimal number to the binary number without using any library function in Python?
๐ŸŒ
Quora
quora.com โ€บ How-do-you-convert-binary-to-a-decimal-without-using-a-bin-in-Python-3
How to convert binary to a decimal without using a bin in Python 3 - Quora
To convert a binary string (e.g., ... the place-value algorithm: read digits left-to-right (or right-to-left), multiply the running total by 2 for each new bit, and add the bit value....
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - Python
The task of converting a decimal number to binary in Python involves representing the number in base-2 format, where each digit is either 0 or 1.
Published ย  July 11, 2025
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert decimal to binary in python
Python Program to Convert Decimal to Binary Number - Scaler Topics
May 16, 2024 - Learn how to convert a decimal into a binary number and vice versa using recursion, built-in and without built-in function, along with an example program by Scaler Topics.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ math โ€บ python-math-exercise-31.php
Python Math: Convert a binary number to decimal number - w3resource
August 11, 2025 - Write a Python program to convert a given decimal number to binary without using the built-in bin() function, and then print the binary string.
๐ŸŒ
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.") print(hex(dec), "in hexadecimal.")
๐ŸŒ
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
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ decimal-binary-recursion
Python Program to Convert Decimal to Binary Using Recursion
To understand this example, you should have the knowledge of the following Python programming topics: ... Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ binary-decimal-vice-versa-python
Binary to decimal and vice-versa in python - GeeksforGeeks
April 13, 2023 - Write Python code for converting a decimal number to it's binary equivalent and vice-versa.
๐ŸŒ
Besant Technologies
besanttechnologies.com โ€บ home โ€บ blogs โ€บ general โ€บ convert decimal to binary in python
How to Convert Decimal To Binary In Python | Besant Technologies
October 8, 2021 - In this Python tutorial, We study How to Convert Decimal To Binary In Python. Let see what decimal and binary values are.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ python-program-binary-equivalent-number-without-recursion
Python Program to Print Binary Equivalent of a Number without Using Recursion - Sanfoundry
May 30, 2022 - This is a Python Program to find the binary equivalent of a number without using recursion. Problem Description The program takes a number and finds the binary equivalent of the number without using recursion. Problem Solution 1. Take a number from the user. 2. Using a while loop, convert each digit into binary and append ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-decimal-numbers-to-binary-by-a-while-loop-in-Python
How to convert decimal numbers to binary by a while loop in Python - Quora
Answer (1 of 8): 1. To convert decimal numbers to binary using a while loop in Python, you can use the following code: [code]def decimal_to_binary(num): binary_num = "" while num > 0: binary_num = str(num % 2) + binary_num num = num // 2 return binary_num [/code]
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ binary to decimal in python
Binary to Decimal in Python - Scaler Topics
April 18, 2022 - We have to convert binary number to decimal number using the while loop, recursion method, and inbuilt int method available in python.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-create-a-program-that-converts-decimal-to-binary-in-Python
How to create a program that converts decimal to binary in Python - Quora
Converting a decimal (base-10) integer to binary (base-2) in Python can be done several ways: using built-in functions, using bitwise/string methods, or implementing the classic division-by-2 algorithm.