Section 9: Python Fundamentals

High-Level Overview

This section provides a strategic overview of Python. For comprehensive Python learning with hands-on exercises, detailed syntax, and practical applications, we strongly recommend our dedicated Intro to Python course.

This section covers: Why Python matters, development environments, basic concepts, and essential libraries.

Learning Objectives

By the end of this section, students will be able to:

  • Understand why Python dominates data science and real estate analytics
  • Recognize Python’s object-oriented programming approach
  • Think procedurally like a Python developer
  • Set up Python development environments for real estate work
  • Write basic Python syntax for property data analysis
  • Identify key Python libraries for real estate analytics

Introduction

Python has become the dominant language for data science and real estate analytics, not by accident but by design. While other languages excel in specific domains, Python’s strength lies in its ecosystem—the vast collection of specialized libraries that transform it from a simple programming language into a comprehensive analytical platform.

Why has Python captured the data science market? The answer lies in its readability and versatility. Python code reads almost like English, making it accessible to analysts who think in business terms rather than computer science concepts. A real estate analyst can write property_price = square_feet * price_per_sqft and immediately understand what the code does, even without programming experience.

Why Python Dominates Real Estate Analytics

The Ecosystem Advantage

Python’s strength comes from its extensive library ecosystem. Unlike languages designed for specific purposes, Python serves as a platform where specialized tools integrate seamlessly. For real estate analytics, this means:

  • pandas for data manipulation (Excel on steroids)
  • numpy for numerical computing (mathematical operations)
  • matplotlib/seaborn for visualization (professional charts)
  • scikit-learn for machine learning (predictive models)
  • geopandas for spatial analysis (mapping and geography)

Consider the alternative: Excel requires manual data manipulation, R has steep learning curves, and specialized tools like SAS cost thousands per user. Python provides all these capabilities in one free, integrated environment.

Object-Oriented Programming (OOP) Foundation

Python is built on object-oriented programming, which mirrors how we think about real estate. Every property is an object with attributes (square footage, bedrooms, price) and behaviors (calculate cap rate, update valuation). This mental model translates directly to code:

class Property:
    def __init__(self, address, sqft, bedrooms, price):
        self.address = address
        self.square_feet = sqft
        self.bedrooms = bedrooms
        self.price = price
    
    def price_per_sqft(self):
        return self.price / self.square_feet
    
    def cap_rate(self, noi):
        return noi / self.price

This isn’t just programming—it’s modeling real estate concepts in code. When you understand OOP, you understand how to structure data and calculations that mirror business logic.

Thinking Like a Python Developer

Procedural vs. Functional Thinking

Python developers think in sequences of operations rather than simultaneous calculations. Unlike Excel where formulas calculate across cells simultaneously, Python executes instructions one line at a time:

Excel approach: All formulas calculate when you press Enter Python approach: Instructions execute sequentially, building results step by step

This mental shift enables complex workflows that Excel cannot handle. Consider monthly portfolio reporting: Python can automate data collection, cleaning, analysis, visualization, and report generation in a single script that runs identically every month.

The Python Philosophy

Python follows the Zen of Python—a set of principles that emphasize:

  • Readability counts - Code should be self-documenting
  • Simple is better than complex - Choose clarity over cleverness
  • There should be one obvious way to do it - Consistency reduces confusion

These principles make Python accessible to real estate professionals who need to solve business problems, not demonstrate programming prowess.

Python Development Environments

Google Colab (Cloud Alternative)

Google Colab offers a zero-setup Python environment that runs entirely in your browser. This cloud-based platform eliminates the need for local software installation while providing access to powerful computing resources. Colab comes pre-configured with popular data science libraries and offers free GPU access for machine learning applications. The platform integrates seamlessly with Google Drive for data storage and enables instant sharing of notebooks with colleagues.

Setup: Visit colab.research.google.com and start coding immediately. No installation required.

  • Browser-based - No software installation required
  • Pre-installed libraries - pandas, numpy, matplotlib ready to use
  • Free GPU access - For machine learning applications
  • Easy sharing - Share notebooks with colleagues instantly
  • Integration - Works with Google Drive for data storage

Jupyter Notebooks (Interactive Analysis)

Jupyter enables interactive data exploration through its cell-based execution model. This web-based application allows you to run code in small, manageable chunks while displaying rich output including charts, tables, and formatted text. The notebook format combines executable code with markdown documentation, making it ideal for exploratory analysis and sharing results. Jupyter’s real-time feedback system lets you see results immediately as you develop and test your code.

Setup: Install via pip install jupyter or use Anaconda distribution from anaconda.com. Launch with jupyter notebook command.

  • Cell-based execution - Run code in small chunks
  • Rich output - Display charts, tables, and text together
  • Documentation integration - Mix code with explanations
  • Real-time feedback - See results immediately

Python Syntax Fundamentals

Variables and Data Types

Python uses dynamic typing—variables automatically determine their type:

# Numbers
property_price = 750000
square_feet = 2400.5
bedrooms = 3

# Text
address = "123 Main Street, Irvine, CA"
property_type = "Single Family"

# Boolean
has_pool = True
is_rental = False

# Lists (collections)
property_features = ["pool", "garage", "fireplace"]
price_history = [700000, 720000, 750000]

Control Flow

Conditional statements filter properties based on criteria:

if property_price > 500000:
    print("High-end property")
elif property_price > 300000:
    print("Mid-range property")
else:
    print("Affordable property")

# Multiple conditions
if bedrooms >= 3 and square_feet > 2000:
    print("Family-sized property")

Loops process multiple properties:

# Process each property in a list
for price in price_history:
    print(f"Historical price: ${price:,}")

# List comprehension (Pythonic way)
high_value_properties = [p for p in properties if p.price > 500000]

Functions

Functions package reusable calculations:

def calculate_cap_rate(noi, purchase_price):
    """Calculate capitalization rate for a property."""
    return (noi / purchase_price) * 100

def price_per_sqft(price, sqft):
    """Calculate price per square foot."""
    return price / sqft

# Usage
cap_rate = calculate_cap_rate(50000, 750000)  # 6.67%
psf = price_per_sqft(750000, 2400)  # $312.50

Data Import and Interaction

Loading Data from Files

Python excels at importing data from various sources. The pandas library provides simple functions for reading common file formats:

import pandas as pd

# Load CSV files
properties_df = pd.read_csv('properties.csv')
market_data = pd.read_csv('market_trends.csv')

# Load Excel files
portfolio_data = pd.read_excel('portfolio_analysis.xlsx', sheet_name='Properties')
financials = pd.read_excel('financial_data.xlsx', sheet_name='Q1_2024')

# Load from specific columns
selected_data = pd.read_csv('large_dataset.csv', usecols=['address', 'price', 'sqft'])

Basic Data Exploration

Once data is loaded, you can quickly explore its structure and content:

# View basic information
print(properties_df.shape)  # (rows, columns)
print(properties_df.columns)  # Column names
print(properties_df.dtypes)  # Data types

# Preview data
properties_df.head()  # First 5 rows
properties_df.tail()  # Last 5 rows
properties_df.sample(10)  # Random 10 rows

# Statistical summary
properties_df.describe()  # Numeric columns summary
properties_df.info()  # Memory usage and data types

Data Selection and Filtering

Python provides intuitive methods for selecting and filtering data:

# Select specific columns
price_data = properties_df[['address', 'price', 'square_feet']]

# Filter rows based on conditions
expensive_properties = properties_df[properties_df['price'] > 500000]
three_bedrooms = properties_df[properties_df['bedrooms'] == 3]

# Multiple conditions
high_value_3br = properties_df[
    (properties_df['price'] > 500000) & 
    (properties_df['bedrooms'] == 3)
]

# Filter by text patterns
irvine_properties = properties_df[
    properties_df['city'].str.contains('Irvine', case=False)
]

Data Manipulation

Python makes it easy to create new columns and modify existing data:

# Create new calculated columns
properties_df['price_per_sqft'] = properties_df['price'] / properties_df['square_feet']
properties_df['property_size'] = properties_df['square_feet'].apply(
    lambda x: 'Large' if x > 3000 else 'Medium' if x > 2000 else 'Small'
)

# Group and aggregate data
avg_price_by_city = properties_df.groupby('city')['price'].mean()
price_stats = properties_df.groupby('bedrooms')['price'].agg(['mean', 'min', 'max', 'count'])

# Sort data
sorted_by_price = properties_df.sort_values('price', ascending=False)
sorted_by_sqft = properties_df.sort_values(['city', 'price'], ascending=[True, False])

Saving Data

Export your processed data back to files:

# Save to CSV
filtered_data.to_csv('filtered_properties.csv', index=False)

# Save to Excel with multiple sheets
with pd.ExcelWriter('analysis_results.xlsx') as writer:
    properties_df.to_excel(writer, sheet_name='All_Properties', index=False)
    expensive_properties.to_excel(writer, sheet_name='High_Value', index=False)
    avg_price_by_city.to_excel(writer, sheet_name='City_Averages', index=False)

Essential Python Libraries for Real Estate

pandas - Data Manipulation

pandas transforms Python into a comprehensive data analysis platform:

import pandas as pd

# Load property data
df = pd.read_csv('properties.csv')

# Basic operations
df.head()  # First 5 rows
df.describe()  # Statistical summary
df.groupby('neighborhood')['price'].mean()  # Average price by area

# Filtering
expensive_properties = df[df['price'] > 500000]

matplotlib/seaborn - Visualization

matplotlib creates professional-quality charts:

import matplotlib.pyplot as plt
import seaborn as sns

# Price distribution
plt.hist(df['price'], bins=50)
plt.title('Property Price Distribution')
plt.xlabel('Price ($)')
plt.ylabel('Frequency')
plt.show()

# Price vs Square Footage
plt.scatter(df['square_feet'], df['price'])
plt.xlabel('Square Feet')
plt.ylabel('Price ($)')
plt.show()

scikit-learn - Machine Learning

scikit-learn enables machine learning modeling:

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

# Prepare data
X = df[['square_feet', 'bedrooms', 'bathrooms']]
y = df['price']

# Build model
model = LinearRegression()
model.fit(X, y)

# Make predictions
predictions = model.predict(X)

Real Estate Analytics Workflow

Typical Python Workflow

  1. Data Collection - Load from CSV, database, or API
  2. Data Cleaning - Handle missing values, outliers, inconsistencies
  3. Exploratory Analysis - Visualize patterns, calculate statistics
  4. Feature Engineering - Create new variables, transformations
  5. Modeling - Build predictive models, forecasts
  6. Visualization - Create charts, dashboards, reports
  7. Automation - Schedule scripts, generate reports

This workflow scales from analyzing a single property to managing entire portfolios with thousands of assets.


© 2025 Prof. Tim Frenzel. All rights reserved. | Version 1.0.5