Programs

Complete Guide To Binary To Decimal Conversion In Python

Python, being a versatile language, is capable of implementing several functionalities. Converting any number from binary to decimal is one of them. This article will focus on the basics of the number system and how to convert binary to Decimal in python. 

In terms of programming, both decimal systems, as well as binary systems, are used. Apart from these two number systems, hexadecimal and octal number systems are also used. Normally, while using a computer binary number system is used & for normal systems, the decimal system is used.  With the growing demand for the language, it is important to be well-versed with these number system conversions. The process of conversion from binary to decimal in python can be done using different methods. Let us now look into what is a binary number and how to convert it into a decimal number using python.

Check out our data science courses to upskill yourself.

Binary System

The Binary system is basically defined as a number system in which the numerical values are represented using two digits only – 0 & 1. This system is used by all the computers and also for programming. 

To do the conversion from binary to decimal in python, we first need to understand how the binary number system works. 

Converting binary to decimal is all about understanding the math of the power of 2. For eg.

      21= 2,  22=4, 23=8, 24=16, 25=32, 26=64 and so on.

Converting Binary To Decimal

To understand this process of conversion, let us take an example.

From binary to decimal

Input : 1011

Output : 11

EXPLANATION:

Following are the steps of the above conversion

1). For the given binary number, find its modulo with 10

    (1011 % 10 = 1)

2). Next, multiply the remainder with 2 raised to the power position from the right end. (position is counted  starting from 0). In the case of 1011, the last one to the right has the 0th position.

    (1 * 2^0)

3). The result is to be added with the result generated previously.

    dec = dec + (1 * 2^0)

4). Divide the binary number by 10 and update it.

    (1011 / 10 = 101)

5). Till the binary number is greater than 0, repeat the above steps.

Final step -: (1 * 2^3) + (0 * 2^2) +

                 (1 * 2^1) + (1 * 2^0) = 11

Program To Convert Binary To Decimal in Python

  • Let us now take a sample code to understand how the conversion from binary to decimal in python is done.
 #program to convert binary number to decimal number

def binToDec(n):

return int(n,2)

#driver code

if _name_= = ‘_main_’:

print(binToDec(‘1011’))

print(binToDec(‘100’))

print(binToDec(‘001’))

print(binToDec(‘101’))

#OUTPUT: 11, 4, 1, 5

  • Binary to Decimal conversion in Python using int() method

In this case, the input will be given by the user.

#program to convert binary to decimal using int() method

bin_num= input(“Enter the no. in binary format”);

dec_num= int(bin_num,2);

print(bin_num, “in decimal=”,dec_num);

#INPUT - 10110

#OUTPUT – 22

Conclusion

This brings us to the end of the article on binary to decimal in python conversion. The process of conversion from one number system to another in python is crucial to ensure smooth implementation of functionalities.

If you’d want to dive deeper into working with Python, especially for data science, upGrad brings you the Executive PGP in Data Science. This program is designed for mid-level IT professionals, software engineers looking to explore Data Science, non-tech analysts, early career professionals, etc. Our structured curriculum and extensive support ensure our students reach their full potential without difficulties. 

Also, Check out all trending Python tutorial concepts in 2024.

Want to share this article?

Become a Certified Data Scientist

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Data Science Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks