Python Numbers can be classified into three types Integers, Floating-point, and Complex numbers, and there is no explicit type declaration needed in Python. The numbers which we type in will be automatically interpreted as a number.
Python Numbers
- Integer Numbers
- Floating-Point Numbers
- Complex Numbers
1. Integer Numbers
Like any other programming language, Python Integer Numbers are whole numbers that can be positive, negative, or zero (..-2, -1, 0, 1, 2,…). In Python Integer is also known as int
A simple declaration of an integer looks like this
>>> a = 10 >>> b = -15 >>> type(a) <class 'int'> >>> type(b) <class 'int'>
We can see the data type of a variable using the type() function, since we are storing an integer [Without decimal points] into the variable ‘a’ and ‘b’, the type of ‘a’ and ‘b’ is assumed by the Python interpreter as ‘int’
Note: In Python, any sequence of digits without a functional part can be assumed as Integers. For example, we can enter an integer literal like below
>>> 7 7
We can also simply print a plain zero(0)
>>> 0 0
But what will happen if we are using zero in front of an integer?
>>> 07 SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers
We will be getting “SyntaxError: leading zeros in decimal integer literals are not permitted”. Though in mathematics world ’07’ is the same as ‘7’, in Python it is not permitted for an integer literal.
In Python, we can also refer int type with different bases,
- Binary numbers (Base 2) have only 0 and 1 numbers.
- Octal numbers (Base 8) uses the numbers from 0 to 7.
- Decimal numbers (Base 10) will have numbers from 0 to 9. This the standard number system which we use.
- Hexadecimal numbers (Base 16) contains numbers from 0 to 9 and letters A through F.
To inform Python interpreter to use a different base other than base 10, we need to add a zero and a special character to the number. For Example, 0x11 tells the Python to treat the number as a hexadecimal number.
Below are the characters which we need to use
- ‘0b’or ‘0B’ for Binary Numbers (Base 2)
- ‘0o’ or ‘0O’ for Octal Numbers (Base 8)
- ‘0x’ or ‘0X’ for Hexadecimal Numbers (Base 16)
We can also convert the numbers from one base to another base using bin(), oct(), and hex() functions. Let’s put all together and try converting into different bases.
Let’s convert the number 10 to different bases, by calling the bin(), oct(), and hex() functions
>>> bin(10) '0b1010' >>> oct(10) '0o12' >>> hex(10) '0xa'
2. Floating-Point Numbers
Floating-Point Numbers can be easily identified by their decimal points or they have an exponential (e) to define the number, they can be positive, negative, or zero (..-2.0, -1.0, 0.0, 1.0, 2.0,…). In Python Floating-Point numbers are also known as float
For Example, 3.0 and -4.1 are examples of floating-point numbers. 5E3 (5 times 10 to the power of 3) is also an example of a floating-point number in Python.
>>> c = 10.0 >>> d = -15.0 >>> e = 0.0 >>> type(c) <class 'float'> >>> type(d) <class 'float'> >>> type(e) <class 'float'>
3. Complex Numbers
A Complex number is a number which consists of real and imaginary part paired together. Complex numbers are typically using in Engineering like hydrodynamics, alternating voltages, etc.. Python is one of the programming languages which supports complex numbers by default.
For Example, 5 + 2j is a complex number, where 5 is the real part and 4 is the imaginary part. The imaginary part always contains a j in it.
We can simply assign a complex number to a variable in Python directly, like below
>>> num = 5 + 2j
We can get the real of the complex number, by using the num.real [variable.real]. Likewise, if we want to get the imaginary part, we need to use num.imag[variable.image]
>>> num.real 5.0 >>> num.imag 2.0
Type Conversion
Whenever we are adding ‘int’ and ‘int’, the result will be an ‘int’, whereas if we are adding ‘int’ and ‘float’, then the result will be in ‘float’, because of Python automatically coverts ‘int’ into ‘float’ before addition.
>>> a = 10 >>> b = 20 >>> c = a + b >>> c 30 >>> type(c) <class 'int'> >>> d = 10 >>> e = 20.1 >>> f = d + e >>> f 30.1 >>> type(f) <class 'float'>
As we saw, Python automatically converts the data type (int to float) whenever needed, we can also convert the datatype manually by ourself.
We can explicitly convert int to float by using the int() function and convert float to int by using the float() function.
>>> int(10.1) 10 >>> int(-10.7) -10 >>> int (-10.2) -10 >>> float (10) 10.0
Note: The int() function truncates the decimal points and does not perform round off
Basic Arithmetic operations
Operator | Operation |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Float Division |
// | Integer Division |
% | Modulus |
** | Exponentiation |
Let’s perform some basic arithmetic operations on python numbers.
>>> 3.1 + 2.9 6.0 >>> 4 * 2 8 >>> 3 / 2 1.5 >>> 3 // 2 1
Did you notice it?
3 // 2 gives us the result of 1 and NOT 1.5
The ‘/’ operator performs the floating-point division even though both the numerator and denominator are int, but the ‘//’ operator is a tricky little operator when the result is positive it truncates the decimal part.
But, when the result is negative, it performs the round off functionality
>>> -7 // 2 -4
When we divided -7 by 2 [-7 // 2], instead of giving -3.5 as a result, it gave -4 as it has performed round off on the result.
What if we want the remainder of a division? then we can use the Modulo operator %
>>> 4 % 2 0 >>> 5 % 2 1 >>> -10 % 3 2
The last operator which we will be looking is ** operator [exponentiation or power operator]
>>> 4 ** 2 16 >>> 2 ** 3 8 >>> -3 ** 2 -9
This operator performs the “raised to the power of” operation, so 4 ** 2 will perform 42
Happy Learning!!
Leave a Reply