Welcome! Before we touch data, we need to understand the containers Python uses to hold it. Think of this lesson as learning where to put things before you start cooking. Let's go.
What you'll learn
What
A variable is a named label that points to a value stored in your computer's memory. A data type tells Python what kind of value it's dealing with — and therefore what you're allowed to do with it.
Why
Without variables you'd have to hard-code every number directly in your code, making it impossible to change. Without data types, Python couldn't know whether `2 + 2` means arithmetic or string concatenation.
Where it's used
Every single Python program ever written uses variables and data types. They are the atoms of programming.
Where this runs in production
Every fare uses float variables for distance and multiplier, int for minutes, and bool flags to check whether surge rules apply.
A song's duration is an int (milliseconds), its title a str, its popularity a float, and whether it's explicit a bool.
Post like counts are integers. The display label ('1.2M') is a string derived from that int at render time.
Imagine your computer's memory as a giant whiteboard. A variable is a sticky note you stick on a value written on that whiteboard. The note has a name ('price') and it points to the value (9.99). You can peel the note off and stick it on a different value at any time.
int — whole numbers (42, -7, 0). float — numbers with a decimal (3.14, -0.5). str — text in quotes ('hello'). bool — True or False (capital T and F, not lowercase).
You can add 5 metres + 3 metres = 8 metres. But 5 metres + 3 kilograms makes no sense. Python's type system enforces exactly this idea: it lets you add int + float (both numbers), but blocks str + int (different 'units') unless you explicitly convert.
name = "Ada" age = 35 height = 1.68 is_active = True print(type(name)) # <class 'str'> print(type(age)) # <class 'int'> print(type(height)) # <class 'float'> print(type(is_active))# <class 'bool'>
Notice how Python infers the type from the value. The type() function is your best friend for debugging — whenever you're unsure what something is, wrap it in type() and print it.
A single = assigns a value. A double == compares two values. Writing `if x = 5:` is a SyntaxError. Always use == inside conditions.
Click any type to see what operations it supports and where it's used in real data pipelines.
Select a type to see its full definition, operations, and data science usage.
You're building a script that greets a new user by name.
We create a variable called username and assign it the string 'Alice'. Python stores 'Alice' in memory and sticks the label 'username' on it.
Your task
A café tracks its daily metrics. Complete the script below so it prints the correct summary. Use the variables provided — do not hard-code the numbers directly into the print statement.
Revenue: $497.00 Wages: $112.00 Profit: $385.00 Profitable: True
Write your solution in the editor on the right, then hit Run.
What type does Python assign to the value `7.0`?
What is the output of `print(type(True))`?
What does `int("3.14")` return?
What is the correct first step?
Write a script that takes a price in US dollars (stored as a float variable) and prints it converted to euros, formatted to 2 decimal places. Formula: EUR = USD × 0.92. Use usd = 149.50.
Which expression correctly checks whether a variable `x` is between 10 and 20 (inclusive)?
What is a variable in Python, and why does every program need them?
A variable is a named label that points to a value stored in memory. It lets you give a value a meaningful name — like price or user_age — so you can reference and change it throughout your code instead of hard-coding the same literal everywhere. If a tax rate changes, you update one variable rather than hunting through the whole file. Variables are what make a program flexible: the same code runs on today's data and tomorrow's just by pointing the labels at new values.
Python is dynamically typed. What does that mean, and what are the trade-offs for data science work?
Dynamic typing means you never declare a type — Python infers it from the value you assign, and a variable can even point to a different type later. You write x = 5 instead of int x = 5, which speeds up exploratory analysis in notebooks where you're constantly reshaping data. The trade-off is that type errors surface at runtime rather than being caught up front, so a column you assumed was numeric but is actually strings won't complain until the calculation fails. That's why many teams add type hints and use tools like mypy on production pipelines. In short, dynamic typing is a gift during exploration and a risk in production code that you manage with discipline.
Why does 0.1 + 0.2 not equal exactly 0.3 in Python, and how would you handle money in a data pipeline because of it?
Floats are stored in binary using the IEEE 754 standard, and most decimal fractions — including 0.1 — have no exact binary representation, so Python stores the nearest approximation. When you add two approximations the tiny errors accumulate, giving results like 0.30000000000000004. This is a property of the hardware format, not a Python bug, and every language with IEEE 754 floats behaves the same way. For money I never rely on float equality: I either work in integer cents, or use Python's decimal.Decimal type which stores base-10 values exactly. When comparing floats generally, I compare within a small tolerance using something like math.isclose rather than ==. The practical rule is that floats are for measurement, not for exact accounting.
Common Mistakes to Avoid
1) Using = (assignment) where you meant == (comparison) inside an if — Python treats `if x = 5:` as a SyntaxError. 2) Assuming input() gives you a number — it always returns a str, so '25' + 5 raises a TypeError until you convert with int(). 3) Testing floats for exact equality: 0.1 + 0.2 == 0.3 is False; compare with a tolerance or use Decimal. 4) Writing True/False in lowercase — Python's booleans are capitalised, and true is just an undefined name. 5) Reusing a built-in name like list, str, or type as a variable, which quietly breaks that function for the rest of your program.
Ask the AI Tutor
Try these prompts in the AI Tutor panel: • 'ELI5: what is the difference between an int and a float?' • 'Quiz me on what type() returns for five different values.' • 'Show me a real bug caused by forgetting to convert a string to a number.' • 'Explain why 0.1 + 0.2 isn't 0.3 with a fresh analogy.' • 'Interview mode: ask me about dynamic typing and grade my answer.'
Glossary
Variable — a named label pointing to a value in memory. Value — the actual data a variable points to. Data type — the kind of value (int, float, str, bool) that decides which operations are allowed. int — a whole number. float — a number with a decimal point. str — text inside quotes. bool — True or False. Dynamic typing — Python infers a variable's type from its value at runtime rather than requiring a declaration. Type conversion (casting) — changing a value from one type to another with int(), float(), or str(). f-string — a formatted string literal (prefixed with f) that embeds variables inside {}.
Recommended Resources
• Docs: the official Python tutorial section 'An Informal Introduction to Python' — numbers and strings from the source. • Read: 'Floating-Point Arithmetic: Issues and Limitations' in the Python docs for the full 0.1 + 0.2 story. • Practice: open a Python REPL and run type() on ten different values until the four primitive types feel automatic. • Next in DSM: you can store single values — next you'll work with text in depth in Strings & String Methods, the raw material of almost every messy dataset.
Recap
✓ A variable is a named label that points to a value in memory. ✓ Python infers the data type automatically from the value you assign (dynamic typing). ✓ The four primitive types are int, float, str, and bool. ✓ Use type() to inspect any value's type at runtime. ✓ Type conversion is explicit: int(), float(), and str() move values between types. ✓ Floats are approximations, so never test them for exact equality. Next up: Strings & String Methods. You can now store text in a str — next you'll slice it, reshape it, and clean it with the methods you'll reach for every time a dataset arrives messy.
Run your code to see the output here.