Numbers in Python
When I was writing this article, I thought to myself🤔, would anyone be interested in it at all. Its just numbers🔢. I mean, how hard can it be. But then I realized, these few enhancements that we talk about exist in the real world for a reason. So, no matter how small a audience might benefit from it, it is still a benefit🫱🏻🫲🏼. And I am gonna go ahead with it. With that guilt out of the way, let's get started
This is gonna be one of those short and sweet articles
The standard way
Of course, we have the standard way of writing numbers
a = 42 # Positive integer
b = -7 # Negative integer
c = 1.57 # Positive float
d = -21.6 # Negative float
e = 3+5j # Complex number
Note that the imaginary number
iis calledjin Python
This can easily be proved
a = 3+5j
b = 3-5j
c = a*b
print(c)
# Output
(34+0j) # Basically meaning 34
Fun fact😜: While
jis used to show imaginary component, it can still be used as a variable name. So,j=3+4jor for that matterj=5jis valid in python (Although mathematically it looks so wrong)
Big numbers
If we have to represent big numbers... well... we just type⌨️ it out...
But it soon gets difficult to check if there was a typo in it. For example a = 1000000000. What is a? You are forced to count the number of zeros making the appearance of a baboon🐵. This can be solved in python in 2 ways
Scientific notation
Generally big numbers or tiny numbers occur in science🔭. What better way to represent them than scientific notation. The x10^{} is replaced with e
a = 1e4 # 10000
b = -3e7 # -30000000
c = 5.284e-8 # 0.00000005284
Underscores
For those of us who are not good at science (basically regular normal people), adding an underscore _ makes a clear distinction to count the numbers. You know how Python loves snake🐍 case right. Hence the underscore. So don't undermine the underscore
a = 10_000
b = -30_000
Python completely ignores the underscore. Which means we don't have to follow any numeric system to put them like a = 1_00_000 (read 1 Lakh in Indian) and a = 100_000 (read hundred thousand in English) are the same
This also means that its easier to store credit card numbers, bank receipt numbers, Social Security numbers etc..
a = 7692_1247_3714
b = 96_2144_4144
But wait a minute, what about outputs?
a = int(input("Enter a big number: " ))
print(a)
Run the code and type 10_000. The output is still 10000. So, the baboon🐒 doesn't leave us after all. Not quite!
This can be done with and without f-strings. I am gonna show it with f-strings, because its the modern way
a = int(input("Enter a big number: " ))
print(f"{a}") # Reference f-string syntax
print(f"{a:_}") # f-string syntax with underscore thingies
print(f"{a:,}") # f-string syntax with comma thingies
Note that even if during the input we give underscores for every 4 digits (like
1234_5678_1234), python strips them off and stores them as proper integers. When its time to print it, it puts the underscores for every 3 digits by default (like123_456_781_234)
Conclusion
So, there you have it. We can represent numbers🔢 in different ways in python