Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
/ | divide | 7 / 2 | 3.5 |
// | floor division | 7 // 2 | 3 |
% | modulus (remainder) | 7 % 2 | 1 |
** | power | 7 ** 2 | 49 |
/ always gives a float, even when the answer is whole: 8 / 2 is 4.0.
Floor division and remainder
Think of sharing 17 chocolates among 5 children:
chocolates = 17
children = 5
each = chocolates // children # 3 chocolates each
left = chocolates % children # 2 left over
% has many uses:
n % 2 == 0checks if a number is even.n % 10gives the last digit of a number.seconds % 60gives the seconds left after taking out whole minutes.
total = 3725
hours = total // 3600 # 1
minutes = (total % 3600) // 60 # 2
seconds = total % 60 # 5
Precedence: Python follows BODMAS
Python works out operators in this order, highest first:
()brackets**power*,/,//,%+,-
Operators on the same level are worked out left to right, except **, which goes right to left.
print(2 + 3 * 4) # 14, not 20
print((2 + 3) * 4) # 20
print(2 ** 3 ** 2) # 512, because 3 ** 2 is done first
When in doubt, add brackets. They make the order clear to anyone reading your code.
Assignment operators
A shortcut for "update a variable using its own value":
| Short form | Means |
|---|---|
x += 5 | x = x + 5 |
x -= 5 | x = x - 5 |
x *= 2 | x = x * 2 |
x /= 2 | x = x / 2 |
x //= 2 | x = x // 2 |
x %= 3 | x = x % 3 |
Python has no x++ or x--. Write x += 1.
Comparison operators
Each comparison gives True or False.
| Operator | Meaning | 5 ? 3 |
|---|---|---|
== | equal to | False |
!= | not equal to | True |
> | greater than | True |
< | less than | False |
>= | greater than or equal | True |
<= | less than or equal | False |
= stores a value; == compares two values. Mixing them up is a very common mistake.
Python allows chained comparisons: 0 <= marks <= 100 checks both limits at once.
Logical operators: and, or, not
| Operator | True when | Example |
|---|---|---|
and | both sides are true | age >= 18 and has_id |
or | at least one side is true | day == "Sat" or day == "Sun" |
not | flips the value | not is_absent |
marks = 72
attendance = 80
can_sit_exam = marks >= 33 and attendance >= 75
print(can_sit_exam) # True
Built-in maths functions
print(round(3.14159, 2)) # 3.14
print(abs(-7)) # 7
print(max(4, 9, 2)) # 9
print(min(4, 9, 2)) # 2
print(pow(2, 10)) # 1024, same as 2 ** 10
Example: GST bill
price = float(input("Price of the phone: "))
gst = price * 18 / 100
total = price + gst
print(f"GST: Rs {gst:.2f}")
print(f"Total: Rs {total:.2f}")
Common mistakes
- Expecting
/to give a whole number. Use//for that. - Writing
if x = 5instead ofif x == 5. - Forgetting brackets:
a + b / 2is not the average ofaandb;(a + b) / 2is. - Writing
0 < x and < 10. Each side ofandmust be a full comparison:0 < x and x < 10.
Key points
//gives the whole-number part of a division and%gives the remainder.- Precedence follows BODMAS; brackets override it.
- Comparisons give
TrueorFalse; combine them withand,or,not. x += 1is the Python way to add one.
Practice questions
1. What are the values of 17 // 4, 17 % 4 and 17 / 4?
4, 1 and 4.25.
2. What does print(10 - 4 / 2 * 3) show?
4.0. First 4 / 2 is 2.0, then 2.0 * 3 is 6.0, then 10 - 6.0 is 4.0.
3. Write an expression that is True when a year is a leap year.
(year % 4 == 0 and year % 100 != 0) or year % 400 == 04. A number is stored in n. How do you get its last digit?
n % 10. For example, 347 % 10 is 7.
5. What is the value of not (5 > 3 and 2 > 4)?
True. 5 > 3 is True and 2 > 4 is False, so the and is False, and not False is True.