Operators in Python are symbols used to perform operations on variables and values. They are essential for performing calculations, comparisons, and logical decisions in programs.
Python provides the following main types of operators:
These operators are used to perform mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | 5 + 2 = 7 |
- | Subtraction | 5 - 2 = 3 |
* | Multiplication | 5 * 2 = 10 |
/ | Division | 5 / 2 = 2.5 |
// | Floor Division | 5 // 2 = 2 |
% | Modulus | 5 % 2 = 1 |
** | Exponent | 2 ** 3 = 8 |
a = 10b = 3 print(a + b)print(a % b)print(a ** b)These operators are used to compare values and return True or False.
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater or equal |
<= | Less or equal |
x = 5y = 3 print(x > y)print(x == y)Used to combine conditional statements.
| Operator | Meaning |
|---|---|
and | True if both conditions are True |
or | True if at least one is True |
not | Reverses the result |
a = 10 print(a > 5 and a < 20)print(not(a > 5))Used to assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
= | x = 5 | Assign value |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
x = 5x += 3print(x)Used to check if a value exists in a sequence.
| Operator | Meaning |
|---|---|
in | Value exists |
not in | Value does not exist |
text = "Python" print("P" in text)print("z" not in text)Used to compare memory locations of objects.
| Operator | Meaning |
|---|---|
is | Same object |
is not | Different objects |
a = 10b = 10 print(a is b)Operator precedence determines which operation is performed first.
Order (high to low):
** (Exponent)*, /, //, %+, -result = 2 + 3 * 4print(result)Output
14Operators are used to perform operations on data.
Types include:
Operator precedence controls execution order.
Operators are essential for calculations, conditions, and logic building.