Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator:
Python language supports the following types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Python Arithmetic Operators:
Python has many Arithmetic Operators. These are following:
| + | addition operator |
| - | subtraction operator |
| * | multiplication operator |
| / | division operator |
| % | modulus operator |
| ** | exponent operator |
| // | floor divisionoperator |
2. Python Comparison Operators:
These operators compare the values on either sides of them and decide the relation among them.They are also called Relational operators.
| == | equal to operator |
| != | not equal to operator |
| > | greater than operator |
| < | less than operator |
| >= | greater than equal to operator |
| <= | less than equal to operator |
3. Python Assignment Operators:
Python has many Arithmetic Operators.These are following:
| = | value assign operator |
| += | It adds right operand to the left operand and assign the result to left operand |
| -= | It subtracts right operand from the left operand and assign the result to left operand |
| *= | It multiplies right operand with the left operand and assign the result to left operand |
| /= | It divides left operand with the right operand and assign the result to left operand |
| %= | It takes modulus using two operands and assign the result to left operand |
4. Python Bitwise Operator:
The following Bitwise operators are supported by Python language −
| & | Binary AND operator copies a bit, to the result, if it exists in both operands |
| | | Binary OR operator copies a bit, if it exists in either operand. |
| ^ | Binary XOR operator copies the bit, if it is set in one operand but not both. |
| ~ | Binary Ones Complement operator is unary and has the effect of 'flipping' bits. |
| << | Binary Left Shift operator is moved left by the number of bits specified by the right operand. |
| >> | Binary Right Shift is moved right by the number of bits specified by the right operand. |
5. Python Logical Operator:
The following logical operators are supported by Python language.
| and | (Logical AND) Operator - If both the operands are true then condition becomes true. |
| or | (Logical OR) Operator - If any of the two operands are non-zero then condition becomes true. |
| not | (Logical NOT) Operator - Used to reverse the logical state of its operand. |
6. Python Membership Operators:
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −
| in Operator | Evaluates to true if it finds a variable in the specified sequence and false otherwise. |
| not in Operator | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. |
| is Operator | Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. |
| is not Oprator | Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. |