Types of Operators in python
- Arithmetic operators
- Relational Operators
- Assignment Operators
- Logical Operators
- Bitwise Operator
- Membership Operators
- Identity Operators
Arithmetic operators
+ Addition | Returns the addition of variable a and b. |
- Subtraction | Returns the output of subtraction of variables a and b. |
* Multiplication | Output is the multiplication of variables a and b. |
/ Division | Output is the division of variable a by b. |
% Modulus | Modulus is the reminder of division of variable a by b. |
** Exponent | Raises a to the power b. |
// Floor Division | Floor division removes the digits after decimal points in quotient after a is divided by b. |
Relational operators
Used to evaluate relationship between 2 variables, below is an example that covers all our relationship operators
== | Returns true if a and b are equal, else will return false. |
!= | Returns true if a is not equal to b, else it will return true. |
> | Returns true if a is greater than b, else it will return false. |
< | Returns true if a is less than b, else will return false. |
>= | Returns true if a is greater than or equal to b, else will return false. |
<= | Returns true if a is less than or equal to b, else will return false. |
Assignment operators
Used to assign or modify values to variables
As we all know, all different types of arithmetic operators we have used above, we need no explanation for that expect for format.
a = 5 | equal to operator assigns right side value to variable on the left side. |
a+=b | It is simply a short form of saying a = a + b, which is similar to the rest in the above picture. |
Bitwise Operator
Standard truth table for bitwise operator.
AND | OR | NOT | EXOR |
0 & 0 = 0 | 0 | 0 = 0 | ~0 = 1 | 0 ^ 0 = 0 |
0 & 1 = 0 | 0 | 1 = 1 | ~0 = 1 | 0 ^ 1 = 1 |
1 & 0 = 0 | 1 | 0 = 1 | ~1 = 0 | 1 ^ 0 = 1 |
1 & 1 = 1 | 1 | 1 = 1 | ~1 = 0 | 1 ^ 1 = 0 |
Below is an example for all Bitwise operations possible in python.
- When a decimal number is passed as an argument to bin() function it returns the binary of that number.
- >> is a right shift operator, which shifts the binary values on the left side of the operator to right by amount specified on the right side of the operator, in above example it is shifted to right by 2 decimal places.
- << is a left shift operator, which shifts the binary values on the left side of the operator to left by amount specified on the right side of the operator, in above example it is shifted to left by 2 decimal places.
Logical operator
a and b | if both are equal to 1 or true, it will return true else will return false. |
a or b | if either are equal to 1 or true, it will return true else will return false. |
not (b) | returns boolean opposite of current boolean value of b here it is true. |
Code below if(condition) statement will be executed if the condition attached to if statement is true. We will discuss more about conditional statements in next article.
Membership Operators
Membership operator is used to find out whether a variable/char exist in list or tuple or string. below is an example.
Identity Operators
These operators compare the identity of 2 variables and returns boolean value accordingly.
- id() returns the identity of the variable passed as argument to the function. which is what we have got in our above example.
- a is b returns true if variables on both sides share same identity, else returns false.
- a is not b returns true if variables on both sides does not share same identity, else returns false.
In the next chapter we will discuss about control structures in python.