Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
How to Convert Binary to Decimal in Python? Program With Examples
In programming, having an understanding of different number systems is essential for effectively representing and manipulating data. While decimal numbers (base-10) are commonly used in our everyday lives, computers primarily operate using binary numbers (base-2).
The knowledge to convert binary to decimal in Python is a fundamental skill for any programmer, as it enables efficient data interpretation and processing.
The binary system consists of only two digits: 0 and 1. Each digit in a binary number represents a specific power of 2. Python program to convert binary to decimal allows us to comprehend and work with binary data in a human-readable format.
This skill is crucial in various programming domains, such as low-level programming, digital signal processing, data analysis, and cryptography.
If you are a beginner, you must check out our free Python Tutorial, or choose the expert-led Python Course.
In this post, we will explore the process of converting binary to decimal in Python programming language. We will provide a practical guide, complete with examples, to help you understand the concept and implement binary-to-decimal conversions in your Python projects effectively. You can practice the given Python programs with the online Python Compiler.
Python Program to Convert Binary to Decimal
Code
def binary_to_decimal(binary):
decimal = 0
power = 0
while binary > 0:
last_digit = binary % 10
decimal += last_digit * (2 ** power)
binary //= 10
power += 1
return decimal
binary_number = input("Enter a binary number: ")
decimal_number = binary_to_decimal(int(binary_number))
print("Decimal equivalent:", decimal_number)
Output
Enter a binary number: 363
Decimal equivalent: 27
Explanation
In this program, we define a function called binary_to_decimal that takes a binary number as input and returns its decimal equivalent.
We use a while loop to iterate through each digit of the binary number. In each iteration, we extract the last digit using the modulus operator % and add it to the decimal number after multiplying it by the corresponding power of 2.
We then update the binary number by integer division // to remove the last digit, and increment the power by 1.
This process continues until the binary number becomes 0. Finally, we prompt the user to enter a binary number, convert it to an integer, call the binary_to_decimal function, and print the decimal equivalent.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program