Binary to Decimal Converter in Python.
About the project:
This beginner-friendly Python project demonstrates how to convert a binary number(a number made up of only 0s and 1s) into its decimal equivalent.
Binary to decimal conversion is a fundamental concept in computer science, as computers internally represent data using binary numbers. This project helps beginners understand both the logic behind the conversion and how to implement it step by step in Python.
The program runs in a continuous loop, allowing you to perform multiple conversions without restarting the script.
This project converts user-input binary numbers into decimal values while validating the input to prevent incorrect data.
How to use this Binary to Decimal Converter:
- Run the Python script.
- When you run it, you can enter any binary number.
- It will immediately convert it to its decimal form.
- The program will continue to run until you type q to quit.
How Binary to Decimal Conversion Works
In the binary number system, each digit represents a power of 2. Starting from the rightmost digit:
- Each position is a power of 2 (2⁰, 2¹, 2², ...)
- If the digit is 1, the value is added
- If the digit is 0, the value is ignored
Example: Binary 1011
- 1 × 2³ = 8
- 0 × 2² = 0
- 1 × 2¹ = 2
- 1 × 2⁰ = 1
Total = 8 + 2 + 1 = 11
Project Level: Beginner
Concepts Covered: Loops, functions, input validation, number systems.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
Sample Input and Output
Enter a binary number: 1010 The decimal equivalent of 1010 is 10. Enter a binary number: 111 The decimal equivalent of 111 is 7.
# binary_to_decimal_converter.py
def binary_to_decimal(binary_string):
"""
Converts a binary string to its decimal integer equivalent.
Args:
binary_string (str): A string containing only '0's and '1's.
Returns:
int: The decimal equivalent of the binary string.
"""
decimal_value = 0
power = 0
# Iterate through the binary string from right to left
# This is equivalent to `binary_string[::-1]`
for digit in reversed(binary_string):
if digit == '1':
# Add 2 raised to the power of its position
decimal_value += 2 ** power
power += 1
return decimal_value
def is_valid_binary(binary_string):
"""
Checks if a string contains only '0's and '1's.
"""
return all(char in '01' for char in binary_string)
def main():
"""
Main function to run the Binary to Decimal Converter in a continuous loop.
"""
print("--- Python Binary to Decimal Converter ---")
print("Enter a binary number (e.g., 1010) or 'q' to quit.")
while True:
user_input = input("\nEnter a binary number: ").strip()
# Check if the user wants to quit
if user_input.lower() == 'q':
print("Exiting Binary to Decimal Converter. Goodbye!")
break
# Check for empty input
if not user_input:
print("Please enter a binary number.")
continue
# Validate the input
if is_valid_binary(user_input):
decimal_result = binary_to_decimal(user_input)
print(f"The decimal equivalent of {user_input} is {decimal_result}.")
else:
print("Invalid input. Please enter a number containing only 1s and 0s.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
If you are new to Python basics, you may also find these tutorials helpful:
Conclusion
This Binary to Decimal Converter is a great beginner project that combines logical thinking with Python fundamentals. By building this project, you learn how number systems work and how Python can be used to process and validate user input effectively.
Try extending this project by adding decimal-to-binary conversion or handling larger binary numbers.
Click to explore more Beginner projects
