Python Arithmetic Operators
Python arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and more on numbers. Arithmetic operators are binary operators in the sense they operate on two operands. Python fully supports mixed arithmetic. That is, the two operands can be of two different number types. In such a situation.
Types of Arithmetic Operators
Following is the table which lists down all the arithmetic operators available in Python:
Operator | Name | Example |
---|---|---|
+ | Addition | a + b = 30 |
– | Subtraction | a b = -10 |
* | Multiplication | a * b = 200 |
/ | Division | b / a = 2 |
% | Modulus | b % a = 0 |
** | Exponent | a**b =10**20 |
// | Floor Division | 9//2 = 4 |
Let us study these operators with examples.
Addition Operator
The addition operator represents by + symbol. It is a basic arithmetic operator. It adds the two numeric operands on the either side and returns the addition result.
Example to add two integer numbers
In the following example, the two integer variables are the operands for the “+” operator.
a=10 b=20print("Addition of two integers")print("a =",a,"b =",b,"addition =",a+b)
It will produce the following output −
Addition of two integers a = 10 b = 20 addition = 30
Example to add integer and float numbers
Addition of integer and float results in a float.
a=10 b=20.5print("Addition of integer and float")print("a =",a,"b =",b,"addition =",a+b)
It will produce the following output −
Addition of integer and float a = 10 b = 20.5 addition = 30.5
Example to add two complex numbers
The result of adding float to complex is a complex number.
a=10+5j b=20.5print("Addition of complex and float")print("a=",a,"b=",b,"addition=",a+b)
It will produce the following output −
Addition of complex and float a= (10+5j) b= 20.5 addition= (30.5+5j)
Subtraction Operator
The subtraction operator represents by – symbol. It subtracts the second operand from the first. The resultant number is negative if the second operand is larger.
Example to subtract two integer numbers
First example shows subtraction of two integers.
First example shows subtraction of two integers.
a=10 b=20print("Subtraction of two integers:")print("a =",a,"b =",b,"a-b =",a-b)print("a =",a,"b =",b,"b-a =",b-a)
Result −
Subtraction of two integers a = 10 b = 20 a-b = -10 a = 10 b = 20 b-a = 10
Example to subtract integer and float numbers
Subtraction of an integer and a float follows the same principle.
a=10 b=20.5print("subtraction of integer and float")print("a=",a,"b=",b,"a-b=",a-b)print("a=",a,"b=",b,"b-a=",b-a)
It will produce the following output −
subtraction of integer and float a= 10 b= 20.5 a-b= -10.5 a= 10 b= 20.5 b-a= 10.5
Example to subtract complex numbers
In the subtraction involving a complex and a float, real component is involved in the operation.
a=10+5j b=20.5print("subtraction of complex and float")print("a=",a,"b=",b,"a-b=",a-b)print("a=",a,"b=",b,"b-a=",b-a)
It will produce the following output −
subtraction of complex and float a= (10+5j) b= 20.5 a-b= (-10.5+5j) a= (10+5j) b= 20.5 b-a= (10.5-5j)
Multiplication Operator
The * (asterisk) symbol is defined as a multiplication operator in Python (as in many languages). It returns the product of the two operands on its either side. If any of the operands negative, the result is also negative. If both are negative, the result is positive. Changing the order of operands doesn’t change the result
Example to multiply two integers
a=10 b=20print("Multiplication of two integers")print("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of two integers a = 10 b = 20 a*b = 200
Example to multiply integer and float numbers
In multiplication, a float operand may have a standard decimal point notation, or a scientific notation.
a=10 b=20.5print("Multiplication of integer and float")print("a=",a,"b=",b,"a*b=",a*b) a=-5.55 b=6.75E-3print("Multiplication of float and float")print("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of integer and float a = 10 b = 20.5 a-b = -10.5 Multiplication of float and float a = -5.55 b = 0.00675 a*b = -0.037462499999999996
Example to multiply complex numbers
For the multiplication operation involving one complex operand, the other operand multiplies both the real part and imaginary part.
a=10+5j b=20.5print("Multiplication of complex and float")print("a =",a,"b =",b,"a*b =",a*b)
It will produce the following output −
Multiplication of complex and float a = (10+5j) b = 20.5 a*b = (205+102.5j)
Division Operator
The “/” symbol is usually called as forward slash. The result of division operator is numerator (left operand) divided by denominator (right operand). The resultant number is negative if any of the operands is negative. Since infinity cannot be stored in the memory, Python raises ZeroDivisionError if the denominator is 0.
The result of division operator in Python is always a float, even if both operands are integers.
Example to divide two numbers
a=10 b=20print("Division of two integers")print("a=",a,"b=",b,"a/b=",a/b)print("a=",a,"b=",b,"b/a=",b/a)
It will produce the following output −
Division of two integers a= 10 b= 20 a/b= 0.5 a= 10 b= 20 b/a= 2.0
Example to divide two float numbers
In Division, a float operand may have a standard decimal point notation, or a scientific notation.
a=10 b=-20.5print("Division of integer and float")print("a=",a,"b=",b,"a/b=",a/b) a=-2.50 b=1.25E2print("Division of float and float")print("a=",a,"b=",b,"a/b=",a/b)
It will produce the following output −
Division of integer and float a= 10 b= -20.5 a/b= -0.4878048780487805 Division of float and float a= -2.5 b= 125.0 a/b= -0.02
Example to divide complex numbers
When one of the operands is a complex number, division between the other operand and both parts of complex number (real and imaginary) object takes place.
a=7.5+7.5j b=2.5print("Division of complex and float")print("a =",a,"b =",b,"a/b =",a/b)print("a =",a,"b =",b,"b/a =",b/a)
It will produce the following output −
Division of complex and float a = (7.5+7.5j) b = 2.5 a/b = (3+3j) a = (7.5+7.5j) b = 2.5 b/a = (0.16666666666666666-0.16666666666666666j)
If the numerator is 0, the result of division is always 0 except when denominator is 0, in which case, Python raises ZeroDivisionError wirh Division by Zero error message.
a=0 b=2.5print("a=",a,"b=",b,"a/b=",a/b)print("a=",a,"b=",b,"b/a=",b/a)
It will produce the following output −
a= 0 b= 2.5 a/b= 0.0 Traceback (most recent call last): File "C:\Users\mlath\examples\example.py", line 20, in <module>ZeroDivisionError: float division by zeroprint ("a=",a,"b=",b,"b/a=",b/a) ~^~
Modulus Operator
Python defines the “%” symbol, which is known aa Percent symbol, as Modulus (or modulo) operator. It returns the remainder after the denominator divides the numerator. It can also be called Remainder operator. The result of the modulus operator is the number that remains after the integer quotient. To give an example, when 10 is divided by 3, the quotient is 3 and remainder is 1. Hence, 10%3 (normally pronounced as 10 mod 3) results in 1.
Example for modulus operation on integers
If both the operands are integer, the modulus value is an integer. If numerator is completely divisible, remainder is 0. If numerator is smaller than denominator, modulus is equal to the numerator. If denominator is 0, Python raises ZeroDivisionError.
Leave a Reply