In this tutorial, I will explain how to convert decimal numbers to binary in Python. As a Python developer working on various projects in the USA, I often encounter situations where I need to convert numbers between different bases. Python provides several ways to perform this conversion, and I’ll walk you through the most common and efficient methods.
Decimal and Binary Number Systems
Before we get into the conversion process, let’s discuss what decimal and binary number systems are:
- Decimal Number System: The decimal number system, also known as base-10, is the most widely used number system in our daily lives. It uses ten digits (0-9) to represent numbers. For example, the number 42 in decimal represents 4 tens and 2 ones.
- Binary Number System: The binary number system, or base-2, is the fundamental number system used in computing. It uses only two digits (0 and 1) to represent numbers. Each digit in a binary number is called a bit. For example, the binary representation of the decimal number 42 is 101010.
Read How to Round Numbers to 2 Decimal Places in Python?
Convert Decimal to Binary in Python
Python provides several ways to convert decimals to binary numbers. Let us see some important methods.
Check out How to Generate Credit Card Numbers in Python for Testing?
Method 1. Use the Built-in bin() Function
Python provides a built-in function bin() that allows you to easily convert an integer to its binary representation. Here’s an example:
decimal_num = 42
binary_num = bin(decimal_num)
print(binary_num) Output:
0b101010I have executed the above example code and added the screenshot below.

The bin() function takes an integer as input and returns a string prefixed with ‘0b’ to indicate that it’s a binary representation. If you want to remove the ‘0b’ prefix, you can slice the string:
binary_num = bin(decimal_num)[2:]
print(binary_num) Output:
101010Read How to Check if a String Contains Only Alphanumeric Characters and Underscores in Python?
Method 2. Use a Custom Function
If you want more control over the conversion process or want to understand the underlying logic, you can create a custom function to convert decimal to binary. Here’s an example:
def decimal_to_binary(decimal):
if decimal == 0:
return "0"
binary = ""
while decimal > 0:
binary = str(decimal % 2) + binary
decimal //= 2
return binary
decimal_num = 42
binary_num = decimal_to_binary(decimal_num)
print(binary_num) Output:
101010I have executed the above example code and added the screenshot below.

In this function, we use a while loop to repeatedly divide the decimal number by 2 and capture the remainder. The remainder are then concatenated in reverse order to form the binary representation.
Check out How to Convert Letters to Numbers in Python?
Method 3. Use Bitwise Operations
Another approach to convert decimal to binary is by using bitwise operations. Here’s an example:
def decimal_to_binary(decimal):
if decimal == 0:
return "0"
binary = ""
while decimal > 0:
binary = str(decimal & 1) + binary
decimal >>= 1
return binary
decimal_num = 42
binary_num = decimal_to_binary(decimal_num)
print(binary_num) Output:
101010I have executed the above example code and added the screenshot below.

In this method, we use the bitwise AND operator (&) to extract the least significant bit of the decimal number in each iteration. We then right-shift the decimal number by 1 using the >>= operator to move to the next bit. The extracted bits are concatenated in reverse order to form the binary representation.
Read How to Pad Numbers with Leading Zeros in Python?
Example: Convert IP Addresses to Binary
As a programmer working on network-related projects in the USA, I often need to convert IP addresses to their binary representations. Let’s see how we can use Python to achieve this:
def ip_to_binary(ip):
octets = ip.split(".")
binary_octets = [bin(int(octet))[2:].zfill(8) for octet in octets]
binary_ip = ".".join(binary_octets)
return binary_ip
ip_address = "192.168.0.1"
binary_ip = ip_to_binary(ip_address)
print(binary_ip)Output:
11000000.10101000.00000000.00000001In this example, we split the IP address into its octets, convert each octet to binary using a list comprehension, and then join the binary octets with dots to form the binary representation of the IP address.
Check out How to Check if Input is a Number in Python?
Conclusion
In this tutorial, I have explained how to convert decimal numbers to binary in Python. I covered various methods including the built-in bin() function, custom functions , and bitwise operations. I also discussed a real-world example of converting IP addresses to binary.
You may also like to read:
- What are Floating Point Numbers in Python?
- How to Count the Number of Digits in a Number in Python?
- How to Split a Number into Digits in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.