Podcast Lookup

_12 Python Tutorial for Beginners _ Number System Conversion in Python

Python To You

  • October 14, 2026
  • 8 min
Episode page

About this episode

No description.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

_12 Python Tutorial for Beginners _ Number System Conversion in Python

00:00—
  1. Speaker 1

    Welcome back aliens. My name is Daven Reddy and let's continue with the series on Python. Now in this video, we'll talk about binary formats. In fact, not just binary, we'll talk about different number systems. Normally when you work in the programming world, we work with binary formats and we work with decimal system. So in the programming world, we use a binary system and a decimal system. But apart from this, we have two more systems, which is octal and hexadecimal. Now, when do we use all this stuff? So in the networking field, we use MAC address, right, which is normally defined in hexadecimal format. Same goes for IPv6, which is defined in hexadecimal format. In your computer, we use binary format. In normal system, we use decimal system. So let's understand how do we convert these values from one system to other system. Now of course you can do it on paper or you can do it in a software or use with the help of codes. Now before understanding how do you convert this using Python, let's try it on paper first. You know we can, in fact we can do it. So if I open my IDLE, you can convert a number from decimal to binary format. Example if I want to convert a number let's say 25. So we have this number 25, I want to convert this into a binary format. Now I don't know how many people of you how many of you know how to convert a number into binary format so people who are very beginner to the programming world they might not be knowing it and if you have experience that's great. Now if you don't know how binary format works I would recommend you to go and watch some videos on binary format or we'll try to do some in this video. Let's say I want to convert this. We can use a function called as bin. So using bin function, we can convert a number from decimal system to binary system. Now when you say decimal system, it means the base of the system is 10. And in binary system, the base is 2, which means in decimal system, we can go from 0 to 9. So we have 10 digits. In binary system, we can go from 0 to 1, 2 digits. And that's why we say it's bits binary digits right so we have binary format there in fact we have octal system where we have eight digits so it starts with zero and it ends at seven on the other hand we have hexadecimal system where we have 16 as a base now it starts with zero it goes till nine and after nine it goes from a to f so in total we have 16 so now let's come back from decimal to binary we'll click on enter you can see this is your binary format it's so simple you just need to use our bin function and in bracket you have to pass 25 But hold on. How do you convert this number? And even if you get this output, how do you know this is the correct output? So let me just take some time to explain you how to convert a number from a decimal system to a binary system. So for that, let me open my paint. So let's try to understand how it works. So let's say we have a number 25. So we'll go for a number 25 here. And if I want to find a decimal for a binary format of this 25, what do we do is you have to divide this number by two. So whatever system you have, so example, if you want to convert from decimal to octal, decimal to hexadecimal system, you have to divide this number by that system. So example here, I want to find binary format. So I will divide this by two. Now everyone knows right so it is 2 into 12 you will get 24 so the remainder is 1 so when you say 2 into 12 we got 24 and the remainder is 1 you have to continue this so you have to say 2 you will get 6 so 2 into 6 you will get 12 so the remainder is 0. Then we have 2 into 3 the remainder is 0 again and then we got 2 into 1 which is remainder is 1 in this case because 2 into 1 is 2 and then 3 minus 2 is 1. Now this is your numbers you know so this is your binary format so these are you convert from decimal to binary format but how do we read this? So we have to read this in a reverse order. So you have to start from this end and you have to go to top. So which is in this case, this is 1. Then we have this is 1, 0, 0, 1. So this is a binary format of 25. So this is how you convert a number from decimal system to binary format. It's so simple. Let's go back to the IDLE here. And here what I will do is you can see the number here. We got 1. 1, 0, 0, 1, the same number. But what is before that? You can see we got 0 and B. That's how you represent a binary format. So if you want to say there's a number which is 1, 1, 0, 0, 1, and you might be thinking if you got this number, this may be decimal, right? So to specify that, hey, this is not a decimal number. We will use 0, B. Okay, that's very important. In fact, you can convert a number from binary system to decimal system. It's very easy. Just say 0B. I will use a number which is 0101 in this case, which is actually a number 5, right? So I will say enter. You can say we got 5. So before the number or before the binary format, if you put 0B, that means the number is binary format and it will give you the output in decimal format. We can do the same thing for octal, right? So, example, let's go the same number. I will say, oh, which function we have to use? You can guess, right? So, for binary, we have bin. For octal, it should be oct, right? And then we'll give a number which is 25. I will say enter. Now, this is octal format. So, the octal conversion of 25 is 31. But what is before that? It's 0 O. So O is octal, right? So in the above case, we have B. That's for binary. The same thing can be done with hexadecimal. So I will say hex and I want to find 25. So I will say enter. You can see this 0 X 1 9. And that's really important, which is X here. So X is for hexadecimal number, right? In fact, I will just try to find the hex of 10. So, x of 10 is a, right? So, if I say enter, you can see we got 0, x, a. You can do the same thing in reverse order. You can say 0, x. Maybe I want to find maybe f. So, in this case, f is 15. So, if I say enter, you got 15. In fact, in the subsequent videos which we are going to see, we will be working with different operators like bitwise operator. and this video is very important for the bitwise operators make sure you know how to convert a decimal number into binary format and for that let me give you some assignments so what you can do is try to convert not on the python command prompt okay if you do it by yourself so try to find the binary format of let's say 31 then try to so this is your homework okay so try to find out binary format of 21 then 52 then 65 so these are some numbers you can try it out and then you can also do reverse okay so let's say if i give you binary format i have a number which is 0b and let's say this is the number i don't know what's the decimal format of this you have to find this as well so in this case it is 410 but how can we know that so in that case you can do this in reverse order as well So the number was 110011010. So how do you convert this number into decimal system? So first let's try with this number and then you can try with this number yourself. Now when you want to convert a number from binary to decimal, it's very simple. Start from the last number. So start from the last number and use 2 raised to 0. Second last number, 2 raised to 1. So we have to go for 2 raised to 2. Then 2 raise.

  2. Speaker 2

    To 3 is 8, 2 raise to 4 is 16 and then once you got this number you just need to add them. So you can see 16 plus 8 which is 24, 24 plus 1 we got 25.

  3. Speaker 1

    So first you have to use this format and then you have to add all the values you will get the decimal format. The same thing can be done with this. You try it out on your machine and let me know if you have done that. So that's it, everyone. That's how you can work with different number system in Python. So I hope you enjoyed this video. Let me know in the comment section. So that's it, everyone. Bye-bye.