![]() |
|
CHAPTER 7 — Numbers & Maths in Python - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 7 — Numbers & Maths in Python (/showthread.php?tid=225) |
CHAPTER 7 — Numbers & Maths in Python - Leejohnston - 11-15-2025 Chapter 7 — Numbers & Maths in Python How Python works with integers, decimals, and calculations Python can handle numbers just as easily as it handles text. In this chapter, we learn: • whole numbers (integers) • decimals (floats) • arithmetic operators • rounding • mathematical shortcuts • order of operations • simple errors beginners often make --- 7.1 Types of Numbers Python uses two main number types: Integers (int) Whole numbers: Code: 5, 42, -10, 0Floats (float) Decimals: Code: 3.14, -2.5, 0.001Python chooses automatically based on what you write. --- 7.2 Basic Arithmetic Python can do all standard maths: Code: print(5 + 3) # additionDivision always returns a float. --- 7.3 Floor Division & Remainder Sometimes you need whole-number division. Floor division cuts off decimals: Code: print(17 // 5) # 3Modulo gives the remainder: Code: print(17 % 5) # 2Together, these answer questions like: • how many items fit in a box? • what’s left over? --- 7.4 Powers & Roots Exponent (power): Code: print(3 ** 4) # 81Square root: Code: print(25 ** 0.5)Or using math library: Code: import math--- 7.5 Order of Operations (BIDMAS) Python follows BIDMAS: Brackets Indices Division Multiplication Addition Subtraction Example: Code: print(4 + 2 * 10)Result: Code: 24Because multiplication happens before addition. Force your own order with brackets: Code: print((4 + 2) * 10)Now result: Code: 60--- 7.6 Rounding Numbers Use `round()`: Code: print(round(3.14159)) # 3Second argument = how many decimal places. --- 7.7 Converting Between Types User input always arrives as a string. Convert to a number using: Code: age = int(input("Enter your age: "))Convert number → string with: Code: str(number)--- 7.8 Avoiding Common Mistakes ❌ ERROR: Code: print("Age: " + 18)✔ FIX: Code: print("Age: " + str(18))❌ ERROR: Forgetting brackets in BIDMAS. ✔ FIX: Use clear grouping: Code: total = (price * quantity) + tax--- 7.9 Mini Project — Simple Calculator Create a file called calculator.py. Ask the user for two numbers. Then print: • sum • difference • product • division Example: Code: Enter first number: 12--- 7.10 Challenge — The Taxi Meter Write a program that calculates a taxi cost using: • base fare: 3.00 • £1.40 per km • values rounded to 2 decimals User enters: • distance travelled Output: "Your total fare is: £X.XX" --- 7.11 Chapter Summary • int = whole number • float = decimal • + - * / are standard operations • // gives whole-number division • % gives remainder • ** powers a number • round() controls decimal places • input() always gives a string — convert it! • Use brackets to avoid errors Next: Chapter 8 — Variables & Naming Rules You’ll learn how professionals name and structure the moving parts of their programs. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |