Numbers In Python
Numbers in Python refer to the numeric data types in Python Programming. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.
Python has three built-in number data types:
- Integers (int)
- Floating-point numbers (float)
- Complex numbers (complex)
- Integer:
In Python, integers are zero, positive, or negative whole numbers without a fractional part and having unlimited precision, e.g. 0, 100, -10.
- num1 = 5
print(type(num1))
Result: <class ‘int’>
- num2 = -35 * 5
print(type(num2))
Result: <class ‘int’>
- num3 = 6859205148421518
print(type(num3))
Result: <class ‘int’>
Note: When you create an integer like this, these are called integer literals because the integers are literally typed into the code. Integers can be binary, octal, and hexadecimal values.
- Float:
In Python, floating point numbers (float) are positive and negative real numbers with a fractional part denoted by the decimal symbol(.). A floating point number can be positive, negative, or zero. Just like decimal numbers, floating point numbers also contain one or more digits after a decimal point. e.g., 1234.56, 3.142, -1.55, 1e6.
- x = 5.0
print(type(x))
Result: <class ‘float’>
- y = 85.962
print(type(y))
Result: <class ‘float’>
- Z = -2.4 * 5
print(type(z))
Result: <class ‘float’>
Note: For large numbers, you can use E-notation. The example below uses E-notation to create a float literal.
>>> 1e6
1000000.0
Python also uses E-notation to display large floating point numbers:
>>> 200000000000000000.0
2e+17
- Complex:
A complex number is one that has both real and imaginary parts. For example, 2 + 3j is a complex number in which 2 is the real component and 3 multiplied by j is the imaginary component.
- a = 2 + 5j
print(type(a))
Result: <class ‘complex’>
- b = 3 – 4j
print(type(b))
Result: <class ‘complex’>
- x = 1 + 2j
y = 3 – 4j
z = x + y
print(z)
print(type(z))
Result: (4-2j)
<class ‘complex’>
Conclusion:
We looked at Python’s three main numeric data types in this blog: integers, floats, and complex numbers. These types, with their distinct characteristics, provide a flexible foundation for numerical operations in Python.