all numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)

>>> bin(10)
'0b1010'
>>> 0b1010
10
Answer from aaronasterling on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
This method efficiently converts a decimal number to binary by applying bitwise operations. It continuously extracts the least significant bit using the & 1 operator and shifts the number right using >>. This is the most efficient approach in ...
Published   July 11, 2025
Discussions

python - Converting binary to decimal integer output - Stack Overflow
The biggest problem is that we are not allowed to use built-in functions. I understand why we are not allowed to use them, but it's making this problem much more difficult, since I know Python has a built-in function for binary to decimal. 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
Python program: a tool to convert binary numbers to decimal, and decimal numbers to binary, and a simple…

heres a link to make it easier https://repl.it/repls/EvergreenPlainAmericanlobster

More on reddit.com
🌐 r/Python
5
0
November 18, 2017
Convert decimal input to 8 bit binary
https://letmegooglethat.com/?q=python+dec+to+bin More on reddit.com
🌐 r/pythontips
7
1
October 29, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › binary-decimal-vice-versa-python
Binary to Decimal and vice-versa in Python - GeeksforGeeks
3 weeks ago - For each number, it keeps dividing by 2 and adds the remainder to form the binary. bin() is a built in Python function that converts a decimal number directly to its 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.") ...
🌐
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) ...
🌐
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.
Find elsewhere
🌐
NareshIT
nareshit.com › blogs › how-to-convert-decimal-to-binary-in-python
How to Convert Decimal to Binary in Python | Step-by-Step Guide
In Python we can do the conversion form binary to decimal in following two manners. Here I am going to discuss both the approaches as below. ... Approach-1: Without using the inbuilt function. Here we can use the loop concept to make the conversion. ... Approach-2: Using the inbuilt function int(). We can also able to do the program using the inbuilt function int().
🌐
Scaler
scaler.com › home › topics › convert decimal to binary in python
Python Program to Convert Decimal to Binary Number - Scaler Topics
May 16, 2024 - In Python, the conversion from a decimal value to its corresponding binary representation can be achieved using the bin() function.
🌐
PYnative
pynative.com › home › python › programs and examples › python convert decimal number to binary and vice versa
Python Convert Decimal Number to Binary and Vice Versa – PYnative
April 22, 2025 - The bin() function in Python converts an integer number into its binary string representation. ... Returns a string that starts with '0b', followed by the binary representation of the integer. ... Note: [2:] slices a string from index 2 to the end, skipping the first two characters. decimal ...
🌐
Vultr
docs.vultr.com › python › examples › convert-decimal-to-binary-octal-and-hexadecimal
Python Program to Convert Decimal to Binary, Octal and Hexadecimal | Vultr Docs
November 22, 2024 - Python’s bin() function returns the binary representation prefixed with '0b'. bin() outputs a string that starts with '0b', denoting that the following digits are in binary format.
🌐
CodeScracker
codescracker.com › python › program › python-program-convert-decimal-to-binary.htm
Python Program to Convert Decimal to Binary
That is, the condition dnum!=0 or 25!=0 evaluates to be true again, therefore program flow again goes inside the loop and evaluates all the four statements of its body · This process continues, until the condition evaluates to be false · Before condition evaluates to be false, here are the list of values one by one after each evaluation of the loop: ... Now print the value of bnum[] list from its last to 0th index. Therefore, the binary equivalent of given decimal number (50) is 110010
🌐
StudyMite
studymite.com › python › examples › program-to-convert-decimal-to-binary-in-python
Decimal to Binary in Python | StudyMite
7 hours ago - Here we'll write a program to convert decimal to binary in Python with explanation, code, and algorithm.
🌐
BeginnersBook
beginnersbook.com › 2018 › 02 › python-program-to-convert-decimal-to-binary
Python Program to Convert Decimal to Binary
In this program we have defined a function decimalToBinary() for the conversion. This function takes the decimal number as an input parameter and converts it into an equivalent binary number. def decimalToBinary(num): """This function converts decimal number to binary and prints it""" if num ...
🌐
WsCube Tech
wscubetech.com › resources › python › programs › convert-decimal-binary-octal-and-hexadecimal
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
October 28, 2025 - Learn how to write a Python program to convert decimal numbers to binary, octal, and hexadecimal formats with this easy-to-follow guide.
🌐
PREP INSTA
prepinsta.com › home › python program › python program for decimal to binary conversion
Decimal to binary conversion in python | PrepInsta
September 29, 2022 - def convertBinary(num): binaryArray = [] while num>0: binaryArray.append(num%2) num = num//2 for j in binaryArray: print(j, end="") decimal_num = 21 convertBinary(decimal_num)
🌐
3Ri Technologies
3ritechnologies.com › how-to-convert-binary-to-decimal-in-python
How to Convert Binary to Decimal in Python - Pune
November 7, 2025 - The int() function takes the binary input and outputs the decimal equivalent of the integer value of the Binary. The decimal result can be used in a mathematical calculation or saved in a variable for later use.
🌐
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
🌐
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 - Then do the n%1 for getting binary number. Take the input decimal number as 9. 9/2. Get the quotient as 4 (n>1) and remainder as 1. 4/2. Get the quotient as 2 (n>1) and remainder as 0. 2/2. Get the quotient as 1 (not greater n>1). And remainder as 0. ... Now write the output from last to first step remainder values like 1001. ... Now let us implement this concept in the python program.
🌐
YouTube
youtube.com › watch
Python program to convert decimal to binary number tutorial
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