Python - Operators

Operators are special symbols that carry out the operations on values and variables.

Python has the following different operator groups.

  • Arithmetic Operators
  • Assignment Operators
  • Logical Operators
  • Comparison Operators
  • Bitwise Operators
  • Identity Operators
  • Membership Operators

Arithmetic Operators

Arithmetic operators are used to perform the mathematical operators which includes addition, subtraction, multiply, division etc.,


Python supports the arithmetic operations for both integers and float(decimal).

Operator Type Definition Syntax
+ Addition operator a + b
- Subtraction operator a - b
* Multiplication operator a * b
/ Division operator a / b
% Module operator a % b
** Left operand power of Right operand a ** b
// Floor Division (Remainder of the division operation) a // b

Assignment Operators

Assignment operators are used to assign a value to a variable.

Operator Type Example Output
= a = 5 a = 5
+= a += 5 a = a +5
-= a -= 5 a = a- 5
*= a *= 5 a = a * 5
%= a %= 5 a = a % 5
/= a /= 5 a = a / 5
//= a //= 5 a = a // 5
**= a **= 5 a = a ** 5
&= a &= 5 a = a & 5
|= a |=5 a = a | 5
^= a ^= 5 a = a ^= 5
>>= a >>= 5 a = a >>= 5
<<= a <<= 5 a = a <<= 5

Logical Operators

Logical operators are used to check the conditions and return the True or False.

Operator Type Definition Example
And Returns True if both conditions or operands are true a and b
Or Returns Ture if any one of the condition or operands is true a or b
Not Returns True if condition or operand is False not(a)

Comparison Operators

Comparison operators are used to compare the values. Returns True or False based on the condition.

Operator Type Definition Example
Equal To (==) Return True if both operands are equal a == b
Not Equal To (!=) Return True if both operands are not equal a != b
Greater than (>) Return True if left operand is greater than the right operand a > b
Less than (<) Return True if left operand is less than the right operand a < b
Greater than or equal (>=) Return True if left operand is greater or equal to the right operand a >= b
Less than or equal (<=) Return True if left operand is less or equal to the right operand a <= b

Bitwise Operators

Bitwise Operators are used compare the binary numbers bit by bit. Binary digits are 0 and 1.

Operator Type Operator Name Definition
& Bitwise AND sets each bit to 1 in the binary number if both bits are 1
| Bitwise OR sets each bit to 1 in the binary number if either bit is 1
^ Bitwise XOR sets each bit to 1 in the binary number if only one of two bits is 1
~ Bitwise NOT Invert the all the bits in the binary number
>> Bitwise Right Shift Shift right by pushing the left most bit and let the rightmost bits fall off.
<< Bitwise Left Shift Shift left by pushing zeros from the right and let the leftmost bits fall off

Identity Operators

Identity operators are used to compare the two values whether they are pointing to same memory location.

If two variables are equal that doesn’t mean both objects are pointing to same of the memory location.

Operator Type Definition Example
Is Returns True if both variables are same object a is b
Is not Returns True if both variables are not the same object a is not b

Membership Operators

Membership operators are used to check whether a value or a variable in sequence.

Operator Type Definition Example
In Returns True if the value found in a sequence a in b
Not In Returns True if the value is not found in a sequence a not in b


Related Tutorials