--- title: Basic Operators --- ## Basic Operators Operators are symbols that tell the interpreter to do a specific operation (e.g. arithmetic, comparison, logical, etc.) The different types of operators in Python are listed below: 1. Arithmetic Operators 2. Comparison (Relational) Operators 3. Bitwise Operators 4. Assignment Operators 5. Logical Operators 6. Membership Operators 7. Identity Operators #### Arithmetic Operators An arithmetic operator takes two operands as input, performs a calculation and returns the result. Consider the expression, “a = 2 + 3”. Here, `2` and `3` are the operands and `+` is the arithmetic operator. The result of the operation is stored in the variable a. (This is because `=` is an assignment operator. See below.)
Operator | Description | Usage |
---|---|---|
+ | Performs Addition on the operands | 12 + 3 = 15 |
- | Performs Subtraction on the operands. Subtracts the right operand from the left operand |
12 - 3 = 9 |
* | Performs Multiplication on the operands | 12 * 3 = 36 |
/ | Performs Division on the operands. Divides the left operand by the right operand |
12 / 3 = 4 |
Note: When two integers are used, the result differs between Python 2 and Python 3. | 5 / 2 = 2 in Python 2 | 5 / 2 = 2.5 in Python 3 |
% | Performs a Modulus on the operands. Returns the remainder obtained while dividing the left operand by the right operand |
16 % 3 = 1 |
** | Performs an Exponentiation operation. The left operand is raised to the power of right operand |
12 ** 3 = 1728 |
// | Performs a Floor Division operation. Returns the integral part of the quotient obtained after diving the left operand by the right operand |
18 // 5 = 3 |
Operator | Description | Usage |
---|---|---|
> | Returns True if the left operand is greater than the right operand Returns False otherwise |
12 > 3 returns True |
< | Returns True if the right operand is greater than the left operand Returns False otherwise |
12 < 3 returns False |
== | Returns True if both the operands are equal Returns False otherwise |
12 == 3 returns False |
>= | Returns True if the left operand is greater than or equal to the right operand Returns False otherwise |
12 >= 3 returns True |
<= | Returns True if the right operand is greater than or equal to the left operand Returns False otherwise |
12 <= 3 returns False |
!= | Returns True if both the operands are not equal Returns False otherwise |
12 != 3 returns True |
Operator | Description | Usage |
---|---|---|
& | Performs bitwise AND operation on the operands | a & b = 2 Binary: 10 & 11 = 10 |
| | Performs bitwise OR operation on the operands | a | b = 3 Binary: 10 | 11 = 11 |
^ | Performs bitwise XOR operation on the operands | a ^ b = 1 Binary: 10 ^ 11 = 01 |
~ | Performs bitwise NOT operation on the operand Flips every bit in the operand |
~a = -3 Binary: ~(00000010) = (11111101) |
>> | Performs a bitwise right shift. Shifts the bits of left operand, right by the number of bits specified as the right operand | a >> b = 0 Binary: 00000010 >> 00000011 = 00000000 |
<< | Performs a bitwise left shift. Shifts the bits of left operand, left by the number of bits specified as the right operand | a << b = 16 Binary: 00000010 << 00000011 = 00001000 |
Operator | Usage |
---|---|
= | a = 5. The value 5 is assigned to the variable a |
+= | a += 5 is equivalent to a = a + 5 |
-= | a -= 5 is equivalent to a = a - 5 |
*= | a *= 3 is equivalent to a = a * 3 |
/= | a /= 3 is equivalent to a = a / 3 |
%= | a %= 3 is equivalent to a = a % 3 |
**= | a **= 3 is equivalent to a = a ** 3 |
//= | a //= 3 is equivalent to a = a // 3 |
&= | a &= 3 is equivalent to a = a & 3 |
|= | a |= 3 is equivalent to a = a | 3 |
^= | a ^= 3 is equivalent to a = a ^ 3 |
>>= | a >>= 3 is equivalent to a = a >> 3 |
<<= | a <<= 3 is equivalent to a = a << 3 |
Operator | Description | Usage |
---|---|---|
and | Returns True if both the operands are True Returns False otherwise |
a and b |
or | Returns True if any one of the operands are True Returns False otherwise |
a or b |
not | Returns True if the operand is False Returns False otherwise |
not a |