PART 1 · PYTHON FROM ZERO · LESSON 3
Numbers and maths
FreeLesson 5 of 78
Python is a calculator that never gets tired. Learn the operators once and you can total, average and split any amount of data.
print(7 + 2, 7 - 2, 7 * 2)
print(7 / 2) # true division: always a decimal
print(7 // 2) # floor division: drop the decimals
print(7 % 2) # remainder (modulo)
print(2 ** 10) # power: 2 to the 10
print(2 + 3 * 4, (2 + 3) * 4) # * before +, brackets firstOutput
9 5 14 3.5 3 1 1024 14 20
| Symbol | Does |
|---|---|
+ - * | Add, subtract, multiply |
/ | Divide, gives a float |
// | Divide, round down |
% | Remainder. x % 2 == 0 means x is even |
** | Power |
amount = 1198
gst = amount * 0.18
print("GST:", gst, "rounded:", round(gst, 2))
total = 0
total += 1198 # same as total = total + 1198
total += 796
total -= 50 # a discount
print("total:", total)
orders, days = 15, 4
print("average per day:", orders / days, round(orders / days, 1))
print(abs(-50), max(3, 9, 4), min(3, 9, 4))Output
GST: 215.64 rounded: 215.64 total: 1944 average per day: 3.75 3.8 50 9 3
text_amount = "1198.50" # from a file: it is text!
print(text_amount + "1") # joins text, does not add
amount = float(text_amount) # convert text to a number
print(amount + 1)
print(int("15") + 5, int(9.99), str(42) + " orders")Output
1198.501 1199.5 20 9 42 orders
Try it yourself before moving on
- Is 2025 an even number? Use %.
- Split 2,847 rupees equally between 4 people, rounded to 2 decimals.
- Turn the text "1,499" into the number 1499 (hint: replace, then int).