6. Numbers and Basic Operations
Level: BeginnerDuration: 26m
Numbers in Python
Python works with different types of numbers. The most common ones are integers and floats.
| Type | Description | Example |
|---|---|---|
| int | Whole numbers | 5, 100, -42 |
| float | Decimal numbers | 3.14, -0.7, 2.0 |
Basic Arithmetic Operators
Python can perform basic math easily using operators.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 3 → 8 |
| - | Subtraction | 10 - 4 → 6 |
| * | Multiplication | 6 * 7 → 42 |
| / | Division (float result) | 15 / 2 → 7.5 |
| // | Floor Division | 15 // 2 → 7 |
| % | Modulus (remainder) | 10 % 3 → 1 |
| ** | Exponent | 3 ** 2 → 9 |
Arithmetic in Action
python
x = 10
y = 3
print(x + y) # 13
print(x / y) # 3.333...
print(x // y) # 3
print(x ** y) # 1000Order of Operations
Python follows the standard math order: Parentheses → Exponents → Multiplication/Division → Addition/Subtraction (PEMDAS).
python
result = 2 + 3 * 4
print(result) # 14
result = (2 + 3) * 4
print(result) # 20Working with Floats
Floating-point numbers may not always be exact due to precision limits.
python
print(0.1 + 0.2) # 0.30000000000000004💡 This is normal in programming—computers store decimals in binary, which can cause tiny rounding errors.
Useful Math Functions
Python includes basic math utilities without any import.
| Function | Description | Example |
|---|---|---|
| round() | Round number | round(3.8) → 4 |
| abs() | Absolute value | abs(-7) → 7 |
| pow() | Power function | pow(2,3) → 8 |
| max() | Largest value | max(3,8,2) → 8 |
| min() | Smallest value | min(3,8,2) → 2 |
Using the math module
python
import math
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793Mini Project Step
In your bank_account.py file, add simple deposit and withdrawal calculations using + and -. Also display account balance using formatted output like: print(f"Your balance is {balance} Naira").