Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconComplete Guide To Binary To Decimal Conversion In Python

Complete Guide To Binary To Decimal Conversion In Python

Last updated:
31st Dec, 2022
Views
Read Time
3 Mins
share image icon
In this article
Chevron in toc
View All
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.

Profile

Rohit Sharma

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

Explore Free Courses

Suggested Blogs

Data Mining Techniques & Tools: Types of Data, Methods, Applications [With Examples]
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
Read More

by Rohit Sharma

07 Jul 2024

An Overview of Association Rule Mining & its Applications
142205
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

07 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples & Applications
16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
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
Read More

by Rohit Sharma

04 Jul 2024

Most Common Binary Tree Interview Questions & Answers [For Freshers & Experienced]
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
Read More

by Rohit Sharma

03 Jul 2024

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

by Rohit Sharma

02 Jul 2024

Graphs in Data Structure: Types, Storing & Traversal
51846
In my experience with Data Science, I’ve found that choosing the right data structure is crucial for organizing information effectively. Graphs
Read More

by Rohit Sharma

01 Jul 2024

Python Banking Project [With Source Code] in 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,
Read More

by Rohit Sharma

25 Jun 2024

Linear Search vs Binary Search: Difference Between Linear Search & Binary Search
66256
In my journey through data structures, I’ve navigated the nuances of linear search vs binary search in data structure, especially when dealing w
Read More

by Rohit Sharma

23 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon