Python Operators
Python operators are special symbols used to perform operations on variables and values. They are classified into several types.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus | 5 % 2 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
| Operator | Description | Example |
|---|---|---|
= | Assign value | x = 5 |
+= | Add and assign | x += 3 → x = x + 3 |
-= | Subtract and assign | x -= 2 → x = x - 2 |
*= | Multiply and assign | x *= 3 → x = x * 3 |
/= | Divide and assign | x /= 2 → x = x / 2 |
//= | Floor divide and assign | x //= 2 |
%= | Modulus and assign | x %= 3 |
**= | Exponent and assign | x **= 2 |
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3> | False |
>= | Greater than or equal | 5 >= 5 | True |
<= | Less than or equal | 5 <= 3 | False |
| Operator | Description | Example | Result |
|---|---|---|---|
and | Returns True if both are True | True and False | False |
or | Returns True if any is True | True or False | True |
not | Returns inverse | not True | False |
| Operator | Description | Example | Result |
|---|---|---|---|
& | AND | 5 & 3 | 1 |
| ` | ` | OR | `5 |
^ | XOR | 5 ^ 3 | 6 |
~ | NOT | ~5 | -6 |
<< | Left shift | 5 << 1> | 10 |
>> | Right shift | 5 >> 1 | 2 |
| Operator | Description | Example | Result |
|---|---|---|---|
in | Returns True if value exists | 'a' in 'Python' | False |
not in | Returns True if value does not exist | 'a' not in 'Python' | True |
| Operator | Description | Example | Result |
|---|---|---|---|
is | Returns True if both are the same object | x is y | Depends |
is not | Returns True if both are not the same object | x is not y | Depends |
Arithmetic operators handle math calculations.
Assignment operators update variable values.
Comparison operators are used in conditions.
Logical operators combine boolean expressions.
Bitwise operators work on binary values.
Membership and identity operators check existence and object identity.
Take quizzes related to this topic and see where you stand!
Start Quiz Now