Types of numbers in Python
Python has three built in numeric data types -
- Integer (int)
- Floating-point numbers (float)
- Complex (complex)
In the above code, the int data type converts the string into integer. There is no limit on how long an integer type can be in Python.
2. Floating-point numbers - float - A floating-point Number is a number with a decimal point. For example -
In the above code, the float data type converts the string into a floating-point number.
In the above code, the type of the number is already defined as a floating point number. The e notation is the short form for Exponential notation and is used to express numbers too big to fit. In this case, 1e8 means 1*10^8. If we take 3e+12,
In the code, +12 means that 12 is a positive power. We can also use e with a negative power. For example -
3. Complex numbers - complex - A Complex Number is a number with two distinct components:A real part and an imaginary part. Python is one of the few coding languages that provides built-in support for complex numbers. An example of complex number is -
Python puts the complex number in brackets to eliminate any confusion of it being a arithmetic expression. To find the real and imaginary element from a complex number, we can do the following -
We can also get the complex conjugate of a number. For example -
That is all for today's post on Python numbers. Hope it was informative and easy to understand. Please contact me for any suggestions or fill the contact form.
Visit my Repl on replit.com here. You can find some simple sample programs here.
Regards,
Aarav Iyer
References:
(1) https://realpython.com/python-numbers/#:~:text=Python%20has%20three%20built%2Din,numbers%20in%20a%20later%20section.
(2) https://www.w3schools.com/python/python_numbers.asp
(3) (image)https://www.faceprep.in/python/numbers-in-python/