Thread Closed 
Thread Rating:
CHAPTER 7 — Numbers & Maths in Python
#1
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, 0

Floats (float) 
Decimals: 
Code:
3.14, -2.5, 0.001

Python chooses automatically based on what you write.

---

7.2 Basic Arithmetic

Python can do all standard maths:

Code:
print(5 + 3)  # addition
print(9 - 2)  # subtraction
print(4 * 7)  # multiplication
print(20 / 5)  # division

Division always returns a float.

---

7.3 Floor Division & Remainder

Sometimes you need whole-number division.

Floor division cuts off decimals:

Code:
print(17 // 5)  # 3

Modulo gives the remainder:

Code:
print(17 % 5)    # 2

Together, 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)  # 81

Square root:

Code:
print(25 ** 0.5)

Or using math library:

Code:
import math
print(math.sqrt(25))

---

7.5 Order of Operations (BIDMAS)

Python follows BIDMAS:

Brackets 
Indices 
Division 
Multiplication 
Addition 
Subtraction 

Example:

Code:
print(4 + 2 * 10)

Result:
Code:
24

Because 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))        # 3
print(round(3.14159, 2))    # 3.14

Second 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: "))
height = float(input("Enter your height: "))

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
Enter second number: 5

Sum: 17
Difference: 7
Product: 60
Division: 2.4

---

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
« Next Oldest | Next Newest »
Thread Closed 


Forum Jump:


Users browsing this thread: 1 Guest(s)