I have promised you that I will illustrate an interesting example in my next post. Here we go with that interesting variable behavior.

If you type an integer with a leading zero, you might get a confusing error.

Other numbers seem to work, but the results are bizarre.

Can you figure out what is going on? It’s really confusing. Isn’t it?

When we assign an integer with a leading zero, Python assumes that it’s an octal number, and stores its corresponding decimal value. Take a look at the figure below.

We know decimal values of (010)

_{8}= 8, (0100)_{8 }= 64 etc. Here (02132)_{8}has the decimal value 1114. Then what is the error in 02492? It’s not an octal number at all. We know octal number have only digits 0 to 7. 02492 have 9 in it. So an error occurred.Just for my curiosity, I tried an integer with two leading zeros and I got the same effect. I tried another one with floating values with leading zeros in their integer part. I found it is same as the normal floating values and no octal-decimal conversion take place.

Have you enjoyed? Keep browsing TaLenCia, I will be back with more interesting facts about Python.

Thanks

AJAY

Man, you got it..! good work.

ReplyDeleteThanks jinesh

ReplyDelete