Chapter 2 — Python for Analysts: “A Bear in the Archive I”

Variables, Values, and Types

16 min

A name, a label, and a value

QUERY unpacked the first box from the black box — a daily data export. The screen showed a column of numbers with no labels at all.

Numbers without names are noise. Give them names, and they become readings. — QUERY

A cadet examines a hologram where a label moves between values, while four kinds of capsules represent basic data types and conversions between them.
The “=” sign doesn't compare values; it binds a name to a value. The value's type determines what you can do with it.

In the prologue, you already wrote orders = 1284. Let's look more closely at that line, because everything that follows builds on it.

orders = 1284

The = sign here does not mean mathematical equality. It's an instruction: “assign the value on the right to the name on the left.” Read it from right to left: Python first evaluates what's on the right, then binds the name to the result.

Here's a useful mental model: the value 1284 lives somewhere in memory, and orders is a label attached to it. You can move that label:

orders = 1284      # the orders label points to 1284
orders = 1310      # the same label moved to 1310; 1284 is no longer needed
print(orders)      # 1310

That's why this next line may look strange at first, even though it's perfectly normal Python:

orders = orders + 120   # take the current value of orders, add 120, and name the result orders again

In math, x = x + 120 makes no sense. In Python, it means “recalculate and assign the new result.” You can write the same thing more briefly as orders += 120.

Naming rules. Use Latin letters, digits, and _; a name can't start with a digit; case matters — orders and Orders are two different names. Choose names that explain what they contain: avg_check instead of x, dau_today instead of d1. A week from now, you won't remember what d1 means, but QUERY will.

Common mistake #1. Reversing the two sides:

1284 = orders   # SyntaxError: cannot assign to literal

In the simple case shown here, the name goes on the left of =, and the value or expression goes on the right.

Common mistake #2. A typo or different capitalization:

orders = 1284
print(Orders)   # NameError: name 'Orders' is not defined

NameError is one of the most common beginner errors. It's usually caused by a typo, different capitalization, or a cell above that you forgot to run.

A variable is a name for a memory cellcodePython memoryorders = 1284avg_check = 730revenue = orders * avg_checkorders1284avg_check730revenue937320on line 3 Python substitutes the values: 1284 × 730name → value; “=” means “put it into the cell”
A variable name is bound to a value in memory like a label. A new assignment binds the same name to a different value.

The four types everything starts with

Every value in Python has a type, which determines what you can do with that value. To get started as an analyst, you only need four:

TypeWhat it isExamples from the archive
intwhole number1284 orders, $73, -41 returns
floatnumber with a fractional part6.0 percent conversion, $7.83 average order value
strtext (a string)'Furrville', '2025-03-14', '124'
boolBoolean: only True or False“is this day anomalous?”

You can check the type of any value with type():

type(1284)        # <class 'int'>
type(7.83)        # <class 'float'>
type('Furrville')  # <class 'str'>
type(True)        # <class 'bool'>

Look closely at the third row of the table: '124' is a string, even though the characters inside the quotes are digits. The quotes make the difference. 124 is a number you can use in arithmetic; '124' is text that only looks like a number.

Arithmetic

124 + 38       # 162    addition
124 - 38       # 86     subtraction
1284 * 73      # 93732  multiplication
124 / 4        # 31.0   division — ALWAYS returns a float, even when it divides evenly
124 // 40      # 3      floor division (how many whole times it fits)
124 % 40       # 4      remainder
2 ** 10        # 1024   exponentiation

Remember the rule for /: 10 / 2 gives 5.0, not 5. Division always returns a float, even when the result is a whole number. When you need an integer, wrap the result in int() or use //.

Comparisons produce bool

124 > 100      # True
73 == 64       # False   == ASKS 'are these equal?', while = COMMANDS 'assign this'
73 != 64       # True    not equal
6.0 >= 6.0     # True
'Furrville' == 'furrville'   # False — case matters; string comparison is exact

The result of a comparison is an ordinary bool value, so you can store it in a variable:

is_big_day = orders > 1200
print(is_big_day)   # True

These comparisons will become filters later. In Chapter 3, pandas will use the same kind of comparison to select rows across an entire table at once.

Common mistake #3. Writing = when you mean to ask a question with ==:

(orders = 1284)    # SyntaxError
(orders == 1284)   # correct

Why this is lesson one, not a footnote

Here's entry #3 from L.'s journal:

I spent half a day trying to figure out why the day's revenue turned into a 41-digit number. The answer: the export gave me the amounts as strings, and I added them together. Python didn't object — it faithfully glued the text into a string half a kilometer long.

This is a routine data problem. CSV files, some BI exports, and API responses can return values as strings. CSV itself has no data types: it's text separated by commas. Here's what happens if you don't notice:

a = '124'   # came from a CSV — this is TEXT
b = '38'    # this is text too

a + b        # '12438'  — not addition, but concatenation

For strings, + means “join one to the other.” You won't get an error, which makes this especially easy to miss: the report won't crash; it'll simply give you the wrong result.

Python does raise an error when you try to mix these types directly:

'124' + 38
# TypeError: can only concatenate str (not "int") to str

In plain English: “You can only join a string to another string, but you gave me a number.” This kind of error is easy to notice because Python stops and shows it immediately.

Type conversion

int('124')         # 124      string → integer
float('730.5')     # 730.5    string → float
str(124)           # '124'    number → string
int(730.9)         # 730      float → int: the fractional part is TRUNCATED, not rounded
round(730.9)       # 731      this is rounding
int('  124  ')     # 124      int() tolerates spaces around the edges

What int() won't tolerate:

int('730.5')       # ValueError: invalid literal for int() with base 10: '730.5'
int('1 240')       # ValueError — space inside the number
int('$7.30')       # ValueError — extra characters

For the first case, convert to float first and then to int: int(float('730.5')) gives 730. For the others, you need to clean the string first. We'll cover that in the next lesson.

One last trap

Strings are compared character by character, like words in a dictionary, rather than by numeric value:

'9' > '10'     # True  (!) because the character '9' comes after '1'
9 > 10         # False

If sorting “by amount” suddenly puts $0.09 above $100.00, you're sorting strings instead of numbers. This is a classic reporting bug, and you can identify it quickly with print(type(value)).

Investigator's rule: when you get new data, check its types first. Don't rely on “those look like numbers”; use type() and verify.

Live cell. First, predict what each line will print. Then run it and compare the output with your prediction. After that, change the values and run it again.
python
Practice: write the code
A row from the March 14 daily export arrived entirely as text. Convert three fields to numbers (orders, avg_check, refunds) and calculate net_revenue — revenue after refunds, where each refund subtracts one average order value.
python
Practice: write the code
Nightly DAU check. Monitoring returned two values as text. Convert them to numbers, calculate drop_ratio — today's DAU as a share of yesterday's DAU (just divide one by the other) — and set the is_anomaly flag to True if today is less than 80% of yesterday.
python
Check yourself
Two fields came from a CSV: a = '124' and b = '38'. What will print(a + b) print?
Check yourself
An API returned an order amount as a string: raw = '12.51'. What will int(raw) return?