- Python Basic Programs
- Python Program Examples
- Python Print Hello World
- Python Get Input from User
- Python Add Two Numbers
- Add Subtract Multiply Divide
- Python Check Even or Odd
- Python Check Prime or Not
- Python Check Alphabet or Not
- Python Check Vowel or Not
- Python Check Leap Year or Not
- Check Reverse equal Original
- Check Positive Negative Zero
- Python Check Armstrong or Not
- Python Check Palindrome or Not
- Python Check Perfect Number
- Python Find Reverse of Number
- Python Count Digits in Number
- Python Add Digits of Number
- Sum of First and Last Digits
- Python Product of Mid Digits
- Sum of Squares of Digits
- Interchange Digits of Number
- Python Sum of n Numbers
- Python Print ASCII Values
- Python Swap Two Numbers
- Python Swap Two Variables
- Python Fahrenheit to Celsius
- Python Celsius to Fahrenheit
- Python Display Calendar
- Python Days into Years, Weeks
- Find Largest of Two Number
- Find Largest of Three Number
- Python Print Fibonacci Series
- Generate Armstrong Numbers
- Python Make Simple Calculator
- Python Add Binary Numbers
- Binary Number Multiplication
- Python Mathematical Programs
- Find Sum of Natural Numbers
- Find Average of n Numbers
- Python Print Multiplication Table
- Print Table using Recursion
- Python Find Average Percentage
- Python Find Grade of Student
- Find Square Root of Number
- Python Print Prime Numbers
- Find Numbers Divisible by
- Python Find Factors of Number
- Python Find Factorial of a Number
- Python Find HCF & LCM
- Python Kilometres to Miles
- Python Find Area of Square
- Python Find Area of Rectangle
- Python Find Area of Triangle
- Python Find Area of Circle
- Python Find Perimeter of Square
- Find Perimeter of Rectangle
- Python Find Perimeter of Triangle
- Find Circumference of Circle
- Python Simple Interest
- Python Solve Quadratic Equation
- Python Different Set of Operations
- Python Display Powers of 2
- Python Find nCr & nPr
- Python Pattern Programs
- Python Print Pattern Programs
- Python Print Diamond Pattern
- Python Print Floyd's Triangle
- Python Print Pascal's Triangle
- Python List Programs
- Python Count Even/Odd in List
- Python Positive/Negative in List
- Python Even Numbers in List
- Python Odd Numbers in List
- Python Sum of Elements in List
- Sum of Odd/Even Numbers
- Python Element at Even Position
- Python Element at Odd Position
- Python Search Element in List
- Python Largest Number in List
- Python Smallest Number in List
- Python Second Largest in List
- Python Second Smallest in List
- Python Insert Element in List
- Python Delete Element from List
- Python Multiply Numbers in List
- Swap Two Elements in List
- Python 1D Array Program
- Python Linear Search
- Python Binary Search
- Python Insertion Sort
- Python Bubble Sort
- Python Selection Sort
- Remove Duplicates from List
- Python Reverse a List
- Python Merge Two List
- Python Copy a List
- Python Conversion Programs
- Python Decimal to Binary
- Python Decimal to Octal
- Python Decimal to Hexadecimal
- Python Binary to Decimal
- Python Binary to Octal
- Python Binary to Hexadecimal
- Python Octal to Decimal
- Python Octal to Binary
- Python Octal to Hexadecimal
- Python Hexadecimal to Decimal
- Python Hexadecimal to Binary
- Python Hexadecimal to Octal
- Python Matrix Programs
- Python Add Two Matrices
- Python Subtract Two Matrices
- Python Transpose Matrix
- Python Multiply Matrices
- Python String Programs
- Python Print String
- Python Find Length of String
- Python Compare Two Strings
- Python Copy String
- Python Concatenate String
- Python Reverse a String
- Python Swap Two Strings
- Python Uppercase to Lowercase
- Python Lowercase to Uppercase
- Python Check Substring in String
- Python Count Character in String
- Count Repeated Characters
- Python Count Word in Sentence
- Python Count Each Vowels
- Python Capitalize Character
- Python Capitalize Word in String
- Python Smallest/Largest Word
- Remove Spaces from String
- Remove Duplicate Character
- Remove Vowels from String
- Remove Punctuation from String
- Python Remove Word in String
- Python Remove Duplicate Words
- WhiteSpace to Hyphens
- Replace Vowels with Character
- Replace Character in String
- Python Sort String in Alphabetical
- Sort Word in Alphabetical Order
- Extract Number from String
- Python Check Anagram Strings
- Python File Programs
- Python Read a File
- Python Write to File
- Python Append Text to File
- Python Copy Files
- Python Merge Two Files
- Python Counts Characters in File
- Python Count Words in File
- Python File Content in Reverse
- Python Lines Contains String
- Python Delete Line from File
- Python Capitalize Word in File
- Python Replace Text in File
- Replace Specific Line in File
- Python Find Size of File
- Python List Files in Directory
- Python Delete Files
- Python Misc Programs
- Python Reverse a Tuple
- Python Merge Two Dictionary
- Python bytes to String
- Python bytearray to String
- Generate Random Numbers
- Python Print Address of Variable
- Python Print Date and Time
- Python Get IP Address
- Python Shutdown/Restart PC
- Python Tutorial
- Python Tutorial
- Give Online Test
- All Test List
- Python Online Test

# Python Program to Convert Decimal to Binary

In this article, we've created some programs in Python to convert decimal number to its equivalent binary value. The decimal number must be entered by user at run-time. Here are the list of programs:

- Decimal to Binary using
**list** - Without using
**list** - Using
**function** - Using
**class** - Using
**bin()** - Shortest Python Code for Decimal to Binary Conversion

**Note - **Before creating these programs, if you're not aware about steps used for the conversion, then refer
to Decimal to Binary Formula and Steps to get every required
things about the topic.

## Decimal to Binary using List

To convert decimal to binary number in Python, you have to ask from user to enter a number in decimal number system, then convert that number into its binary equivalent as shown in the program given below:

print("Enter the Decimal Number: ") dnum = int(input()) i = 0 bnum = [] while dnum!=0: rem = dnum%2 bnum.insert(i, rem) i = i+1 dnum = int(dnum/2) i = i-1 print("\nEquivalent Binary Value is:") while i>=0: print(end=str(bnum[i])) i = i-1 print()

Here is its sample run:

Now enter any number in decimal number system say 50 and then press `ENTER`

key to convert and print its
equivalent value in binary number system as shown in the snapshot given below:

The dry run of above program with user input **50** goes like:

- Initial values,
**dnum=50**(entered by user),**i=0** - Now the condition (of
**while loop**)**dnum!=0**or**50!=0**evaluates to be true, therefore program flow goes inside the loop **dnum%2**or**50%2**or**0**gets initialized to**rem**- Using the following statement

`bnum.insert(i, rem)`

the value of**rem**gets initialized to**bnum[i]**. Therefore**rem**or**0**gets initialized to**bnum[i]**or**bnum[0]** **i+1**or**0+1**or**1**gets initialized to**i****int(dnum/2)**or**int(50/2)**or**25**gets initialized as new value of**dnum**- Now program flow goes back and evaluates the condition of
**while loop**again. That is, the condition**dnum!=0**or**25!=0**evaluates to be true again, therefore program flow again goes inside the loop and evaluates all the four statements of its body - This process continues, until the condition evaluates to be false
- Before condition evaluates to be false, here are the list of values one by one after each evaluation of the loop:
- At first execution:
- rem = 0
- bnum[0] = 0
- i = 1
- dnum = 25

- At second execution:
- rem = 1
- bnum[0] = 1
- i = 2
- dnum = 12

- At third execution:
- rem = 0
- bnum[0] = 0
- i = 3
- dnum = 6

- At fourth execution:
- rem = 0
- bnum[0] = 0
- i = 4
- dnum = 3

- At fifth execution:
- rem = 1
- bnum[0] = 1
- i = 5
- dnum = 1

- At sixth execution:
- rem = 1
- bnum[0] = 1
- i = 6
- dnum = 0

- In this way, the list
**bnum[]**has its value as:- bnum[0] = 0
- bnum[1] = 1
- bnum[2] = 0
- bnum[3] = 0
- bnum[4] = 1
- bnum[5] = 1

- Now print the value of
**bnum[]**list from its last to**0**index.^{th} - Therefore, the binary equivalent of given decimal number (50) is
**110010**

#### Modified Version of Previous Program

This is the modified version of previous program. The **end=** is used to skip printing of an automatic
newline using **print()**. The **str()** method is used to convert any type of value to a string type.

print("Enter Decimal Number: ", end="") d = int(input()) i = 0 b = [] while d!=0: b.insert(i, d % 2) i = i+1 d = int(d / 2) i = i-1 print(end="\nEquivalent Binary Value = ") while i>=0: print(end=str(b[i])) i = i-1 print()

Here is its sample run with user input, **105** as decimal number:

## Decimal to Binary without List

This program does the same job as of previous program, but without using list. You can take its dry run with yourself to understand it in better way:

print("Enter Decimal Number: ", end="") dnum = int(input()) bnum = 0 mul = 1 while dnum>0: rem = dnum%2 bnum = bnum+(rem*mul) mul = mul*10 dnum = int(dnum/2) print("\nEquivalent Binary Value =", bnum)

Here is its sample run with user input, **345** as decimal number input:

## Decimal to Binary using Function

This program uses a user-defined function named **DecToBin()** that receives a number as its argument and returns
its equivalent binary value. Therefore, we've passed the decimal number entered by user to this function, so that
its return value gets initialized to **bnum**. And the value of this variable gets printed on output as binary
equivalent of given decimal number by user at run-time:

def DecToBin(d): b = 0 m = 1 while d>0: b = b + ((d%2)*m) m = m*10 d = int(d/2) return b print("Enter Decimal Number: ", end="") dnum = int(input()) bnum = DecToBin(dnum) print("\nEquivalent Binary Value =", bnum)

This program produces same output as of previous program.

## Decimal to Binary using Class

This is the last program, created using a class named **CodesCracker**. Class is an object-oriented feature of
Python. To access anything like member function of the class, we've to create an object before doing this

Therefore an object named **ob** is created of class **CodesCracker**. So that, we can access its member function named
**DecToBin()** using **dot (.)** operator to do the job. Rest of the things works similar to a normal function.

class CodesCracker: def DecToBin(self, d): b = 0 m = 1 while d>0: b = b + ((d%2)*m) m = m*10 d = int(d/2) return b print("Enter Decimal Number: ", end="") dnum = int(input()) ob = CodesCracker() bnum = ob.DecToBin(dnum) print("\nEquivalent Binary Value =", bnum)

## Decimal to Binary using bin()

This is the program that uses **bin()**, a predefined function of Python to do the same job as previous programs
have done. The **bin()** function returns binary equivalent of value passed as its argument.

print("Enter Decimal Number: ", end="") dnum = int(input()) bnum = bin(dnum) print("\nEquivalent Binary Value =", bnum)

Here is its sample run with user input, **49**:

**Note - **To remove first two characters, add **[2:]** just after **bnum** while printing its value
to print all its element starting from second index. Or just replace the following statement:

print("\nEquivalent Binary Value =", bnum)

with the statement given below:

print("\nEquivalent Binary Value =", bnum[2:])

now the output with same user input, that is **49** looks like:

### Shortest Python Code for Decimal to Binary Conversion

This is the shortest Python code for decimal to binary conversion. This program doesn't print any message. The program receives input from user, and print its binary equivalent, without any extra things like previous programs have:

dnum = int(input()) print(bin(dnum)[2:])

Here is its sample run with user input **59**:

#### Same Program in Other Languages

« Previous Program Next Program »

Follow/Like Us on Facebook

Subscribe Us on YouTube