a + b
, a
and b
are operands, and +
is the operator.In this article, we’ll dive into the 7 types of operators in Python, their usage, and examples, presented in an engaging and SEO-optimized format.**
) and floor division (//
).Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 3 = 1.67 |
// | Floor Division | 5 // 3 = 1 |
% | Modulo (Remainder) | 5 % 3 = 2 |
** | Exponentiation (Power) | 5 ** 3 = 125 |
Operator | Description | Example |
---|---|---|
& | AND | 5 & 3 = 1 |
` | ` | OR |
^ | XOR | 5 ^ 3 = 6 |
~ | NOT | ~5 = -6 |
<< | Left Shift | 5 << 1 = 10 |
>> | Right Shift | 5 >> 1 = 2 |
True
or False
). Python has three logical operators:Operator | Description | Example |
---|---|---|
and | Logical AND | (5 > 3) and (3 > 1) |
or | Logical OR | (5 > 3) or (3 < 1) |
not | Logical NOT | not(5 > 3) |
Operator | Description | Example |
---|---|---|
= | Assign | a = 5 |
+= | Add and Assign | a += 3 (a = 8) |
-= | Subtract and Assign | a -= 3 (a = 2) |
*= | Multiply and Assign | a *= 3 (a = 15) |
/= | Divide and Assign | a /= 3 (a = 1.67) |
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 3 (False) |
!= | Not equal to | 5 != 3 (True) |
> | Greater than | 5 > 3 (True) |
< | Less than | 5 < 3 (False) |
>= | Greater than or equal to | 5 >= 3 (True) |
<= | Less than or equal to | 5 <= 3 (False) |
Operator | Description | Example |
---|---|---|
is | Returns True if identical | a is b |
is not | Returns True if not identical | a is not b |
Operator | Description | Example |
---|---|---|
in | Returns True if found | 'a' in 'apple' |
not in | Returns True if not found | 'b' not in 'apple' |