Like other programming languages, Python has operators which help us to perform operations.
As we know the operators perform operations on operands.
A + B Here “+” is the operator and A and B are the Operands.
The basic types of operators in Python:
- Arithmetic Operators
- Relational Operators (Comparision)
- Logical operators
- Assignment operators
- Bitwise operators
- Identity operators
- Membership operators
Let us have look at one by one type of operators
- Arithmetic Operators:
As we know those operators are useful to perform arithmetic operations such as addition, subtraction, multiplication, and division they are called Arithmetic operators.
- + (Plus) – Addition
- – (Minus) – Subtraction
- * (Asterisk) – Multiplication
- / (slash) – Division – it gives the quotient
- % (Percentage) – Modulus remainder – It gives the remainder as a result.
- ** (Double asterisk) – Exponential – It is nothing but exponential of two operands.
- // (Double Slash) – Floor division – It gives the result as quotient as a whole number. This means that it removes the number after the decimal in case a result is a decimal number.
See the below example for a better understanding.
>>> a = 10
>>> b = 2
>>> print("addition of a+b", a+b)
addition of a+b 12
>>> print("subtraction of a-b", a-b)
subtraction of a-b 8
>>> print("Multiplication of a*b", a*b)
Multiplication of a*b 20
>>> print("Division of a/b", a/b)
Division of a/b 5.0
>>> print("Modulus Remainder of a%b", a%b)
Modulus Remainder of a%b 0
>>> print("Exponential of a**b", a**b)
Exponential of a**b 100
>>> print("Floor Division of a//b", a//b)
Floor Division of a//b 5
>>> a = 19
>>> b = 3
>>> print("Floor Division of a//b", a//b)
Floor Division of a//b 6
>>> print("Division of a/b", a/b)
Division of a/b 6.333333333333333
>>> print("Modulus Remainder of a%b", a%b)
Modulus Remainder of a%b 1
>>>
In the above example, we are able to understand the floor division, modulus division, and normal division.
Relationship Operators (Comparision Operators):
These operators are useful to compare both side values either which are bigger, smaller, or equal, etc to be determined using these operators.
- == (Double Equal) – This operator is useful to compare the equality between two values. for example, a == b if a and b are both are having same values it is true otherwise false.
a = 5 and b = 6 then a == b is false. - != (Exclamation equal) – This operator is useful to compare inequality between two values. For example, a != b if a and b are both are not having the same values.
a = 5 and b = 6 then a != b is True. - < (Less than) – This operator is useful when comparing two values, when the left-hand value is smaller than the right-hand side value then it is true.
a = 5 and b = 6 then a < b is true. - > (Greater than) – This operator is useful when comparing two values, when the left-hand value is bigger than the right-hand side value then it is true.
a = 6 and b = 5 then a > b is True. - >= (Greater than or Equal) – This operator is useful in performing to find the relation between two values, If the value of the left-hand side is greater than or equal then it is true.
a = 6 and b = 6 then a >= b is True - <= (Less than or equal) – If the left-hand side value of the operator is less than or equal to the right-hand side value then it is true otherwise it is false.
a = 5 and b = 6 then a <= b is True
>>> a = 6
>>> b = 8
>>> print(a==b)
False
>>> print(a!=b)
True
>>> print(a<b)
True
>>> print(a>b)
False
>>> print(a>=b)
False
>>> print(a<=b)
True
>>>