Complete Guide To Binary To Decimal Conversion In Python

# Complete Guide To Binary To Decimal Conversion In Python

Last updated:
31st Dec, 2022
Views
3 Mins
View All

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.

#### Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.
Get Free Consultation

Select
Select Area of interest
Select Work Experience
By clicking 'Submit' you Agree to

## Suggested Blogs

101463
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno

07 Jul 2024

142205
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or

07 Jul 2024

16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on

04 Jul 2024

82582
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes

04 Jul 2024

9996
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par

03 Jul 2024

70136
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right

02 Jul 2024

51846
In my experience with Data Science, I’ve found that choosing the right data structure is crucial for organizing information effectively. Graphs

01 Jul 2024

14852
The banking sector has many applications for programming and IT solutions. If you’re interested in working on a project for the banking sector,