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 are used to perform an 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 are used to perform mathematical calculations like addition, subtraction, multiplication, etc.
|+||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.
|==||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 a conditional statement that is either true or false.
|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 to make sure that both the objects are equal with the same memory location.
|is||Returns true only if both the variables are the same object||x is y|
|is not||Returns true only if both the variables are not the same object||x is not y|
Python Membership Operator
The membership operator in python is used to validate the membership of value.
|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 an action on bit-level programming.
|&||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|
Python Assignment Operators
The assignment operator in python is used to assign values to the variable.
|=||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|