Operators
Learn how to perform calculations, compare values, and combine conditions in Python.
Operators
An operator is a symbol that performs an operation on one or more values. You already know some from math — +, -, *. Python has several categories of operators, each with a different purpose.
Arithmetic operators
These do math. They work on numbers (int and float).
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.3333... |
// | Floor division | 10 // 3 | 3 |
% | Modulus | 10 % 3 | 1 |
** | Exponentiation | 2 ** 8 | 256 |
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.3333333333333335
print(10 // 3) # 3 — divides and drops the decimal
print(10 % 3) # 1 — remainder after division
print(2 ** 8) # 256Division always returns a float in Python, even if the result is a whole number:
print(10 / 2) # 5.0 — not 5
print(10 // 2) # 5 — use // if you want an integerModulus (%) gives you the remainder. It is more useful than it looks — a classic use is checking if a number is even or odd:
print(10 % 2) # 0 — no remainder, so 10 is even
print(7 % 2) # 1 — remainder of 1, so 7 is odd+ also works on strings — it joins them together:
first = "Hello"
second = "World"
print(first + " " + second) # Hello WorldComparison operators
These compare two values and always return a bool — either True or False.
| Operator | Meaning | 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 | 3 <= 5 | True |
age = 20
print(age == 20) # True
print(age != 18) # True
print(age > 18) # True
print(age < 18) # False
print(age >= 20) # True
print(age <= 19) # FalseDo not confuse = and ==. A single = assigns a value to a variable. A double == compares two values. Using = when you mean == is one of the most common beginner mistakes.
Logical operators
These combine multiple conditions together.
| Operator | Meaning | Example | Result |
|---|---|---|---|
and | Both must be True | True and False | False |
or | At least one must be True | True or False | True |
not | Flips True to False | not True | False |
age = 25
has_id = True
# and — both conditions must be True
print(age >= 18 and has_id) # True
# or — at least one condition must be True
print(age < 18 or has_id) # True
# not — reverses the result
print(not has_id) # FalseA real example — checking if someone can enter:
age = 20
has_ticket = True
if age >= 18 and has_ticket:
print("Welcome in!")
else:
print("Sorry, you cannot enter.")
# Welcome in!Assignment operators
These assign a value to a variable. You already know =. The others are shortcuts that combine assignment with an operation.
| Operator | Meaning | Example | Same as |
|---|---|---|---|
= | Assign | x = 5 | x = 5 |
+= | Add and assign | x += 3 | x = x + 3 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 3 | x = x * 3 |
/= | Divide and assign | x /= 3 | x = x / 3 |
//= | Floor divide and assign | x //= 3 | x = x // 3 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
**= | Exponent and assign | x **= 3 | x = x ** 3 |
score = 10
score += 5 # score is now 15
score *= 2 # score is now 30
score -= 10 # score is now 20
print(score) # 20Identity operators
These check if two variables point to the exact same object in memory — not just the same value.
| Operator | Meaning |
|---|---|
is | Same object |
is not | Not the same object |
x = None
print(x is None) # True
print(x is not None) # FalseUse is only for comparing with None, True, or False. For everything else, use ==. Two variables can have the same value but be different objects — is would return False in that case.
Membership operators
These check if a value exists inside a sequence like a string, list, or tuple.
| Operator | Meaning |
|---|---|
in | Value is in the sequence |
not in | Value is not in the sequence |
name = "Ali Ahmed"
print("Ali" in name) # True
print("Khan" not in name) # True
numbers = [1, 2, 3, 4, 5]
print(3 in numbers) # True
print(9 in numbers) # FalseOperator precedence
When multiple operators appear in one expression, Python follows a specific order — just like BODMAS in math. Higher in the list means evaluated first:
**— exponentiation*,/,//,%— multiplication and division+,-— addition and subtraction==,!=,>,<,>=,<=— comparisonsnot— logical notand— logical andor— logical or
print(2 + 3 * 4) # 14 — not 20, multiplication first
print((2 + 3) * 4) # 20 — parentheses override the order
print(2 ** 3 + 1) # 9 — exponent first, then + 1When in doubt, use parentheses. They make your intent clear and prevent bugs from unexpected precedence.
Summary
| Category | Operators |
|---|---|
| Arithmetic | + - * / // % ** |
| Comparison | == != > < >= <= |
| Logical | and or not |
| Assignment | = += -= *= /= and more |
| Identity | is is not |
| Membership | in not in |