In this tutorial, we are going to learn about Arithmetic Operators and their types in Python with examples.
Definition: Arithmetic Operators are used to perform mathematical operations like Addition, Substraction, Multiplication and Division.
There are mainly two types of arithmetic operators in Python:
- Unary Operators
- Binary Operators
Unary arithmetic operators perform mathematical operations only on one operand. The ‛+’ and ‛-’ are two unary operators. The unary operator minus (-) produces the negation of its numeric operand. The unary operator plus (+) returns the numeric operand without change.
(+X returns the same value, i.e. X)
|Returns the same value that is provided as input|
(-X returns the negation of X)
|Negates the original value so that the positive value becomes negative and vice versa|
Examples of Unary Operators
>>> x=-5 #Negates the value of X >>> x -5 >>> x=+6 #Returns the same value >>> x 6
Binary Operators are the operators that require two operands to perform any mathematical operation. They are written in infinix form, i.e. the operator is written in between the two operands.
Types of Binary Operators
There are mainly 7 types of Binary Operators available in Python:
- Addition Operator
- Subtraction Operator
- Multiplication Operator
- Division Operator
- Floor Division Operator
- Modulo Operator
- Exponent Operator
Addition (+) Operator
The ‛+’ operator in Python can be used with both the binary and unary form. If the addition operator is applied in between two operands, it returns the result as the arithmetic sum of operands.
Examples of Addition (+) operator
>>> 4+7 #Addition 11 >>> 5+5 #Addition 10
The following table explains the syntax and semantics of the addition operator in Python, using its three numeric types, viz. Int, Float and complex.
|(int, int)-> int||2+4 returns 6|
|(float, float)-> float||1.0+4.0 returns 5.0|
|(int, float)-> float||1+2.0 returns 3.0|
|(float, int)-> float||2.0+1 returns 3.0|
|(complex, complex)-> complex||3j+2j returns 5j|
Subtraction (-) Operator
The ‘-‘ operator in Python can be used with binary and unary form. If the subtraction operator is applied in between two operands, that will calculate the difference between both of the operands and return it as output.
>>> 7-4 #Subtraction 3 >>> 5-2 #Subtraction 3
Syntax and Semantics of Subtraction operator in Python:
|(int, int)-> int||4-2 returns 2|
|(float, float)-> float||3.5-1.5 returns 2.0|
|(int, float)-> float||3-1.5 returns 1.5|
|(float, int)-> float||3.5-1 returns 2.5|
|(complex, complex)-> complex||7j-5j returns 2j|
Multiplication (*) Operator
The ‘*’ operator in Python can only be used with binary form. The multiplication operator can be used between two operands that will result as the product of both the operands and return as output from the Python program.
>>> 7*4 #Multiplication 28 >>> 5*2 #Multiplication 10
Syntax and Semantics of Multiplication Operator:
|(int, int)-> int||4*3 returns 12|
|(float, float)-> float||2.5*2.0 returns 5.0|
|(int, float)->float||2*2.5 returns 5.0|
|(float, int)-> float||2.5*2 returns 5.0|
|(complex, complex)-> complex||2j*2j returns -4+0j|
Division (/) Operator
Division operator (/) in Python can be only operated or used with binary forms. It is used between two operands to calculate the quotient of operands and it return it as an output through the Python program.
>>> 4/2 #Division 2.0 >>> 10/3 3.3333333333
Syntax and Semantics of Division Operator in Python:
|(int, int)-> float||5/2 returns 2.5|
|(float, float)-> float||0.4/2.0 returns 0.2|
|(int, float)-> float||2/0.5 returns 1.0|
|(float, int)-> float||4.2/2 returns 2.1|
|(complex, complex)-> complex||6j/2j returns 3+0j|
Floor Division (//) Operator
Floor Division (//) Operator in Python can be only used with binary forms. This Operator is used between two operands to get the quotient as the result of Python program and show it as output. Now, the difference is that the Floor Division operator ignoes the numbers after decimal point in the quotient and put a zero after decimal.
>>> 4/2 #Floor Division 2 >>> 13/4 #Floor Division 3
Syntax and Semantics of Floor Division Operator:
|(int, int)-> int||25//5 returns 5|
|(float, float)-> float||10.5//5.0 returns 2.0|
|(int, float)-> float||11//2.5 returns 4.0|
|(float, int)-> float||4.0//3 returns 1.0|
Modulo (%) Operator
Modulo (%) Operator is used between two operands to perform division and obtain the reminder as output from the program.
>>> 10%4 #10 is divided by 4 and we got 2 as remainder 2 >>> 13%5 3
Syntax and Semantic of Modulo Operator:
|(int, int)-> int||25%4 returns 1|
|(float, float)-> float||2.5%1.2 returns 0.10|
|(int, float)-> float||13%2.0 returns 1.0|
|(float, int)-> float||1.5%2 returns 1.5|
Exponent ** Operator
Exponent ** Operator is used to calculate the power or exponent of a number. To compute Xy(X raised to y power), the expression is written as X**y in Python. The exponent operator is also called Power operator.
>>> 4**2 #calculate square of 4 16 >>> 2**3 #calculate cube of a number 2 8
Syntax and Semantics of Exponent operator:
|(int, int)-> int||2**4 returns 16|
|(float, float)-> float||2.0**3.0 returns 8.0|
|(int, float)-> float||5**2.0 returns 25.0|
|(float, int)-> float||4.0**3 returns 64.0|