Operators in Python | Types of Operators in Python with Example
In this post we will learn about what is operator and different types of operators in Python.
What is an Operator?
An operator is a special symbol which is use to perform operation on variable or values.
Types of Operators in Python
- Arithmetic operators
- Logical operators
- Comparison operators
- Identity operators
- Membership operators
- Bitwise operators
- Assignment operators
Python Arithmetic Operators
Arithmetic operators in python is used to perform mathematical calculations like addition, subtraction, multiplication etc.
Symbol | Operation | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponent | x ** y |
// | Floor Division | x // y |
Python Comparison Operators
The comparison operator in python is used to compare two operands.
Symbol | Operation | Example |
---|---|---|
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than equal to | x >= y |
<= | Less than equal to | x <= y |
Python Logical Operators
The logical operator in python is used to perform logical operations on two values or variables. It is used in conditional statement that is either true or false.
Symbol | Operation | Example |
---|---|---|
and | Returns true if both the conditions are true | x < 8 and x < 10 |
or | Returns true if any one condition is true | x < 6 or x < 10 |
not | Returns true if the result is false and vice versa | not(x < 6 or x < 10) |
Python Identity Operators
Python identity operator is used to compare two objects not only if they are equal but also make sure that both the objects are equal with same memory location.
Symbol | Operation | Example |
---|---|---|
is | Returns true only if both the variables are same object | x is y |
is not | Returns true only if both the variables are not same object | x is not y |
Python Membership Operator
The membership operator in python is used to validate the membership of value.
Symbol | Operation | Example |
---|---|---|
in | Returns true if the sequence with the specified value is present in the object | x in y |
not in | Returns true if the sequence with the specified value is not present in the object | x in not y |
Python Bitwise Operators
Bitwise operator in Python is used to perform action on bit level programmimg.
Symbol | Operation | Description |
---|---|---|
& | AND | Set each bit to 1 if both bits are 1 |
| | OR | Set each bit to 1 if one of the two bits is 1 |
^ | XOR | Set each bit to 1 if only one of the two bits is 1 |
~ | NOT | Inverts all bits |
<< | Left Shift | |
>> | Right Shift |
Python Assignment Operators
The assignment operator in python is used to assign values to the variable.
Symbol | Operation | Example |
---|---|---|
= | x = 10 | x = 10 |
+= | x = x + 10 | |
-= | x = x - 10 | x -= 10 |
*= | x = x * 10 | x *= 10 |
/= | x = x / 10 | x /= 10 |
%= | x = x % 10 | x %= 10 |
//= | x = x // 10 | x //= 10 |
**= | x = x ** 10 | x **= 10 |
&= | x = x & 10 | x &= 10 |
|= | x = x | 10 | x |= 10 |
^= | x = x ^ 10 | x ^= 10 |
>>= | x = x >>= 10 | x >>= 10 |
<<= | x = x <<= 10 | x <<= 10 |