Python BasicsEasy

Binary to Decimal conversion

Detailed guide and Python implementation for the 'Binary to Decimal conversion' problem.

Problem Statement

Easy

Write a function binary_to_decimal(binary_str) that takes a string representing a binary number (containing only '0' and '1') and returns its decimal (base-10) integer equivalent.

Each digit in a binary number represents a power of 2, starting from the rightmost digit (2^0). For example, binary '1010' = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10.

Constraints
  • 1 <= len(binary_str) <= 20
  • binary_str contains only '0' and '1'

Examples

Example 1
Input
binary_to_decimal('1010')
Output
10
Explanation

1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10.

Example 2
Input
binary_to_decimal('1111')
Output
15
Explanation

1×2³ + 1×2² + 1×2¹ + 1×2⁰ = 8 + 4 + 2 + 1 = 15.

Example 3
Input
binary_to_decimal('10000')
Output
16
Explanation

1×2⁴ = 16.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.