Lesson 06 - Numbers (Integers and Float)
Python uses integers and floats to handle numbers. Learn the difference and see how it affects real architectural calculations like areas, heights, and budgets.
Introduction
Architects work with numbers constantly. Floor counts, room areas, building heights, budget calculations.
Python handles numbers in two ways: integers (whole numbers) and floats (decimal numbers).
Understanding the difference matters. It affects how your calculations work and what results you get.
This lesson shows you how Python treats numbers and how to use them in real architectural calculations.
Lesson Overview
This section contains a general overview of topics you will learn in this lesson.
- Understand the difference between integers and floats
- Learn when to use each type
- Perform basic math operations (+, -, *, /)
- Understand why division behaves differently than you expect
- See how number types affect real calculations
Two Types of Numbers
Python treats whole numbers and decimal numbers as different types.
Integers (int) — Whole numbers, no decimal point
floor_count = 12
window_count = 48
door_count = 24
Floats (float) — Numbers with decimal points
floor_height = 3.2
room_area = 42.5
wall_thickness = 0.2
When to Use Each Type
Use Integers When Counting Things
Things you count are always whole numbers. You can't have 12.5 floors or 3.7 doors.
# Counting
floor_count = 8
room_count = 45
column_count = 16
# IDs and numbers
sheet_number = 101
revision_number = 3
Use Floats When Measuring Things
Measurements are rarely exact whole numbers.
# Distances and heights
floor_height = 3.2
ceiling_height = 2.7
wall_length = 12.5
# Areas and volumes
room_area = 42.5
floor_area = 850.75
# Coordinates
x_coordinate = 125.3
y_coordinate = 78.9
Basic Math Operations
Python uses the operators you already know.
Addition (+)
ground_floor_height = 4.5
typical_floor_height = 3.2
total_height = ground_floor_height + typical_floor_height
print(total_height) # Output: 7.7
Subtraction (-)
total_area = 450.0
circulation_area = 75.5
usable_area = total_area - circulation_area
print(usable_area) # Output: 374.5
Multiplication (*)
floor_count = 8
floor_height = 3.2
total_height = floor_count * floor_height
print(total_height) # Output: 25.6
Division (/)
total_area = 450.0
floor_count = 3
area_per_floor = total_area / floor_count
print(area_per_floor) # Output: 150.0
The Division Surprise
Division in Python always returns a float, even when dividing whole numbers.
total_floors = 12
half_floors = total_floors / 2
print(half_floors) # Output: 6.0 (not 6)
print(type(half_floors)) # Output: <class 'float'>
Notice: 6.0 not 6. The result is a float.
This is intentional. Python assumes you want precision.
Integer Division (//)
If you want to divide and get an integer back, use // (floor division).
total_floors = 13
floors_per_section = total_floors // 3
print(floors_per_section) # Output: 4 (not 4.333...)
Floor division divides and rounds down to the nearest whole number.
When This Matters
# You have 100 seats and tables that fit 8 people
seats = 100
seats_per_table = 8
tables_needed = seats // seats_per_table
print(tables_needed) # Output: 12
# Regular division would give you 12.5 tables
# But you can't have half a table
Modulo (%) — The Remainder
The modulo operator % gives you the remainder after division.
seats = 100
seats_per_table = 8
tables_needed = seats // seats_per_table
leftover_seats = seats % seats_per_table
print(tables_needed) # Output: 12
print(leftover_seats) # Output: 4
You need 12 tables. 4 seats will be left over.
Architectural Use Case
# You're ordering tiles. Tiles come in boxes of 25.
tiles_needed = 378
tiles_per_box = 25
boxes_needed = tiles_needed // tiles_per_box
extra_tiles = tiles_needed % tiles_per_box
print(boxes_needed) # Output: 15 boxes
print(extra_tiles) # Output: 3 tiles short
# You need 16 boxes to have enough
Exponentiation (**)
Raise a number to a power using **.
# Area of a square
side_length = 5
area = side_length ** 2
print(area) # Output: 25
# Volume of a cube
side_length = 3
volume = side_length ** 3
print(volume) # Output: 27
Mixing Integers and Floats
When you mix integers and floats, Python converts the result to a float.
floor_count = 8 # Integer
floor_height = 3.2 # Float
total_height = floor_count * floor_height
print(total_height) # Output: 25.6 (float)
This is called type promotion. Python promotes the integer to a float to avoid losing precision.
Order of Operations (PEMDAS)
Python follows standard math rules.
Parentheses
Exponents
Multiplication and Division (left to right)
Addition and Subtraction (left to right)
# Without parentheses
result = 10 + 5 * 2
print(result) # Output: 20 (multiplication first)
# With parentheses
result = (10 + 5) * 2
print(result) # Output: 30 (parentheses first)
Architectural Example
# Calculate total building height
ground_floor_height = 4.5
typical_floor_height = 3.2
floor_count = 8
# Wrong (without parentheses)
total_height = ground_floor_height + floor_count * typical_floor_height
print(total_height) # Output: 30.1
# This calculates: 4.5 + (8 * 3.2) = 4.5 + 25.6
# Correct (with parentheses)
typical_floors = floor_count - 1
total_height = ground_floor_height + (typical_floors * typical_floor_height)
print(total_height) # Output: 27.9
# This calculates: 4.5 + (7 * 3.2) = 4.5 + 22.4
When in doubt, use parentheses. They make your intent clear.
Real Architectural Calculations
Example 1: Building Height
ground_floor_height = 4.5
typical_floor_height = 3.2
floor_count = 12
# Ground floor + typical floors
typical_floors = floor_count - 1
total_height = ground_floor_height + (typical_floors * typical_floor_height)
print("Building height:", total_height, "meters")
# Output: Building height: 39.7 meters
Example 2: Floor Area Ratio (FAR)
site_area = 2000.0
gross_floor_area = 8500.0
far = gross_floor_area / site_area
print("Floor Area Ratio:", far)
# Output: Floor Area Ratio: 4.25
Example 3: Budget Per Square Meter
total_budget = 5000000
total_area = 3500.0
cost_per_sqm = total_budget / total_area
print("Cost per sqm:", cost_per_sqm)
# Output: Cost per sqm: 1428.5714285714287
Notice: The result has many decimal places. We'll learn to format numbers properly in a later lesson.
The Pain vs The Python Fix
The Pain: Calculating building metrics by hand. Every time a dimension changes, you recalculate everything.
The Python Fix: Define the variables once. Change one value, everything updates.
# Define once
floor_count = 12
floor_height = 3.2
ground_floor_height = 4.5
# Calculate automatically
typical_floors_height = (floor_count - 1) * floor_height
total_height = ground_floor_height + typical_floors_height
average_height = total_height / floor_count
print("Total height:", total_height)
print("Average height:", average_height)
# Change floor count
floor_count = 15
# Recalculate automatically
typical_floors_height = (floor_count - 1) * floor_height
total_height = ground_floor_height + typical_floors_height
average_height = total_height / floor_count
print("New total height:", total_height)
print("New average height:", average_height)
Common Mistakes
Mistake 1: Expecting integer division to give an integer
total_floors = 12
result = total_floors / 2
print(result) # Output: 6.0 (float, not 6)
Use // if you want an integer.
total_floors = 12
result = total_floors // 2
print(result) # Output: 6 (integer)
Mistake 2: Dividing by zero
area = 450.0
floor_count = 0
area_per_floor = area / floor_count
# Error: ZeroDivisionError: division by zero
Python can't divide by zero. Check your values before dividing.
Mistake 3: Forgetting order of operations
# You want: 4.5 + (7 * 3.2)
# You write:
total = 4.5 + 7 * 3.2
print(total) # Output: 26.9 (correct, but unclear)
# Better (explicit):
total = 4.5 + (7 * 3.2)
print(total) # Output: 26.9 (clear intent)
Mistake 4: Confusing * (exponent) with ^ (not exponent in Python)
# Wrong (^ is not exponent in Python)
area = 5 ^ 2 # This does something else entirely
# Correct
area = 5 ** 2
print(area) # Output: 25
Assignment
- Create a new file called
building_calculations.py - Define these variables:
- Ground floor height (4.5 meters)
- Typical floor height (3.2 meters)
- Number of floors (10)
- Site area (1500 square meters)
- Floor area per level (450 square meters)
- Calculate and print:
- Total building height
- Total gross floor area
- Floor Area Ratio (FAR)
- Average floor height
- Change the number of floors to 15 and run the script again. Verify all calculations update.
- Experiment:
- Try dividing by zero. Read the error.
- Calculate the number of full floors in 32 meters with 3.2m floor height (use
//) - Find how many meters are left over (use
%) - Calculate the area of a square room with 4.5m sides (use
*)
- Create a budget calculation:
- Total budget: 4,000,000
- Total area: 2,800 square meters
- Calculate cost per square meter
- Calculate 15% contingency on the budget
Knowledge Check
The following questions are an opportunity to reflect on key topics in this lesson.
- What's the difference between an integer and a float?
- When should you use integers vs floats?
- What type does regular division
/always return? - What does floor division
//do? - What does the modulo operator
%return? - How do you calculate 5 squared in Python?
- What happens when you multiply an integer by a float?
- Why should you use parentheses in complex calculations?
Additional Resources
This section contains helpful links to related content. It isn't required, so consider it supplemental.
- Python's official documentation on numeric types covers integers and floats in detail
- Real Python's guide to operators explains all Python operators with examples
- Python's math module provides additional mathematical functions (we'll cover this later)