Python Programming (138 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (92 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How To Convert Decimal To Binary In Python

Last updated on Jul 15,2021 13.9K Views

34 / 62 Blog from Python Fundamentals

Python is a highly versatile and capable programming language. Among the many things one can do, converting from decimal to binary and vice versa is one that stands out the most. Thus in this article, we will speak more about how to convert decimal to binary in Python and vice versa.

Following pointers will be covered in this article,

Let’s begin!

To understand what this operation means, check out the example below. 

From decimal to binary

Input : 8

Output : 1 0 0 0

From binary to decimal

Input : 100

Output : 4

Let us see how to convert Decimal to Binary in Python,

Converting Decimal To Binary In Python

In order to convert decimal to binary, take a look at the example below.

Keep calling conversion function with n/2  till n > 1,

later perform n % 1 to get MSB of converted binary number.  

Example: 7

1). 7/2 = Quotient = 3(grater than 1), Remainder = 1.

2). 3/2 = Quotient = 1(not grater than 1), Remainder = 1.

3). 1%2 = Remainder = 1.

Therefore, answer is 111.

Let us see a sample program,

Sample Program

# Function to print binary number for the
# input decimal using recursion
def decimalToBinary(n):
if(n > 1):
# divide with integral result
# (discard remainder)
decimalToBinary(n//2)
print(n%2, end=' ')
# Driver code
if __name__ == '__main__':
decimalToBinary(8)
print("n")
decimalToBinary(18)
print("n")
decimalToBinary(7)
print("n") 

The output of the above program will look something like this. 

1000

10010

111

We can convert Decimal To Binary in Python using bin function as well, let us see how,

Making Use Of Bin Function

#Function to convert Decimal number 
# to Binary number 
def decimalToBinary(n): 
return bin(n).replace("0b","") 
# Driver code 
if __name__ == '__main__': 
print(decimalToBinary(8)) 
print(decimalToBinary(18)) 
print(decimalToBinary(7))

The output of the above program will look something like this

1000

10010

111

Now that you know how to convert from decimal to binary in Python, let’s see how to do the reverse that is binary to decimal. 

Binary To Decimal In Python

To understand this better, refer to the example below.

Example: 1011

1). Take modulo of given binary number with 10. 

    (1011 % 10 = 1)

2). Multiply rem with 2 raised to the power

    it’s position from right end. 

    (1 * 2^0)

    Note that we start counting position with 0. 

3). Add result with previously generated result.

    decimal = decimal + (1 * 2^0)

4). Update binary number by dividing it by 10.

    (1011 / 10 = 101)

5). Keep repeating upper steps till binary > 0.

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

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

Let us take a look at a sample program,

Sample Program

When the above program is executed, the output will look like this. 

4

5

9

Let us move to the last bit of this Decimal to Binary in Python article.

Sample Program

# Function to convert Binary number 
# to Decimal number 
def binaryToDecimal(n): 
return int(n,2) 
# Driver code 
if __name__ == '__main__': 
print(binaryToDecimal('100')) 
print(binaryToDecimal('101'))
print(binaryToDecimal('1001'))

The output of the above program will be

4

5

9

This brings us to the end of this article on Decimal To Binary In Python.

To get in-depth knowledge on Python along with its various applications, you can enroll now for the best Python course online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of this article and we will get back to you.

Upcoming Batches For Python Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th March,2023

25th March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Convert Decimal To Binary In Python

edureka.co