🐍 Python for Data Engineers · Chapter 1
Values, names and types
A variable is not a box you put a value in. It is a label you stick on a value. Everything else follows from that.
Study this lesson free in the app →
The basics
A value is a piece of data — 250, "ride_9001", True. A name (variable) is a label you stick on a value so you can refer to it later. A type says what kind of value it is, and therefore what you can do with it.
The problem it solves: every program is values flowing through names. Get the model wrong now and later lessons — lists that change "by themselves", filters that match nothing — look like magic.
The mental model: luggage tags, not boxes. fare = 250 ties the tag fare to the bag holding 250. total = fare ties a second tag to the same bag — no copy. fare = 300 moves the fare tag to a new bag; total doesn't move.
Words you must know 5
- Type
int(whole numbers),float(decimals, never for money),str(text),bool(True/False). Python works out the type from the value; you never declare it, but it is real.- None
- "there is no value here". Not
0, notFalse, not"". - Immutable
- can't be changed after creation. Strings are immutable, so
city.strip()returns a new string and leavescityalone. - Casting
- converting deliberately:
int("250"). Files and APIs hand you text; a CSV's"250"is not equal to250. - Truthiness
- in an
if,0,"",[]andNoneall count as false.
Explained
fare = 250
fare = 300Most people read that as "the box called fare now holds 300".
Python reads it as "take the label fare off the value 250 and stick it on the value 300".
Same result here. Completely different result in about six lessons' time — so learn it the right way now.
Luggage tags at the airport
Bags sit on the belt. Each bag exists whether or not anyone has claimed it.
A name is a tag you tie to a bag. fare = 250 ties the tag fare to the bag containing 250.
Two tags can hang off the same bag — total = fare doesn't create a second 250, it adds a second tag to the one that's there.
And when the last tag comes off a bag, the airport takes it away. That is garbage collection, and it is the only memory management you ever have to think about.
The types you will use every day
| Type | Example | What it's for | | --- | --- | --- | | int | 250 | whole numbers — counts, ids | | float | 250.75 | decimals — never for money | | str | "ride_9001" | text | | bool | True / False | yes/no | | None | None | "there is no value here" |
Python figures out the type from the value — you never declare it. But the type is real and it decides what works:
"250" + 10 # TypeError: can only concatenate str to str
int("250") + 10 # 260 ✅
type(fare) # <class 'int'>None is worth its own moment. It is not zero, not False, not an empty string — it means absent. In data work that distinction matters constantly: a fare of 0 is a free ride; a fare of None is a ride we have no fare for.
Strings: the 10 things you'll do
ride = "ride_9001"
city = " Shillong "
len(ride) # 9
ride.upper() # 'RIDE_9001'
city.strip() # 'Shillong' — kills the spaces
ride.startswith("ride") # True
ride.replace("ride", "trip") # 'trip_9001'
ride.split("_") # ['ride', '9001']
"_".join(["ride", "9001"]) # 'ride_9001'
ride[0] # 'r' — first character
ride[-1] # '1' — last character
ride[5:] # '9001' — from index 5 to the end
# f-strings: put values inside text
fare = 250
print(f"{ride} in {city.strip()} cost ₹{fare}")
# ride_9001 in Shillong cost ₹250Notice city.strip() doesn't change city — it returns a new string. Strings in Python can never be modified in place. city = city.strip() is how you keep the result.
Watch the tags move
Step 3 is the one to hold on to: reassigning a name never disturbs other names.
Predict the output
a = 10
b = a
b = b + 5
print(a, b)
x = "250"
y = 250
print(x == y)
print(int(x) == y)Gotchas that cost beginners hours
=vs==— one assigns, one compares.if fare = 250is a syntax error;if fare == 250is a question- Numbers from files are text — CSV and JSON hand you
"250". Convert deliberately floatfor money —0.1 + 0.2is0.30000000000000004. Use integers of paise, orDecimalNoneisn't0— andif fare:treats0,"",[]andNoneas all false. Useif fare is None:when you mean absent- Indentation is syntax — four spaces, consistently. Mixing tabs and spaces is a real error, not a style opinion
- Case matters —
Fareandfareare different names
A name is a tag on a value, not a box holding it.
Re-tagging one name never moves another.
"250" != 250. Fix types at the door.
Words to know
| Variable | A name bound to a value — a tag, not a container. |
|---|---|
| Type | What kind of thing a value is: int, float, str, bool, None. |
| Immutable | Cannot be changed after creation. Strings, ints and tuples are immutable. |
| None | Python's 'no value here' — distinct from 0, False or an empty string. |
| f-string | A string prefixed with f where {expressions} are substituted. |
| Type casting | Converting a value's type explicitly, e.g. int("250"). |
| Truthiness | How a value behaves in an if: 0, "", [], {} and None are all falsy. |
In two minutes
- A name is a tag on a value, not a box; assignment moves the tag, never copies the value.
- Re-tagging one name never changes another name that pointed at the old value.
- Core types: int, float, str, bool, None — Python infers them, but they decide what operations work.
- Data from CSV/JSON arrives as strings; '250' == 250 is False, so convert types at the door.
- Strings are immutable: methods like strip, upper and replace return a new string you must assign.
- None means absent; 0 means a real zero. Use 'is None' when you mean missing.
- Never use float for money — store paise as int or use Decimal.
Test yourself
1. a = 10; b = a; b = b + 5. What is a?
- 15
- 10
- None
- an error
Show the answer
10 — b was re-bound to a new value. Rebinding one name never affects another name that happened to share the old value.
2. What does "250" == 250 evaluate to?
- True
- False
- TypeError
- None
Show the answer
False — A string is never equal to a number. This silently makes filters match nothing — the classic CSV bug.
3. Why should you not use float for money?
- It is slower
- Binary floats can't represent decimals exactly, so errors accumulate
- It doesn't support addition
- It rounds to whole numbers
Show the answer
Binary floats can't represent decimals exactly, so errors accumulate — 0.1 + 0.2 == 0.30000000000000004. Over millions of rows this becomes a real discrepancy against the bank. Use Decimal or integer paise.
4. city.strip() — what happens to city?
- It loses its spaces
- Nothing — strip returns a new string
- It becomes None
- It raises an error
Show the answer
Nothing — strip returns a new string — Strings are immutable. Every string method returns a new string; you must assign it to keep the result.
5. What does 7 / 2 return in Python 3?
- 3
- 3.5
- 4
- '3.5'
Show the answer
3.5 — / always returns a float in Python 3, even for two ints. Use // for floor division (3). The answer 3 is Python 2 behaviour and C/Java integer division.
6. A CSV row gives fare = "250.50". Which line correctly gets a number?
- int(fare)
- fare + 0
- float(fare)
- fare.number()
Show the answer
float(fare) — int('250.50') raises ValueError because the string isn't a whole number; fare + 0 is a TypeError (str + int). float() parses it — and for money you'd then convert to paise or use Decimal.
There are 10 questions, 3 puzzles and 5 recall cards for this lesson in the app — the recall cards come back on later days, just before you would forget.
Start this lesson →