
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Decimal to binary list conversion in Python
Python being a versatile language can handle many requirements that comes up during the data processing. When we need to convert a decimal number to a binary number we can use the following python programs.
Using format
We can Use the letter in the formatter to indicate which number base: decimal, hex, octal, or binary we want our number to be formatted. In the below example we take the formatter as 0:0b then supply the integer to the format function which needs to get converted to binary.
Example
Dnum = 11 print("Given decimal : " + str(Dnum)) # Decimal to binary number conversion binnum = [int(i) for i in list('{0:0b}'.format(Dnum))] # Printing result print("Converted binary list is : ",binnum)
Output
Running the above code gives us the following result −
Given decimal : 11 Converted binary list is : [1, 0, 1, 1]
Using bin
The bin() is a in-built function which can also be used in a similar way as above. This function Python bin() function converts an integer number to a binary string prefixed with 0b. So we slice the first two characters.
Example
Dnum = 11 print("Given decimal : " + str(Dnum)) # Decimal to binary number conversion binnum = [int(i) for i in bin(Dnum)[2:]] # Printing result print("Converted binary list is : ",binnum)
Output
Running the above code gives us the following result −
Given decimal : 11 Converted binary list is : [1, 0, 1, 1]
- Related Articles
- Decimal to Binary conversion\n
- Program for Binary To Decimal Conversion in C++
- Program for Decimal to Binary Conversion in C++
- Decimal to binary conversion using recursion in JavaScript
- C Program for Decimal to Binary Conversion?
- Decimal to Binary conversion using C Programming
- Java Program for Decimal to Binary Conversion
- Program to convert Linked list representing binary number to decimal integer in Python
- Conversion between binary, hexadecimal and decimal numbers using Coden module
- Dictionary to list of tuple conversion in Python
- List of tuples to dictionary conversion in Python
- Binary to decimal and vice-versa in Python
- Convert decimal to binary number in Python program
- Binary to BCD conversion in 8051
- BCD to binary conversion in 8051
