Posted in: Python

Arithmetic Operators in Python – Types (Unary and Binary) and Examples

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:

  1. Unary Operators
  2. Binary Operators
Arithmetic Operator Types

Unary 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.

Unary OperatorExampleDescription
++X
(+X returns the same value, i.e. X)
Returns the same value that is provided as input
-X
(-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

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:

  1. Addition Operator
  2. Subtraction Operator
  3. Multiplication Operator
  4. Division Operator
  5. Floor Division Operator
  6. Modulo Operator
  7. 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.

SyntaxExample
(int, int)-> int2+4 returns 6
(float, float)-> float1.0+4.0 returns 5.0
(int, float)-> float1+2.0 returns 3.0
(float, int)-> float2.0+1 returns 3.0
(complex, complex)-> complex3j+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.

Example:

>>> 7-4 #Subtraction
3 
>>> 5-2 #Subtraction
3 

Syntax and Semantics of Subtraction operator in Python:

SyntaxExample
(int, int)-> int4-2 returns 2
(float, float)-> float3.5-1.5 returns 2.0
(int, float)-> float3-1.5 returns 1.5
(float, int)-> float3.5-1 returns 2.5
(complex, complex)-> complex7j-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.

Example:

>>> 7*4 #Multiplication
28 
>>> 5*2 #Multiplication
10

Syntax and Semantics of Multiplication Operator:

SyntaxExample
(int, int)-> int4*3 returns 12
(float, float)-> float2.5*2.0 returns 5.0
(int, float)->float2*2.5 returns 5.0
(float, int)-> float2.5*2 returns 5.0
(complex, complex)-> complex2j*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.

Example:

>>> 4/2 #Division
2.0
>>> 10/3
3.3333333333

Syntax and Semantics of Division Operator in Python:

SyntaxExample
(int, int)-> float5/2 returns 2.5
(float, float)-> float0.4/2.0 returns 0.2
(int, float)-> float2/0.5 returns 1.0
(float, int)-> float4.2/2 returns 2.1
(complex, complex)-> complex6j/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.

Example:

>>> 4/2 #Floor Division
2
>>> 13/4 #Floor Division
3

Syntax and Semantics of Floor Division Operator:

SyntaxExample
(int, int)-> int25//5 returns 5
(float, float)-> float10.5//5.0 returns 2.0
(int, float)-> float11//2.5 returns 4.0
(float, int)-> float4.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.

Example:

>>> 10%4 #10 is divided by 4 and we got 2 as remainder
2
>>> 13%5 
3

Syntax and Semantic of Modulo Operator:

SyntaxExample
(int, int)-> int25%4 returns 1
(float, float)-> float2.5%1.2 returns 0.10
(int, float)-> float13%2.0 returns 1.0
(float, int)-> float1.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.

Example:

>>> 4**2 #calculate square of 4
16
>>> 2**3 #calculate cube of a number 2
8

Syntax and Semantics of Exponent operator:

SyntaxExample
(int, int)-> int2**4 returns 16
(float, float)-> float2.0**3.0 returns 8.0
(int, float)-> float5**2.0 returns 25.0
(float, int)-> float4.0**3 returns 64.0

Leave a Reply

Your email address will not be published.