Operators in Python

"Operators are one of the main concepts in programming"

Now, we have variables and different types of data types and we need to perform some operations with our data or values like calculations or comparisons, etc. So, here comes the operators which help us to perform these operations. In Python, we have different types of operators and these are:

(i) Logical Operators: Logical operators are the operators which compare two expressions and return the value in True or False. Following are the logical operators:

1) and: It compares two expressions and returns True if both the operators return True. For example -: let's take two expressions A and B then

table-chart (1).png

2) or: It compares two expressions and returns True if one of the expressions returns True. For example -: let's take two expressions A and B then

table-chart (3).png

3) not: It returns True if the expression returns False and returns False if the expression returns True.

(ii) Equality Operators: These operators strictly check the equality
of two values. The following operators belong to this equality operator:

1) ==: Equivalent Operator, returns True if both the value are equal. For example

            2 == 2
            returns True

2) !=: Not Equivalent, returns True if both values are not equal.

            2 != 3
            returns True

3) is: Same Identity, it is for reference equality. It checks if two variables point toward the same object.

            a = 1
            b = 1
            a is b
            returns True

4) is not:Different Identity, it checks if both variables don't point toward the same object.

            a = 1
            b = "1"
            a is not b
            returns True

(iii) Comparison Operators: Comparison operators are the operators which are used to compare two values. Following are the comparison operators available in Python:

1) <: Less than, it checks if the left-hand side value is less than that of the right-hand side value, if so it returns True.

             2 < 3
             returns True
             4 < 2
             returns False 

2) >: Greater than, it checks if the left-hand side value is greater than that of the right-hand side value, if so it returns True.

             2 > 3
             returns False
             4 > 2
             returns True

3) <=: Less than equal to, it checks if the left-hand side value is less than or equal to that of the right-hand side value, if so it returns True.

             3 <= 3
             return True
             2 <= 4
             return True
             3 <= 2
             return False

4) >=: Greater than equal to, it checks if the left-hand side value is greater than or equal to that of the right-hand side value

             3 >= 3
             return True
             2 >= 4
             return False
             3 >= 2
             return True

(iv) Arithmetic Operators: Arithmetic operators are used to perform mathematical operations like calculations. Following are the arithmetic operators present in Python:

1) +: Use for addition

2) -: Use for subtraction

3) * : Use for multiplication

4) /:Use for division

5) // : Integer division, it returns the integer part of the quotient For example:

            13 // 4
            return 3
            11 // 5 
            return 2

6) % : Modulo operator: it returns the remainder of the division For example:

            11 % 4 
            return 3
            15 % 6
            return 3

There are many other operators in Python like the Bitwise operator and Sequence operator.

Summary

So in this article, we have talked about operators and the different types of operators present in Python programming language. Hope you have enjoyed it.

Thanks for reading!