flowchart LR
P["Your Python Code"] -->|"interpreted"| M["Python Engine<br/>(runs your code)"]
P -->|"calls"| C["pandas / NumPy<br/>(pre-built libraries)"]
C -->|"already compiled,<br/>runs fast"| M
A Practical Overview
The new reality
What this session gives you
Goal today: Build enough vocabulary to read and understand Python — not to write it from memory.
# profit_margins.py
products = ["Widget", "Gadget", "Doohickey"]
revenues = [120_000, 85_000, 200_000]
costs = [ 80_000, 60_000, 140_000]
for i in range(len(products)):
margin = (revenues[i] - costs[i]) / revenues[i]
print(f"{products[i]}: {margin:.1%} margin")Running this prints the profit margin for each product. By the end of this session you’ll be able to name every pattern in that code.
flowchart LR
P["Your Python Code"] -->|"interpreted"| M["Python Engine<br/>(runs your code)"]
P -->|"calls"| C["pandas / NumPy<br/>(pre-built libraries)"]
C -->|"already compiled,<br/>runs fast"| M
Two paths to execution
Script — a .py file, runs top to bottom
# profit_margins.py
products = ["Widget", "Gadget", "Doohickey"]
revenues = [120_000, 85_000, 200_000]
costs = [ 80_000, 60_000, 140_000]
for i in range(len(products)):
margin = (revenues[i] - costs[i]) / revenues[i]
print(f"{products[i]}: {margin:.1%} margin")Run from terminal: python profit_margins.py
Same code — different containers. Notebooks let you run one piece at a time and inspect the result.
A library is a collection of pre-built code you can import and use.
Key libraries for business
| Library | What it does |
|---|---|
pandas |
Data tables — read, clean, reshape, summarize |
numpy |
Fast math on arrays of numbers |
matplotlib |
Charts and plots |
seaborn |
Statistical visualization |
Hundreds more exist — for machine learning (scikit-learn), optimization (scipy), web APIs, and more. import pandas as pd means: load pandas and call it pd for short.
region product revenue units
0 East Widget 42000 210
1 West Gadget 31500 180
2 North Widget 28000 140
3 East Doohickey 19500 95
4 South Gadget 22000 125
df is a DataFrame — think of it as a spreadsheet in Python. df.head() shows the first 5 rows.
One line that groups, sums, and plots:
Reading left to right
| Step | Code | Returns |
|---|---|---|
| 1 | df.groupby("region") |
grouped data object |
| 2 | ["revenue"] |
revenue values per group |
| 3 | .sum() |
total revenue per region |
| 4 | .plot(kind="bar") |
bar chart |
Each method returns an object; the next method acts on that object. Under the hood, pandas runs these operations as precompiled C code — which is why it handles millions of rows without slowing down. This is the precompiled path from the earlier diagram.
| # | Pattern | One-liner example |
|---|---|---|
| 1 | Objects & Types | 42 · 3.14 · "hello" · True |
| 2 | Variables | price = 182.50 |
| 3 | Collections | ["AAPL", "GOOGL"] · {"ticker": "AAPL"} |
| 4 | Conditionals | if revenue > target: ... |
| 5 | Loops | for company in portfolio: ... |
| 6 | Functions & Methods | len(items) · text.upper() |
You’ll also encounter
[x * 2 for x in prices] — this is Pattern 5 written in shorthandtry: ... except: ... — guards code that might fail (bad data, missing file)| Pattern | What to look for |
|---|---|
| Objects & Types | Literal values: 42, 3.14, "text", True / False |
| Variables | name = value — a single = sign |
| Collections | [...] = list (ordered) · {key: value, ...} = dictionary |
| Conditionals | if / elif / else followed by : and an indented block |
| Loops | for ... in ...: or while ...: followed by an indented block |
| Functions & Methods | name(...) = function · object.method(...) = method |
| List comprehension | [expr for item in collection] — compact loop inside brackets |
| try / except | try: + indented block, then except: + error handler |
Patterns
name = value0.25 is a floatif / else with indented blocksprint(...) called twicePatterns
[...]total, value, ifor i in range(...):len(...), range(...), print(...)total += value inside loopPatterns
[{...}, ...]for loop in bracketsvalues, totalsum(...), print(...)Patterns
def keyword, parameters, returnmargin, rating0.30, 0.15; stringsif / elif / else inside the functionassess_margin(500_000, 320_000)Patterns
for company in companies:clean, has_suffix.title() on a string objectif has_suffix:any(...), print(...)Patterns
import pandas as pddf, tech, summary, topdf[df["sector"] == "Technology"].groupby(...)[...].sum().idxmax(), .read_csv()print(...)Go to this link in your browser
colab.research.google.com/github/kerryback/workshop_python/blob/main/python_overview_exercises.ipynb
How to get code from Gemini
For each prompt on the following slides: type it into Gemini, run the code, then use the “How to Spot Each Pattern” table to identify what’s in the result.
Type this into Gemini
“Write a Python function that calculates the future value of an investment given a starting amount, annual return rate, and number of years. Then call the function to show how $10,000 grows at 7% over 10, 20, and 30 years. Print the results in a formatted table.”
After Gemini generates the code, look for:
def block — the function definition (Pattern ⑥)** — exponentiation for compounding math (Pattern ①)print() with f-string formatting (Pattern ⑥)Type this into Gemini
“Write Python code that defines a list of 12 monthly sales figures. Calculate the best month, worst month, and monthly average. Also count how many months exceeded the average. Print a formatted summary report.”
After Gemini generates the code, look for:
max(), min(), sum(), len() (Pattern ⑥)Type this into Gemini
“Write Python code with a list of 6 employees. Each employee should have a name, department, and salary. Loop through the list and print the name and salary of anyone earning more than $80,000. At the end, print how many employees qualified.”
After Gemini generates the code, look for:
for loop iterating over the list (Pattern ⑤)if checking the salary (Pattern ④)Type this into Gemini
“Write Python code with a list of 4 large companies, each with a name, annual revenue, and annual costs. Write a function to calculate profit margin. Sort the companies from highest to lowest margin and print a numbered ranking with each company’s margin formatted as a percentage.”
After Gemini generates the code, look for:
def function with a return statement (Pattern ⑥)sorted() or .sort() with a key= argument (Pattern ⑥)enumerate() for the numbering (Pattern ⑤)Type this into Gemini
“Write Python code using pandas that creates a DataFrame with columns for region, product, revenue, and units (at least 8 rows of made-up data). Then calculate total revenue and average revenue per unit for each region. Show the results sorted by total revenue, highest first.”
This is the synthesis prompt. In roughly 15 lines, you should find all 6 patterns:
import pandas as pd # library import
data = {...} # ③ dictionary → collection
df = pd.DataFrame(data) # ⑥ function call
df.groupby(...).agg(...).sort_values(...) # ⑥ method chainCan you name the pattern for every line Gemini produces?
A complete analysis in a single method chain:
import pandas as pd
df = pd.read_csv("sales.csv")
(df
.query("revenue > 10_000") # filter rows
.groupby("region")["revenue"] # group by region
.sum() # aggregate
.sort_values(ascending=False) # sort high to low
.plot(kind="bar", title="Revenue by Region") # chart
)Every step is Pattern ⑥ — Methods: each returns an object, and the next method acts on it. This is the method chaining pattern you’ll see throughout AI-generated pandas code.
A taste of what else pandas can do:
# Combine two tables on a shared key (like VLOOKUP)
df = pd.merge(prices, company_info, on="ticker")
# Reshape: rows → columns (like a pivot table)
df.pivot_table(values="revenue", index="quarter", columns="region")
# Find missing data
df.isnull().sum()
# Summary statistics in one call
df.describe()Each of these is a method call (Pattern ⑥). When AI generates code for data tasks, it will use pandas. Recognizing the method-chain pattern tells you what each line is doing — even if you don’t know the specific method name.
The same method-chaining pattern — different kind=:
# Distribution of one variable
df["revenue"].plot(kind="hist", bins=20)
# Comparison across categories
df.groupby("region")["revenue"].sum().plot(kind="bar")
# Relationship between two variables
df.plot(kind="scatter", x="units", y="revenue")
# Trend over time
df.set_index("date")["revenue"].plot(kind="line")Different charts, same pattern: build a series or DataFrame, call .plot(kind=...). When you see a visualization in AI-generated code, look for this pattern — then look at what kind= is set to.
products = ["Widget", "Gadget", "Doohickey"] # ③ List — a collection
revenues = [120_000, 85_000, 200_000] # ③ List — a collection
costs = [ 80_000, 60_000, 140_000] # ③ List — a collection
for i in range(len(products)): # ⑤ Loop + ⑥ Functions
margin = (revenues[i] - costs[i]) / revenues[i] # ② Variables + ① Math
print(f"{products[i]}: {margin:.1%} margin") # ⑥ Function + f-stringThis is the same code from slide 2. Every line is one of the 6 patterns. You now have the vocabulary to read it — and to read the code AI generates for you.
How Python works
The 6 patterns
Your workflow
You don’t need to memorize syntax. You need to recognize patterns — and now you can.