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

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905263
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20923
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5068
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5179
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17645
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10803
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

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

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
139134
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

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