Variables
Lesson opener
In one line: A variable is a name that holds a value you will use again — assign it, read it, then overwrite it when the number changes.
Why it matters: The analyst who types 35.17 in five places will miss one of them when spend updates. The analyst who stores monthly_spend = 35.17 updates one name and every later line follows. That is the whole job of this lesson: put customer facts in named boxes so a script can reuse them without retyping.
Before this lesson: Intro to Python — you can run a cell, import pandas, and load data/customers.csv.
Practitioner focus: Every number printed here comes from the real 300-row
data/customers.csv(or from a one-line calculation on a value taken from that file). Run each block from the lesson folder sopd.read_csv("data/customers.csv")resolves. You do not need to declare types. You do need names that another person can read next week.
What you will learn
By the end of this lesson you will be able to:
- Assign a customer field to a name and print it.
- Choose
snake_casenames that describe the value (monthly_spend, notx). - Unpack several values from one row in a single line.
- Reassign a name when the business number changes, and show the new value.
- Mark a fixed business limit with an ALL_CAPS constant (a convention, not a language rule).
- Load one real customer from
data/customers.csvinto named variables and print a one-line summary.
What a variable is
A variable is a sticky note on a value. The note is the name. The value is whatever you put on the right of =. Python does not ask you to write int or str first — the value decides the type.
Use a variable when you will mention the same fact more than once: a customer id, a spend figure, a ticket count, a cap from the SLA. Do not invent a name for a value you type once and never read again.
The file this lesson uses is the same 300-customer table as BA 2. First row is customer C0001, segment Silver, monthly spend ₹35.17, two support tickets.
Setup. Python 3.10 or newer, plus the pinned library used to read the CSV:
pip install pandas==2.3.3If a block dies with ModuleNotFoundError: No module named 'pandas', the install step is missing.
Worked example: name the first customer
Step 1 — Load the file and bind four names. The first row is one customer. Pull the fields you will reuse into names, then print them so you can see the boxes are filled.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.iloc[0]
customer_id = row["customer_id"]
segment = row["segment"]
monthly_spend = float(row["monthly_spend"])
support_tickets = int(row["support_tickets"])
print(customer_id)
print(segment)
print(monthly_spend)
print(support_tickets)Expected output:
C0001
Silver
35.17
2Those four print lines are the proof the names hold the real row — not invented demo numbers.
Step 2 — Reassign when the number changes. Finance asks for a 5% spend uplift on this customer. Overwrite monthly_spend. The old 35.17 is gone unless you stored it under another name.
import pandas as pd
df = pd.read_csv("data/customers.csv")
monthly_spend = float(df.iloc[0]["monthly_spend"])
monthly_spend = monthly_spend * 1.05
print(round(monthly_spend, 2))Expected output:
36.9335.17 × 1.05 = 36.9285, which rounds to 36.93. The name did not change; the value did.
Step 3 — Unpack several names at once. When you already know the order of the fields, one line can bind three names.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.iloc[0]
customer_id, segment, spend = (
row["customer_id"],
row["segment"],
float(row["monthly_spend"]),
)
print(customer_id, segment, spend)Expected output:
C0001 Silver 35.17Use unpacking when a function or a row gives you a known sequence. If the order is unclear, assign one name at a time.
Step 4 — A constant is a name you promise not to overwrite. Python will not stop you. The ALL_CAPS name is a team signal: this is a rule, not a measurement.
import pandas as pd
df = pd.read_csv("data/customers.csv")
support_tickets = int(df.iloc[0]["support_tickets"])
MAX_TICKETS = 10
print(support_tickets > MAX_TICKETS)Expected output:
FalseC0001 has 2 tickets, so they are under the cap. The comparison uses the named cap instead of a bare 10 buried in the line.
Naming
- Use
snake_case:monthly_spend,support_tickets,customer_id. - Name the value, not the type:
segmentnotstr1. - Do not use Python keywords (
if,class,return) as names — that is aSyntaxError. xis allowed by the language and useless to the next reader.
Pause and think
Q1. Why store monthly_spend instead of typing 35.17 in every later line?
When spend updates, you change one assignment. Every print, uplift, and comparison follows. Five pasted numbers become five chances to miss one.
*Q2. What happens to the old value when you write monthly_spend = monthly_spend 1.05?**
The name now points at the new number. The old 35.17 is gone unless another name still holds it.
Q3. Is MAX_TICKETS = 10 a different kind of variable to the language?
No. It is an ordinary assignment. The capitals are a convention that tells teammates "do not overwrite this in the middle of the script."
Guided practice: summarise C0003
Load data/customers.csv, find customer C0003, and print segment and monthly spend. Use names, not raw iloc in the print line.
Hints:
- Hint 1:
df.loc[df["customer_id"] == "C0003"]selects the row. - Hint 2:
iloc[0]turns that one-row table into a Series you can index by column name. - Hint 3: print the two names, not the whole Series.
Checkpoint: you should see Gold and 71.01.
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.loc[df["customer_id"] == "C0003"].iloc[0]
segment = row["segment"]
monthly_spend = float(row["monthly_spend"])
print(segment)
print(monthly_spend)Expected output:
Gold
71.01Independent practice: one-line customer card
Deliverable: a short script that loads the CSV, binds customer_id, segment, and monthly_spend for the first row, and prints a single sentence.
Do it this way:
- Read
data/customers.csv. - Take
df.iloc[0]. - Assign the three names.
- Print one line that a teammate can read without opening the CSV.
Model answer you can verify against:
import pandas as pd
df = pd.read_csv("data/customers.csv")
row = df.iloc[0]
customer_id = row["customer_id"]
segment = row["segment"]
monthly_spend = float(row["monthly_spend"])
print(
customer_id,
"is",
segment,
"with monthly spend",
monthly_spend,
)Expected output:
C0001 is Silver with monthly spend 35.17Common mistake
The deliberate mistake
Symptom: the script prints
71.01in three places. One of them is still35.17after the uplift.Diagnosis: the number was pasted, not stored. Reassignment only updates the name you actually assigned.
Correction: assign once (
monthly_spend = ...), computemonthly_spend = monthly_spend * 1.05, then print the name.Prevention: if a number appears twice, it wants a name. If a limit is a policy (
MAX_TICKETS), write it in ALL_CAPS at the top of the script.
Where this connects
What you can now do:
- Assign, unpack, and reassign names.
- Read one real customer into variables.
- Keep a business cap in a constant.
Next lesson: Data types. Names are useless until you know what kind of value they hold — numbers, text, True/False — and how those types refuse to mix.