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 approach follows the same logic as the divide-by-2 method but builds the binary number by concatenating the results. While simple to understand, this method can be slower due to the overhead of string manipulation in each iteration. ... n = 7 # decimal number res = '' # binary result while n > 0: res = str(n % 2) + res n //= 2 print(res)
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
Converting Binary to Decimal in python (without built in binary function) - Stack Overflow
Alrighty, first post here, so please forgive and ignore if the question is not workable; Background: I'm in computer science 160. I haven't taken any computer related classes since high school, so More on stackoverflow.com
🌐 stackoverflow.com
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
1 week 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.
🌐
DataCamp
datacamp.com › tutorial › python-data-type-conversion
Python Data Type Conversion: A Guide With Examples | DataCamp
February 16, 2025 - Let's see this with a more complex example to make it clear: Binary Number = 1001111 Decimal value = (1*(2^6)) + (0*(2^5)) + (0*(2^4)) + (1*(2^3)) + (1*(2^2)) + (1*(2^1)) + (1*(2^0)) = 79 · In Python, you can simply use the bin() function to convert from a decimal value to its corresponding ...
🌐
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
Find elsewhere
🌐
Replit
replit.com › home › discover › how to convert binary to decimal in python
How to convert binary to decimal in Python
February 6, 2026 - Python's built-in int() function makes this process straightforward. You'll learn several conversion techniques and get practical tips. You will also explore real-world applications and find advice on how to debug common errors to help you master ...
🌐
NareshIT
nareshit.com › blogs › how-to-convert-decimal-to-binary-in-python
How to Convert Decimal to Binary in Python | Step-by-Step Guide
Here we are also going to discuss that how they are used to support the inbuilt functions which is being provided by the Python library. Basic Procedure for Converting the Decimal value to Binary value:
🌐
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 - This approach does not use the built-in function but manually convert the Binary number into Decimal Number using for loop in Python. ... Binary is base-2, meaning each digit represents a power of 2, from right to left.
🌐
Medium
medium.com › @pivajr › pythonic-tips-how-to-convert-binary-to-decimal-in-python-9f781b029130
Pythonic Tips: How to Convert Binary to Decimal in Python | by Dilermando Piva Junior | Medium
May 12, 2025 - The int() function is one of the most direct ways to convert a binary number (as a string) into a decimal. You just need to pass the string and specify the base (2 for binary). ... Python interprets the string and returns the corresponding decimal ...
🌐
Scaler
scaler.com › home › topics › binary to decimal in python
Binary to Decimal in Python - Scaler Topics
April 18, 2022 - This example shows us how to convert binary number to decimal number in python using the inbuilt function int.
🌐
Reddit
reddit.com › r/pythontips › convert decimal input to 8 bit binary
r/pythontips on Reddit: Convert decimal input to 8 bit binary
October 29, 2023 -

Hi all i'm very new to coding (a few weeks) and Python and was wondering if there is a way to get an input of a decimal number and convert it to 8 bit binary. Currently using the Thonny IDE

🌐
Django Central
djangocentral.com › convert-binary-number-to-decimal-and-vice-versa
Python Program to Convert Binary Number to Decimal and Vice-Versa
# Taking binary input binary = input("Enter a binary number:") # Calling the function BinaryToDecimal(binary) def BinaryToDecimal(binary): decimal = 0 for digit in binary: decimal = decimal*2 + int(digit) print("The decimal value is:", decimal) ... We can use the built-in int() function to ...
🌐
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 - Converting numbers between decimal, binary, octal, and hexadecimal formats in Python is straightforward thanks to the built-in functions bin(), oct(), and hex(). These functions return strings that are easy to manipulate if you need to remove ...
🌐
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.
🌐
Python.org
discuss.python.org › python help
Program that converts the Binary string to the Numeric - Python Help - Discussions on Python.org
August 13, 2023 - b=‘1111’ i=0 while b: i = i*2+(ord(b[0])-ord(‘0’)) b=b[1:] print(‘Decimal value’+ str(i) Can anyone explain what does ord(b[0]) do here?
🌐
3Ri Technologies
3ritechnologies.com › how-to-convert-binary-to-decimal-in-python
How to Convert Binary to Decimal in Python | 3RI
November 7, 2025 - ‘0’ or ‘1’ should be the characters in the string that is used as the input to represent this number. The second and third kinds of text can be converted to a decimal integer using Python’s int() method.
🌐
Intellipaat
intellipaat.com › home › blog › how to convert binary to decimal in python
How to Convert Binary to Decimal in Python [2026 UPDATED]
May 30, 2025 - This number should be written as a string, with each character being either ‘0’ or ‘1’. ... The int() method in Python can be used to convert a binary text to a decimal integer.