// Basics
Numbers — The Full Picture
The idea!
You've covered a lot. int, float, arithmetic, the math module, two real projects.
This is your reference. Everything in one place.
Come back here whenever you need a reminder.
Number Types
age = 25 # int — whole number
height = 1.80 # float — decimal number
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
Python assigns the type automatically. No quotes — it's a number.
Arithmetic Operators
print(10 + 3) # 13 — addition
print(10 - 3) # 7 — subtraction
print(10 * 3) # 30 — multiplication
print(10 / 3) # 3.333... — division (always float)
print(10 // 3) # 3 — floor division (int)
print(10 % 3) # 1 — modulo (remainder)
print(10 ** 3) # 1000 — exponentiation
int vs float
print(10 / 2) # 5.0 — division always returns float
print(10 // 2) # 5 — floor division returns int
print(int(3.9)) # 3 — truncates, does not round
print(float(5)) # 5.0
print(5 + 2.0) # 7.0 — mixing returns float
print(0.1 + 0.2) # 0.30000000000000004 — float precision
Conversion
print(int(3.9)) # 3 — truncates
print(float(5)) # 5.0
print(str(25)) # "25" — number to string
print(int("25")) # 25 — string to number
Rounding
print(round(3.14159, 2)) # 3.14
print(round(3.5)) # 4
print(round(2.5)) # 2 — banker's rounding
The math module
import math
print(math.pi) # 3.141592653589793
print(math.sqrt(16)) # 4.0
print(math.pow(2, 8)) # 256.0
print(math.floor(3.9)) # 3
print(math.ceil(3.1)) # 4
Input with numbers
# input() always returns string — convert before math
age = int(input("Enter your age: "))
height = float(input("Enter your height: "))
print(f"Age: {age} | Height: {height}")
Common Mistakes
- Division
/always returns float — use//for int int()truncates — useround()for rounding- Float precision is a known quirk — not a bug
- Input always returns string — convert before math
- Division by zero crashes — always
- Operator precedence applies — use parentheses when in doubt
What you should understand now
- int and float are different types — Python assigns them automatically
- Division always returns float
- The math module gives you sqrt, floor, ceil, pi, pow and more
- Always convert input() before doing math
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment