Operators are used to performing operations on variables and values. In this article, you will learn operators in python in detail with examples.
Operators in python are divided into the following types:
- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Assignment Operators
- Membership operators
- Identity operators
- Bitwise operators
Arithmetic operators are used for mathematical computations. Arithmetic operators are used with numeric values like integers or float.
|+||Addition Operator: Adds two values||a * b|
|–||Subtraction Operator: Subtracts two Values||a – b|
|*||Multiplication Operator: Multiplies two Values||a * b|
|/||Division Operator: Divides two Values||a / b|
|//||Floor Division Operator: Returns result of the division in integer||a // b|
|%||Modulus Operator: Returns the remainder after division||a % b|
|**||Exponent Operator: Performs exponentiation operation||a ** b|
|and||Returns True if both conditions are true||a < b and b < c|
|or||Returns True if one of the conditions is true||a< b or b < c|
|not||Reverse the result i.e returns true if the result is true||not (a < b)|
|>||Greater than||a > b|
|<||Less than||a < b|
|>=||Greater than or equal to||a >= b|
|<=||Less than or equal to||a <= b|
|==||Equal||a == b|
Assignment operators are used to assigning values to the variables.
|+||a = 2||a = 2|
|+=||a += 2||a = a + 2|
|-=||a -= 2||a = a – 2|
|*=||a *= 2||a = a * 2|
|/=||a /= 2||a = a / 2|
|%=||a %= 2||a = a % 2|
|//=||a //= 2||a = a // 2|
|**=||a **= 2||a = a ** 2|
|&=||a &= 2||a = a & 2|
||=||a |= 2||a = a | 2|
|^=||a ^= 2||a = a ^ 2|
|<<=||a <<= 2||a = a << 2|
|>>=||a >>= 2||a = a >> 2|
Membership operators in python are used to check if a particular item or substring is presented in a string, list, tuple, etc.
|in||in operator returns True if a particular item or substring is present in the string, list, tuple, etc. But it returns False if that item is not present.||a in b|
|not in||not in operator returns True if a particular item is not present in the string, list, tuple, etc. But it returns False if that item is present.||a not in b|
Identity operators in python checks if two objects or variables are the same or not and if their memory location is also the same or not.
|is||is operator returns True if two objects or variables are the same and their memory location is also the same.||a is b|
|is not||is not operator returns True if two objects or variables are not the same and their memory location is also not the same.||a is not b|
Bitwise operators treat operands as binary digits and perform operations bit by bit.
|&||Bitwise AND operator||a & b|
||||Bitwise OR operator||a | b|
|^||Bitwise XOR operator||a ^ b|
|~||Bitwise NOT operator||~a|
|<<||Bitwise left shift operator||a<<|
|>>||Bitwise right shift operator||a>>|
Operators are used to performing operations on variables and values. There are different types of operators in python like arithmetic, logical, comparison, assignment, membership, identity, and bitwise.