#12 Python Tutorial for Beginners | Number System Conversion in Python

 

#12 Python Tutorial for Beginners | Number System Conversion in Python

Binary Formats in Python

Welcome back, aliens! My name is Naveen Ready and in this blog post, we'll continue our series on Python by discussing binary formats. In the programming world, we commonly work with decimal and binary systems. However, there are two other number systems that we use: octal and hexadecimal. In this post, we'll learn how to convert numbers from one system to another using Python.

Converting Decimal to Binary

Let's start by understanding how to convert a decimal number to binary. To do this, we can use the bin function in Python. The base of the decimal system is 10, while the base of the binary system is 2. In the decimal system, we have 10 digits (0-9), whereas in the binary system, we only have 2 digits (0 and 1).

For example, let's convert the number 25 to binary. By using the bin function, we can easily get the binary format of 25, which is 0b11001.

But how do we know if this is the correct output? Let me explain how to convert a decimal number to binary manually. Let's take the number 25 again as an example. To convert this to binary, we start by dividing the number by 2 repeatedly until we reach 0. The remainders of each division give us the binary digits. In this case, the binary format of 25 is 11001.

When using the bin function, it's important to note that the binary format is represented by the prefix 0b. So, the binary format of 25 is 0b11001.

Converting Binary to Decimal

We can also convert a binary number to decimal using Python. To do this, we simply use the binary prefix 0b followed by the binary number. For example, if we have the binary number 0b10101, we can convert it to decimal by using the int function, which gives us the decimal value of 21.

Converting Decimal to Octal

Similarly, we can convert a decimal number to octal using Python. The octal prefix is 0o. For example, if we want to convert the decimal number 25 to octal, we use the oct function, which gives us the octal value of 31 (0o31).

Converting Decimal to Hexadecimal

Lastly, we can convert a decimal number to hexadecimal using Python. The hexadecimal prefix is 0x. For example, if we want to convert the decimal number 25 to hexadecimal, we use the hex function, which gives us the hexadecimal value of 25 (0x19).

In conclusion, we have learned how to convert numbers between decimal, binary, octal, and hexadecimal systems using Python. Understanding these conversions is important when working with different number systems in programming. In future blog posts, we'll explore the bitwise operators in Python, which rely on these conversions. To practice what you've learned, try converting numbers from one system to another and vice versa. Enjoy coding!

Example

# Python program to convert decimal into other number systems
dec = 344

print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
Output
The decimal value of 344 is:
0b101011000 in binary.
0o530 in octal.
0x158 in hexadecimal.

Comments

Popular posts from this blog

#22 Python Tutorial for Beginners | Break Continue Pass in Python

#14 Python Tutorial for Beginners | IDLE Previous Command | Clear Screen?

Python Tutorial for Beginners