Similar to other programming languages, Python also has some special characters called operators. Operators in Python are useful to perform different operations on variables/values. These variables/values which are passed as an input to an operator are known as operands. Further, these operands & operators form an expression.
Consider a basic arithmetic expression, a + b
. Here, a and b are operands and the symbol +
is an operator.
Types of Operators in Python
Operators in Python are classified into the following 7 types:
- Arithmetic Operators
- Bitwise Operators
- Assignment Operators
- Relational or Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
a) Arithmetic Operators in Python
Using arithmetic operators, we can perform mathematical operations like addition, subtraction, multiplication, division, etc. In Python, we also have additional arithmetic operators such as exponent and floor division.
Click here to practice example Python programs using arithmetic operators.
Operator | Name | Description |
+ | Addition | The operands on either side of this operator are added |
– | Subtraction | The operand on the right side of this operator is subtracted from the one on the left side |
* | Multiplication | The operands on either side of this operator are multiplied |
/ | Division | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of a floating-point value |
% | Modulo | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the remainder value |
** | Exponent | This operator raises the left side operand to the power of the right side operand |
// | Floor division | The operand on the left side of this operator is divided by the one on the right side. As a result, it returns the quotient in the form of an integer value |
b) Bitwise Operators in Python
Python bitwise operators are used to perform bitwise calculations on integers. The given integers are converted into binary format and then operations are performed bit by bit. These are also called as Binary Operators.
Operator | Name |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Bitwise left shift |
>> | Bitwise right shift |
Go ahead and try to understand the example Python programs using Bitwise operators.
c) Logical Operators in Python
There are three logical operators in Python – and, or and not. They are used to combine two or more conditional statements.
Operator | Description |
and | The output is true when both the expressions are true |
or | The output is true if either one of the expression is true |
not | Reverses the output |
d) Assignment Operators in Python
Using assignment operators, we can assign values to an operand.
Operator | Name | Description |
= | Assignment | This operator assigns its right-side value to its left-side operand |
+= | Addition assignment | This operator adds left and the right side operands and assigns the result to the left side operand |
-= | Subtraction assignment | This operator subtracts the right operand from the left operand and assigns the result to the left side operand |
*= | Multiplication assignment | This operator multiplies the right-side operand with the left-side operand and assigns the result to the left side operand |
/= | Division assignment | This operator divides the left side operand by the right-side operand and assigns the quotient to the left side operand |
%= | Modulus assignment | This operator divides the left side operand by the right side operand and assigns the remainder to the left side operand |
**= | Exponentiation assignment | This operator raises the left side operand to the power of the right-side operand and assigns the result value to the left side operand |
//= | Floor division assignment | This operator divides the left operand by the right operand and assigns the quotient value (in the form of an integer value) to the left operand |
&= | Bitwise AND assignment | This operator performs a bitwise AND operation on both the left and the right side operands and assigns the result to the left side operand |
|= | Bitwise OR assignment | This operator performs a bitwise OR operation on both the left and the right side operands and assigns the result to the left side operand |
^= | Bitwise XOR assignment | This operator performs bitwise XOR operation on both the left and the right side operands and assigns the result to the left side operand |
>>= | Bitwise right shift assignment | This operator right shifts the given value by the specified position and assigns the result to the left side operand |
<<= | Bitwise left shift assignment | This operator left shifts the given value by the specified position and assigns the result to the left side operand |
>> Know more about Assignment operators in Python
e) Relational or Comparison Operators in Python
Using these operators, we can compare two operands and determine their relationship.
Operator | Name | Description |
== | Equal to | Checks whether two operands are equal |
!= | Not equal to | Checks whether two operands are not equal |
> | Greater than | Checks whether the left side operand is greater than the right side operand |
< | Less than | Checks whether the left side operand is less than the right side operand |
>= | Greater than or equal to | Checks whether the left side operand is either greater or equal to the right side operand |
<= | Lesser than or equal to | Checks whether the left side operand is either less than or equal to the right side operand |
f) Identity Operators in Python
Identity operators is
and is
not are used to check whether the memory locations of two variables/objects are the same or not. Also, we can use these operators to find if a variable/object belongs to a particular type or not.
Operator | Description |
---|---|
is | Returns True if the operands on either side of the operator point to the same object and false otherwise. |
is not | Returns False if the operands on either side of the operator point to the same object and true otherwise. |
g) Membership Operators in Python
Membership operators in
and not in
are used to find whether a value is present in a particular Python object or not.
Operator | Description |
---|---|
in | Returns True if it finds a variable in the specified sequence and false otherwise. |
not in | Returns False if it does not find a variable in the specified sequence and false otherwise. |