What’s one thing you learned? What’s still confusing?
Calling APIs in Python: requests, JSON & Authentication
Master the requests library — GET/POST, JSON parsing, API auth, error handling.
The collections Module
Counter, defaultdict, OrderedDict, namedtuple, deque, plus heapq and bisect.
Regular Expressions: Pattern Matching Mastery
Regex patterns, groups, lookahead/lookbehind, and real-world text processing.
Interactive Labs for This Track
Loop Visualizer
You're a factory robot repeating the same task on an assembly line — watch how loops automate repetitive work
List Slicing
You have a playlist of 50 songs — grab just tracks 10 through 20 with a single slice expression
Sorting Algorithms
You're organizing a library of 10,000 books — which sorting method is fastest?
Ask questions, share insights
OPENAI_API_KEY in a public GitHub repo can drain $10,000 in hours — bots scrape new commits in seconds. Every production Python service at Stripe, Anthropic, and OpenAI reads secrets from environment variables (or a secrets manager), never from source code. The 12-factor app rule "store config in the environment" is non-negotiable.import os
# Method 1: os.environ["KEY"] — raises KeyError if missing
api_key = os.environ["ANTHROPIC_API_KEY"]
# Method 2: os.environ.get("KEY") — returns None if missing (safer)
api_key = os.environ.get("ANTHROPIC_API_KEY")
if api_key is None:
raise ValueError("ANTHROPIC_API_KEY not set. Check your .env file.")
# Method 3: os.environ.get("KEY", "default") — returns default if missing
debug = os.environ.get("DEBUG", "false")
port = int(os.environ.get("PORT", "8000")).get() with a clear error message. Crashing with KeyError: 'ANTHROPIC_API_KEY' tells the user nothing. Crashing with ValueError("ANTHROPIC_API_KEY not set. Check your .env file.") tells them exactly how to fix it.# Temporary — only for this terminal session
export ANTHROPIC_API_KEY=sk-your-key-here
# Verify it's set
echo $ANTHROPIC_API_KEY
# Now run your Python script — it will see the variable
python my_script.py
set ANTHROPIC_API_KEY=sk-your-key-here
python my_script.py
$env:ANTHROPIC_API_KEY = "sk-your-key-here"
python my_script.py
.env file lets you put all secrets in one place for your project.pip install python-dotenv
.env file in your project root# .env — NEVER commit this to Git
ANTHROPIC_API_KEY=sk-ant-your-key-here
OPENAI_API_KEY=sk-your-openai-key
DATABASE_URL=postgresql://user:password@localhost/mydb
DEBUG=true
from dotenv import load_dotenv
import os
# Load variables from .env into os.environ
load_dotenv()
# Now read them normally
api_key = os.environ.get("ANTHROPIC_API_KEY")
db_url = os.environ.get("DATABASE_URL")load_dotenv() reads the .env file and loads every variable into os.environ. Your code works the same way whether variables come from a .env file or the system environment..env to your .gitignore so you never accidentally commit it:# .gitignore
.env
.env.local
.env.production
__pycache__/
*.pyc
.venv/
.env.example instead# .env.example — commit this, it shows what variables are needed
ANTHROPIC_API_KEY=your-key-here
DATABASE_URL=postgresql://user:password@host/dbname
DEBUG=false
.env.example to .env, fill in their own keys, and everything works.my-ai-project/
├── .env ← secrets (never commit)
├── .env.example ← template (commit this)
├── .gitignore ← includes .env
├── main.py
├── requirements.txt
└── README.md
# main.py
from dotenv import load_dotenv
import os
import anthropic
def main():
load_dotenv() # Load .env if it exists, silently skip if not
api_key = os.environ.get("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError(
"ANTHROPIC_API_KEY not set!\n"
"1. Copy .env.example to .env\n"
"2. Add your API key\n"
"3. Run again"
)
client = anthropic.Anthropic(api_key=api_key)
# ...
if __name__ == "__main__":
main()For larger projects, use Pydantic to validate all config at startup:
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
anthropic_api_key: str
database_url: str
debug: bool = False
port: int = 8000
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
# Raises a clear error if any required variable is missing
settings = Settings()
print(settings.port) # 8000port becomes an int, debug becomes a bool, missing required fields raise a clean error with the variable name.Why use os.environ.get('KEY') instead of os.environ['KEY']?