top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Decimal to Binary Conversion using Python

Writer's picture: Magdy AhmedMagdy Ahmed


In this post, you will learn how to convert a decimal into an equivalent binary number using Python. Here, I have mentioned two ways to do that:


1.Using loops


Step 1. Divide the integer by 2, while noting the remainder.

    while(decimal > 0):  
        rem = decimal % 2

Step 2. Then, append this remainder to a list. then, divide the quotient again by 2.

        binary_num_list.append(rem)
        decimal = decimal // 2

Step 3. Like this, keep dividing each successive quotient by 2 until you get a quotient of zero.


Step 4. After this, we reverse the order of the list to get the binary representation of

binary_num_list.reverse()    
print("Binary Equivalent is: ")
for i in binary_num_list:
    print(i, end=" ")  

Here's the full code:

User_input=int(input("Enter a number: "))

binary_num_list=[]

def decimal_to_binary(decimal):
        
    while(decimal > 0):
        
        rem = decimal % 2
        
        binary_num_list.append(rem)
        
        decimal = decimal // 2
        
    binary_num_list.reverse()
    
    print("Binary Equivalent is: ")
    
    for i in binary_num_list:
        
        print(i, end=" ")

2.Using recursive function


A recursive function is a function that calls itself during its execution something. For this, we have defined a function and taken a decimal number as an input and converted it to an equivalent binary number.

User_input = int(input("Enter a number: "))

print("Binary Equivalent is: ")

def decimal_to_binary(decimal):
            
    if decimal > 1:
        
        decimal_to_binary(decimal // 2)        
        
    print(decimal % 2, end = " ")

Personally I think this conversion is better and much more efficient than the first one.

1 comment

Recent Posts

See All

1 comentario


Data Insight
Data Insight
19 sept 2021

Nice. You did not test the code though, i.e no example.

Me gusta

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page