How to convert a decimal number to binary in Python

Gaining an understanding of how to convert decimal numbers into binary numbers in Python is an essential skill that will improve our coding skills and number system comprehension. This information helps us solve problems more effectively and gives us the resources we need to take on challenging programming tasks. Gaining proficiency in both the manual algorithm and Python's built-in functions will equip us to develop code that works better and more effectively. Let’s find out how we can improve our Python skills and solidify our foundation!

Binary numbers are the numbers represented with base 2, whereas decimal numbers are represented with base 10.

Algorithm to convert decimal number to binary

Assuming we have a decimal number decimal that we convert to its binary equivalent:

  • Divide the decimal number by 2, and save the remainder into a variable remainder, and the quotient into a variable quotient .

  • We keep on dividing the variable quotient by 2, and saving the attained quotient into quotient until quotient becomes 0.

    • Each time, the attained remainder is appended at the start of the remainder .

  • When quotient becomes 0, the value of remainder is our binary equivalent of the decimal number decimal .

Here we have 8 as dividend and 4 as divisor
Here we have 8 as dividend and 4 as divisor
1 of 5

Implementation of algorithm

We can implement the aforementioned algorithm in Python in the following manner:

# Decimal number to be converted
decimal = 60
# Initializing quotient and remainder
quotient = decimal
remainder = ""
# Dividing until quotient is equal to zero
while (quotient != 0):
remainder = str(quotient % 2) + remainder
quotient = quotient // 2
# Printing the binary number attained from the remainder
print (remainder)

Code explanation

Let's breakdown the code written above:

  • Line 2: We have the decimal number that we would like to convert into binary. We can alter the number and test out different numbers.

  • Lines 9–11: We divide the decimal numbers and attain the quotients and remainders as per our algorithm. We keep appending the remainder to the variable remainder which represents the binary equivalent when the quotient becomes 0.

In-built conversion in Python

It is worth noting that in our example, remainder is a string, and our binary equivalent is being stored as a string. Python has an in-built function bin() that returns a binary number when we pass a decimal number as a parameter. We can see this in action below:

decimal = 60
binary = bin(decimal)
print(binary)

The prefix 0b appended to the number helps differentiate that it is binary as opposed to being a decimal number.

Conclusion

Python coders must understand how to convert decimal number to binary. It improves our capacity to develop effective code and our comprehension of number systems. By combining Python's in-built functions with customized algorithms, we can get a flexible toolkit for handling a wide range of programming issues. This knowledge enables us to create software of the highest caliber and overcome obstacles with greater efficiency.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved