Python arithmetic expressions are combinations of numbers and at least one arithmetic operation.
To perform an arithmetic expression, the following must be understood.
- Arithmetic operators
- Number type conversion
- Combining two numbers of different types
- Operators Precedence
Python version 3 supports three types of numbers: integers, floats and complex numbers.
According to Wikipedia, integers are numbers without a fractional component.
a = 5 print(type(a)) >>> <class 'int'>
Floats are the set of numbers containing numbers that have a decimal place.
a = 7.23 print(type(a)) >>> <class 'float'>
Complex numbers are the set of numbers containing a real and imaginary parts.
C = x +yj, where
x is the real part and
y is the imaginary part.
j is the square root of -1.
a = 5 + 2j print(type(a)) >>> <class 'complex'>
Python stores the real and imaginary parts of a complex number as floats.
a = 5 + 2j print(a.real) >>> 5.0 print(a.imag) >>> 2.0
Python arithmetic operators are:-
- Addition (
- Subtraction (
- Multiplication (
- Division (
- Floor division (
- Exponent (
- Modulus (
The above operators are binary in nature, which means they operate on two numbers (also known as operands).
The addition operator (
+) produces the sum of operands.
a = 15 b = 5 print(a + b) >>> 20
The subtraction operator (
-) subtracts the two operands, producing their difference.
a = 15 b = 5 print(a - b) >>> 10
The multiplication operator (
*) produces the product of the operands.
a = 15 b = 5 print(a * b) >>> 75
The division operator (
/) produces the quotient of its operands, the result will be a float type.
a = 15 b = 5 print(a / b) >>> 3.0
The floor operator(
//) is used to make a whole number adjusted to the left hand side in the number line. Basically, it produces the quotient of a division operation. The result is of type
a = 20 b = 3 print(a // b) >>> 6 print(type(a // b)) >>> <class 'int'>
The modulus operator (
%) returns the remainder left when one operand is divided by a second operand. The result is of type
a = 15 b = 4 print(a % b) >>> 3 print(type(a % b)) >>> <class 'int'>
The exponent operator (
**) returns the result of raising first operand to the power of the second operand.
a = 2 b = 3 print(a ** b) >>> 8
Number type conversion
All integers can be converted to floats. Just add a decimal to the end.
a = 25 a = 25.0 print(type(a)) >>> <class 'float'>
Another way to do this is to pass an integer to the float constructor.
a = 25 b = float(a) print(type(b)) >>> <class 'float'>
All intergers/floats can be converted to complex numbers but not vice versa.
To convert an integer or a float to a complex number just add
0j to the number.
a = 7.3 b = a + 0j print(type(b)) >>> <class 'complex'>
Another way to do this is to pass an integer or a float to the complex constructor.
a = 12 b = complex(a) print(type(b)) >>> <class 'complex'> print(b) >>> (12+0j)
It’s not possible to convert complex number to an integer of a float number.
Let’s try and convert a complex number to float.
a = 12 + 3j print(float(a)) Traceback (most recent call last): File <input>, line 2, in <module> float(a) TypeError: can't convert complex to float
Now let’s repeat the above and convert a complex number to integer.
a = 12 + 3j print(int(a)) Traceback (most recent call last): File <input>, line 2, in <module> float(a) TypeError: can't convert complex to int
Only floats numbers with 0 as decimal can be converted to integer.
a = 3.0 b = int(a) print(b) >>> 3 print(type(b)) >>> <class 'int'> # Notice this !! print(a == b) >>> True
But what happens when you convert an actual fraction to integer? Python just chops off the decimal.
a = 3.7 b = int(a) print(b) >>> 3 print(type(b)) >>> <class 'int'> print(a == b) >>> False
Combining two numbers of different types
To use numbers responsibly you should learn the rules of arithmetic.
Arithmetic may seem trivial, but complications arise when you combine numbers of different types. To use arithmetic in python you first need to understand the idea of narrower and wider types.
As already mentioned above Python 3 supports 3 types of numbers : integers, floats and complex numbers.
Python 3 also fully supports mixed arithmetic.
The question that arises is: What is the type of the arithmetic expression, when a binary arithmetic operator has operands of different numeric types.
**The rule of thumb **is : the number with the
narrower type is widened to that of the other, where integer is narrower than float and float is narrower than complex.
For example, expressions in which an integer and a float number is involved, the integer will be promoted to float. In summary, Python will widen any numbers to make sure all values are of the same type.
Example 1 - Arithmetics with integer and a float
a = 3 b = 4.5 print(a + b) >>> 7.5 print(type(a + b)) >>> <class 'float'>
Example 2 - Arithmetics with integer and a integer
a = 7 b = 3 print(a * b) >>> 21 print(type(a * b)) >>> <class 'int'>
b are integers, no widening is required.
Dividing an integer with an integer returns a float. This is a change introduced in Python 3. To learn more relevant differences between Python 3 and 2, check out our article here.
a = 12 b = 3 print(a/b) >>> 4.0
Example 3 - Arithmetics with integer and complex number.
a = 14 + 4j b = 2 print(a/b) >>> (7+2j)
Integers are narrower than complex numbers. So
b is widened to a complex number before division. it becomes
2 + 0j.
Example 4 - Arithmetic with multiple types of numbers
When combining different types of numbers, Python will widen numbers to the same type.
a = 7 b = 3.5 d = 2.0 f = 2 + 3j print(a + b * d - a / f ) >>> (12.923076923076923+1.6153846153846154j)
Are you wondering how Python decides the precedence of operations? Let’s move on to the next section.
Precedence of arithmetic operators
Python has well-defined rules for specifying the order in which the arithmetic operators in an expression are evaluated when the expression has several operators.
The order of operation can be summarized as follows:
Any operations enclosed in parentheses are performed first.
Exponents are performed next.
Multiplication and Division is performed from left to the right.
In the end, any addition and subtraction operations are performed from left to the right.
Let’s see an illustration to understand this better.
I hope this article draws a clear picture about use of arithmetics in Python. If you have any questions or suggestions - do leave them in the comment box below.