Python Operators
You will use Python operators to calculate, compare values, and combine conditions.
Arithmetic operators
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 2 + 3 gives 5 |
| - | Subtraction | 7 - 2 gives 5 |
| * | Multiplication | 4 * 3 gives 12 |
| / | Division | 7 / 2 gives 3.5 |
| // | Floor division | 7 // 2 gives 3 |
| % | Remainder | 7 % 2 gives 1 |
| ** | Power | 2 ** 3 gives 8 |
total = 17 people = 4 print(total // people) print(total % people) print(2 ** 5)
17 // 4 keeps only the whole-number result, so it prints 4. 17 % 4 prints the leftover 1. Finally, 2 ** 5 means 2 to the fifth power, which is 32.
Assignment and comparison
| Operator | Meaning | Example |
|---|---|---|
| = | Assign a value | score = 10 |
| += | Add, then assign | score += 2 |
| -= | Subtract, then assign | score -= 2 |
| *= | Multiply, then assign | score *= 2 |
| /= | Divide, then assign | score /= 2 |
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | 5 == 5 |
| != | Not equal to | 5 != 3 |
| > | Greater than | 5 > 3 |
| < | Less than | 3 < 5 |
| >= | Greater than or equal to | 5 >= 5 |
| <= | Less than or equal to | 3 <= 5 |
score = 8 score += 4 print(score) print(score >= 10) print(score == 12)
+= adds 4 to the current score, so the first line is 12. The next two comparisons ask whether 12 meets each condition. Both answers are True.
Logical membership and identity
| Operator | Meaning | Example |
|---|---|---|
| and | True when both conditions are true | age >= 18 and has_ticket |
| or | True when either condition is true | is_weekend or is_holiday |
| not | Reverses a Boolean value | not is_closed |
| Operator | Meaning | Example |
|---|---|---|
| in | Value appears in a collection | 'a' in 'cat' |
| not in | Value does not appear in a collection | 4 not in [1, 2, 3] |
| Operator | Meaning | Example |
|---|---|---|
| is | Checks whether two names refer to the same object | item is None |
| is not | Checks whether two names do not refer to the same object | item is not None |
age = 20
has_ticket = True
word = 'python'
value = None
print(age >= 18 and has_ticket)
print('py' in word)
print(value is None)
print(2 + 3 * 4)
The first three tests are true: the age and ticket conditions both pass, 'py' appears in the word, and the value is exactly None. Python follows precedence rules: multiplication happens before addition, so 2 + 3 * 4 is 14. Use parentheses when you want to make the order clear.
Key points
- Use // for whole-number division and % for a remainder.
- Compound assignment updates a variable in place.
- Comparisons produce True or False.
- Use in for membership and is mainly for None.
- Parentheses make evaluation order clear.
Worked example
Four friends share a 1250-taka dinner. Floor division gives everyone's share; modulo tells what is left over for the tip bowl.
bill = 1250 friends = 4 share = bill // friends leftover = bill % friends print(share, leftover)
How it works, step by step
- // divides and throws away the fraction: 1250 // 4 = 312.
- % keeps only the remainder: 1250 % 4 = 2 taka.
- Together they describe a complete division.
References & Further Reading
Related entries from PyLabs's own reference library.
Practice exercise
Not passed yetSet apples to 23 and boxes to 5. Print how many whole apples go in each box, then print the number left over.
Expected output:
4
3
# Write your code below
Check yourself
Python Operators quiz
10 questions. You get instant feedback per question, and the timer starts when you press the button.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.