Use of Operators on Python

Author: Al-mamun Sarkar Date: 2021-04-04 10:58:44

Operators are being used to perform various operations. Some operators work with 2 operands called binary operators and some operators work with only one operand is called unary operators. 

 

Python has the following types of Operators:

  1. Python Arithmetic operators
  2. Python Assignment operators
  3. Python Comparison operators
  4. Python Logical operators
  5. Python Identity operators
  6. Python Membership operators
  7. Python Bitwise operators

 

 

Arithmetic Operators in Python:

Name Operator Example Use Case
Addition Operator + a + b Add two numbers
Subtraction Operator - a - b Calculate Subtraction
Multiplication Operator * a * b Calculate Multiplication
Division Operator / x / y Calculate Division
Modulus Operator % x % y Calculate Modulus
Exponentiation Operator ** a ** b Calculate x to the power y.
Floor division Operator // a // b Calculate Quotient

 

 

Comparison Operators in Python:

Name Operator Example
Equal Operator == a == b
Not equal Operator != a != b
Greater than Operator > a > b
Less than Operator < a < b
Greater than or equal to Operator >= a >= b
Less than or equal to Operator <= b <= b

 

 

Python Assignment Operators

Operator Example Same As
= a = 5 a = 5
+= a += 3 a = a + 3
-= a -= 3 a = a - 3
*= a *= 3 a = a * 3
/= a /= 3 a = a / 3
%= a %= 3 a = a % 3
//= a //= 3 a = a // 3
**= a **= 3 a = a ** 3
&= a &= 3 a = a & 3
|= a |= 3 a = a | 3
^= a ^= 3 a = a ^ 3
>>= a >>= 3 a = a >> 3
<<= a <<= 3 a = a << 3

 

 

Logical Operators in Python:

Operator Use
and  Logical AND
or Logical OR
not Logical NOT

 

 

Identity Operators in Python:

Operator Use Case Example
is  True for the same object a is b
is not True for different object a is not b

 

 

Membership Operators in Python:

Operator Example
in  a in b
not in a not in b

 

Bitwise Operators in Python:

Name Operator
Bitwise AND
Bitwise OR |
Bitwise XOR  ^
Bitwise NOT
Bitwise Zero fill left shift <<
Bitwise Signed right shift >>