Skip to content
Python for Data Science
Python Fundamentals 8 min read

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.

Download notebook Open Google ColabIn Colab: File → Upload notebook → pick the downloaded file.

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: number and numbeR are two different variables (an easy source of bugs — pick one casing and stick to it).
  • Multi-word names use snake_case in Python: flight_schedules, not flightSchedules (camelCase is common in other languages, but underscores are the Python convention).
  • Choose descriptive English names: total_price beats tp.
Python — runs in your browser

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:

Python — runs in your browser

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:

Python — runs in your browser

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:

Python — runs in your browser

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 slicinglist[start:stop] takes items from start up to (but not including) stop:

Python — runs in your browser

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):

Python — runs in your browser

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:

Python — runs in your browser

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:

Python — runs in your browser

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:

Python — runs in your browser

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

5 questions · free
  1. Q1.Which variable name is invalid in Python?

  2. Q2.What does products[1:3] return for products = ['a', 'b', 'c', 'd']?

  3. Q3.What's the key difference between a list and a tuple?

  4. Q4.Given user = {'name': 'Ana', 'address': {'city': 'Jakarta'}}, how do you get 'Jakarta'?

  5. 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.