Python - Operators
Operators are used to perform operation on two operands. Operators in Python can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic operators are used to perform arithmetic operations on two operands.
|+||Addition||Add two values|
|-||Subtraction||Subtract one value from another|
|*||Multiplication||Multiply two values|
|/||Division||Divide one value by another|
|**||Exponent / Power||Returns first operand raised to the power of second operand|
|%||Modulus||Returns remainder of division operation|
|//||Floor division||Returns quotient of division operation|
Assignment operators are used to assign values of right hand side expression to left hand side operand.
|=||a = 5||a = 5||Example|
|+=||a += b||a = a + b|
|-=||a -= b||a = a - b|
|*=||a *= b||a = a * b|
|/=||a /= b||a = a / b|
|**=||a **= b||a = a ** b|
|%=||a %= b||a = a % b|
|//=||a //= b||a = a // b|
|&=||a &= b||a = a & b||More Info|
||=||a |= b||a = a | b||More Info|
|^=||a ^= b||a = a ^ b||More Info|
|>>=||a >>= b||a = a >> b||More Info|
|<<=||a <<= b||a = a << b||More Info|
Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.
|>=||Greater than or equal to|
|<=||Less than or equal to|
Logical operators are used to combine two or more conditions.
|and||Returns True when all conditions are true|
|or||Returns True when any of the conditions is true|
|not||Returns opposite boolean result|
Identity operators are used to compare memory location of two objects.
|is||Returns true when both variables are the same object||a is b|
|is not||Returns true when both variables are not the same object||a is not b|
Membership operators are used to check the membership of element(s) in a sequence like lists, tuple etc.
|in||Returns True if element(s) is present in the object||a in b|
|not in||Returns True if element(s) is not present in the object||a not in b|
Bitwise operators are used to perform bitwise operations on two operands.
|&||AND||Returns 1 if both bits at the same position in both operands are 1, else returns 0||More Info|
||||OR||Returns 1 if one of two bits at the same position in both operands is 1, else returns 0||More Info|
|^||XOR||Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0||More Info|
|~||NOT||Reverse all the bits||More Info|
|>>||Right shift||The left operand is moved right by the number of bits present in the right operand||More Info|
|<<||Left shift||The left operand value is moved left by the number of bits present in the right operand||More Info|