0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Binary to Decimal Number

You can convert a binary string to decimal in Python using decimal_number = int(binary_string, 2), where binary_string is your binary input.
📋

Examples

Input0
Output0
Input1011
Output11
Input100000
Output32
🧠

How to Think About It

To convert binary to decimal, think of each binary digit as a power of 2, starting from the right with power 0. Multiply each digit by 2 raised to its position index and add all these values together to get the decimal number.
📐

Algorithm

1
Get the binary number as a string input.
2
Start from the rightmost digit and assign it position 0.
3
For each digit, multiply it by 2 raised to the power of its position.
4
Add all these values to get the decimal equivalent.
5
Return or print the decimal number.
💻

Code

python
binary_string = input("Enter a binary number: ")
decimal_number = int(binary_string, 2)
print(f"Decimal number: {decimal_number}")
Output
Enter a binary number: 1011 Decimal number: 11
🔍

Dry Run

Let's trace the input '1011' through the code.

1

Input binary string

binary_string = '1011'

2

Convert using int() with base 2

decimal_number = int('1011', 2) which equals 11

3

Print the decimal number

Output: Decimal number: 11

Binary DigitPositionValue (digit * 2^position)
101 * 2^0 = 1
111 * 2^1 = 2
020 * 2^2 = 0
131 * 2^3 = 8
💡

Why This Works

Step 1: Using int() with base 2

The int() function can convert strings in any base to decimal by specifying the base as 2 for binary.

Step 2: Binary digit powers

Each binary digit represents a power of 2, starting from 0 on the right, which int() uses internally.

Step 3: Simple and efficient

This method avoids manual calculation and is fast and reliable for any binary input.

🔄

Alternative Approaches

Manual calculation with loop
python
binary_string = input("Enter binary: ")
decimal = 0
for i, digit in enumerate(binary_string[::-1]):
    decimal += int(digit) * (2 ** i)
print(f"Decimal number: {decimal}")
This method shows the step-by-step calculation but is longer and less efficient than using int().
Using recursion
python
def binary_to_decimal(b_str):
    if not b_str:
        return 0
    return binary_to_decimal(b_str[:-1]) * 2 + int(b_str[-1])

binary_string = input("Enter binary: ")
print(f"Decimal number: {binary_to_decimal(binary_string)}")
Recursion illustrates the concept but is less practical for large inputs due to call stack limits.

Complexity: O(n) time, O(1) space

Time Complexity

The conversion scans each digit once, so it takes linear time proportional to the length of the binary string.

Space Complexity

The method uses constant extra space since it only stores the input and the result.

Which Approach is Fastest?

Using int() with base 2 is the fastest and simplest. Manual loops or recursion add overhead and complexity.

ApproachTimeSpaceBest For
int() with base 2O(n)O(1)Quick and reliable conversion
Manual loop calculationO(n)O(1)Learning and understanding binary math
RecursionO(n)O(n)Conceptual demonstration, not large inputs
💡
Use Python's built-in int() with base 2 for quick and error-free binary to decimal conversion.
⚠️
Beginners often forget to specify base 2 in int(), causing errors or wrong results.