Variables & Data Types
Master Python's core data types — numbers, strings, booleans, lists, tuples, dictionaries, and sets — the building blocks of every dataset you'll ever touch.
Every dataset you will ever analyze is built from a handful of basic Python types: numbers, text, true/false values, and containers that group them together. In this lesson you'll learn each type, how to inspect and convert between them, and how to reach into nested structures — the exact skill you need before pandas ever enters the picture.
Variables and naming rules
A variable is a name bound to a value with =. Python has a few hard rules
and a few strong conventions:
- Names can contain letters, digits, and underscores — but can't start
with a digit and can't contain spaces or symbols like
-or@. - Names are case-sensitive:
numberandnumbeRare two different variables (an easy source of bugs — pick one casing and stick to it). - Multi-word names use
snake_casein Python:flight_schedules, notflightSchedules(camelCase is common in other languages, but underscores are the Python convention). - Choose descriptive English names:
total_pricebeatstp.
Numbers: int and float
Python has two everyday number types: int for whole numbers and float
for decimals. The built-in type() function tells you what you're holding —
use it whenever you're unsure:
Note that / returns a float even when the result is a whole number —
10 / 10 is 1.0, not 1. That distinction matters when a library expects
an integer (like an index or a count).
Booleans: True and False
A bool is either True or False (capitalized!). Booleans usually come
from comparison operators, and they're the fuel for every if statement
you'll write in the next lessons:
Strings and f-strings
A str is text between quotes — single or double both work. The killer
feature for data work is the f-string: put an f before the opening
quote and any expression inside curly braces gets evaluated and inserted:
Methods like .upper() return a new string — the original is unchanged.
Strings in Python are immutable: you never edit one in place, you build a
modified copy.
Lists: ordered, changeable collections
A list holds multiple values in order, written with square brackets.
Positions are counted from zero, and you can grab ranges with
slicing — list[start:stop] takes items from start up to (but not
including) stop:
That "stop not included" rule trips everyone up at first, but it has a nice
property: products[2:5] contains exactly 5 - 2 = 3 items, and
products[:k] plus products[k:] reassembles the whole list.
Tuples: lists that can't change
A tuple looks like a list with parentheses — but it's immutable. Once
created, you can't replace, add, or remove items. Use tuples for fixed
records where accidental modification would be a bug (coordinates, RGB
colors, database rows):
Dictionaries: labeled data
Lists find values by position; a dict finds them by key. This is the
single most important container for data science — a JSON API response, a
pandas row, a model's configuration: all dictionaries at heart. Values can be
anything, including other dicts and lists, which is how real-world data nests:
The chaining pattern is worth practicing: users[1]["address"]["city"]
reads as "take item 1 of the list, then its address dict, then the city
inside that". Work through it one bracket at a time.
Sets: unique values only
A set (curly braces, no keys) keeps only unique values and has no
order. Its main use in data work is deduplication and membership tests:
Type conversion and None
You can convert between types with int(), float(), str(), and
bool() — essential when data arrives as text (which it very often does).
Python also has None, a special value meaning "nothing here yet", which is
how missing data is often represented before it becomes NaN in pandas:
The classic string-number bug
If numbers arrive as text (from a CSV, a form, an API), math on them silently misbehaves: "42" + "1" is "421". When results look strange, print type(value) first — a stray string is the most common culprit.
Check your understanding
Q1.Which variable name is invalid in Python?
Q2.What does products[1:3] return for products = ['a', 'b', 'c', 'd']?
Q3.What's the key difference between a list and a tuple?
Q4.Given user = {'name': 'Ana', 'address': {'city': 'Jakarta'}}, how do you get 'Jakarta'?
Q5.What does '7' + '3' evaluate to?
Exercise: Navigate a nested user database
Create a list called users containing three dictionaries. Each has a
name (string), an age (int), and an address key holding another
dict with a city. Use the names Rama (25, Jakarta), Budi (30, Bandung),
and Teguh (28, Jakarta). Then: (1) print Budi's city using chained
access, (2) print an f-string like "Rama is 25 years old and lives in Jakarta" for the first user, and (3) collect all three cities into a
list and use set() to print the unique cities.
Next up: functions — how to package the logic you just wrote so you can reuse it with a single call.