How to convert a decimal number to binary in Python
Gaining an understanding of how to convert decimal numbers into binary numbers in Python is an essential skill that will improve our coding skills and number system comprehension. This information helps us solve problems more effectively and gives us the resources we need to take on challenging programming tasks. Gaining proficiency in both the manual algorithm and Python's built-in functions will equip us to develop code that works better and more effectively. Let’s find out how we can improve our Python skills and solidify our foundation!
Binary numbers are the numbers represented with base 2, whereas decimal numbers are represented with base 10.
Algorithm to convert decimal number to binary
Assuming we have a decimal number decimal that we convert to its binary equivalent:
Divide the decimal number by 2, and save the remainder into a variable
remainder, and the quotient into a variablequotient.We keep on dividing the variable
quotientby 2, and saving the attained quotient intoquotientuntilquotientbecomes 0.Each time, the attained remainder is appended at the start of the
remainder.
When
quotientbecomes 0, the value ofremainderis our binary equivalent of the decimal numberdecimal.
Implementation of algorithm
We can implement the aforementioned algorithm in Python in the following manner:
# Decimal number to be converteddecimal = 60# Initializing quotient and remainderquotient = decimalremainder = ""# Dividing until quotient is equal to zerowhile (quotient != 0):remainder = str(quotient % 2) + remainderquotient = quotient // 2# Printing the binary number attained from the remainderprint (remainder)
Code explanation
Let's breakdown the code written above:
Line 2: We have the decimal number that we would like to convert into binary. We can alter the number and test out different numbers.
Lines 9–11: We divide the decimal numbers and attain the quotients and remainders as per our algorithm. We keep appending the remainder to the variable
remainderwhich represents the binary equivalent when the quotient becomes 0.
In-built conversion in Python
It is worth noting that in our example, remainder is a string, and our binary equivalent is being stored as a string. Python has an in-built function bin() that returns a binary number when we pass a decimal number as a parameter. We can see this in action below:
decimal = 60binary = bin(decimal)print(binary)
The prefix 0b appended to the number helps differentiate that it is binary as opposed to being a decimal number.
Conclusion
Python coders must understand how to convert decimal number to binary. It improves our capacity to develop effective code and our comprehension of number systems. By combining Python's in-built functions with customized algorithms, we can get a flexible toolkit for handling a wide range of programming issues. This knowledge enables us to create software of the highest caliber and overcome obstacles with greater efficiency.
Free Resources