What is an operator?#
An operator is a symbol that performs an operation on values. In 10 + 5, the + is the operator and 10 and 5 are the operands.
Python has these groups of operators:
- Arithmetic
- Comparison (relational)
- Logical
- Assignment
- Membership
- Identity
- Bitwise
1. Arithmetic operators#
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 3 |
13 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 10 * 3 |
30 |
/ |
Division (always float) | 10 / 3 |
3.3333... |
// |
Floor division | 10 // 3 |
3 |
% |
Modulus (remainder) | 10 % 3 |
1 |
** |
Exponent (power) | 10 ** 3 |
1000 |
/हमेशा पूरा भाग देता है और जवाब float में आता है:10 / 2 = 5.0//(floor division) भाग देकर दशमलव हटा देता है:17 // 5 = 3%(modulus) शेषफल (remainder) देता है:17 % 5 = 2(क्योंकि 5 × 3 = 15 और 17 - 15 = 2)
% बहुत काम का है: कोई संख्या सम (even) है या नहीं - n % 2 == 0; आखिरी अंक निकालना - n % 10।
Where // and % are used in real programs#
Operators with strings#
print("Py" + "thon") # Python (joining = concatenation)
print("ha" * 3) # hahaha (repetition)
2. Comparison operators#
They compare two values and return True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
equal to | 5 == 5 |
True |
!= |
not equal to | 5 != 3 |
True |
> |
greater than | 5 > 8 |
False |
< |
less than | 5 < 8 |
True |
>= |
greater than or equal | 5 >= 5 |
True |
<= |
less than or equal | 4 <= 3 |
False |
= assigns a value. == compares two values. Writing if x = 5: is a SyntaxError.
Python allows chained comparisons, just like maths:
3. Logical operators#
Combine conditions:
| Operator | Result is True when... | Example |
|---|---|---|
and |
both sides are True | age > 18 and has_id |
or |
at least one side is True | is_weekend or is_holiday |
not |
reverses True/False | not is_raining |
Truth table:
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
Short-circuiting: in A and B, if A is False, Python doesn't even check B (the answer is already False). In A or B, if A is True, B is skipped. This avoids errors like x != 0 and 10 / x > 2.
4. Assignment operators#
Shortcuts for updating a variable:
| Operator | Same as |
|---|---|
x += 5 |
x = x + 5 |
x -= 5 |
x = x - 5 |
x *= 5 |
x = x * 5 |
x /= 5 |
x = x / 5 |
x //= 5 |
x = x // 5 |
x %= 5 |
x = x % 5 |
x **= 2 |
x = x ** 2 |
Python has no ++ or -- operators. Use count += 1.
5. Membership operators: in, not in#
Check whether a value exists inside a string, list, tuple, set or dict:
6. Identity operators: is, is not#
is checks whether two names point to the same object in memory (not just equal values).
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True - same values
print(a is b) # False - two different list objects
print(a is c) # True - same object
Use is mainly with None: write if result is None:. For comparing values, always use ==.
7. Bitwise operators (overview)#
They work on the binary (0/1) form of integers. Used in networking, graphics and competitive programming.
| Operator | Name | Example (5 = 101, 3 = 011) |
Result |
|---|---|---|---|
& |
AND | 5 & 3 |
1 |
| |
OR | 5 | 3 |
7 |
^ |
XOR | 5 ^ 3 |
6 |
~ |
NOT | ~5 |
-6 |
<< |
left shift | 5 << 1 |
10 |
>> |
right shift | 5 >> 1 |
2 |
Operator precedence#
When an expression has several operators, Python follows this order (highest first):
| Priority | Operators |
|---|---|
| 1 | ( ) brackets |
| 2 | ** (right to left) |
| 3 | +x, -x, ~x (unary) |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | <<, >>, &, ^, | |
| 7 | ==, !=, >, <, >=, <=, is, in |
| 8 | not |
| 9 | and |
| 10 | or |
When in doubt, use brackets. (a + b) * c is clearer than relying on rules - and clear code has fewer bugs.
Summary#
- Arithmetic:
+ - * / // % **-/always gives float,//floors,%gives remainder. - Comparison operators return
True/False;==compares,=assigns. - Logical:
and,or,not(with short-circuiting). - Assignment shortcuts:
+=,-=,*=, etc. No++in Python. inchecks membership,ischecks identity (use withNone).- Brackets first, then
**, then* / // %, then+ -, then comparisons,not,and,or.