◐ ai-teacher

🐍 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, not False, not "".
Immutable
can't be changed after creation. Strings are immutable, so city.strip() returns a new string and leaves city alone.
Casting
converting deliberately: int("250"). Files and APIs hand you text; a CSV's "250" is not equal to 250.
Truthiness
in an if, 0, "", [] and None all count as false.

Explained

fare = 250
fare = 300

Most 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 ₹250

Notice 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

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

VariableA name bound to a value — a tag, not a container.
TypeWhat kind of thing a value is: int, float, str, bool, None.
ImmutableCannot be changed after creation. Strings, ints and tuples are immutable.
NonePython's 'no value here' — distinct from 0, False or an empty string.
f-stringA string prefixed with f where {expressions} are substituted.
Type castingConverting a value's type explicitly, e.g. int("250").
TruthinessHow a value behaves in an if: 0, "", [], {} and None are all falsy.

In two minutes

Test yourself

1. a = 10; b = a; b = b + 5. What is a?

  1. 15
  2. 10
  3. None
  4. 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?

  1. True
  2. False
  3. TypeError
  4. 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?

  1. It is slower
  2. Binary floats can't represent decimals exactly, so errors accumulate
  3. It doesn't support addition
  4. 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?

  1. It loses its spaces
  2. Nothing — strip returns a new string
  3. It becomes None
  4. 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?

  1. 3
  2. 3.5
  3. 4
  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?

  1. int(fare)
  2. fare + 0
  3. float(fare)
  4. 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.

Learn it so it stays. Read it, test yourself, then recall it on two different days. The app schedules that for you, free.

Start this lesson →