Variables & Data Types
Lesson 2 of 7 · View course roadmap
Learn the idea
A variable is a named box that stores a value. You create one with = (the assignment operator):
Python has four essential basic types:
str— text:"hello"int— whole numbers:42float— decimals:3.14bool— truth values:True/False
Use type() to check what type a value is. Variable names should be lowercase with underscores: user_age, not UserAge. Python is dynamically typed — a variable can hold any type, and you never declare the type up front.
Where you'll use this
Every configuration file, user record and shopping cart is variables of these four types. Type confusion (a "5" that should be a 5) is the single most common bug class in real codebases — banks have lost money to it.
Common mistakes
- Using a variable before assigning it → NameError. Python reads top-to-bottom.
- Confusing
=(assign) with==(compare). - Expecting
"5" + 5to work — Python never silently converts between str and int the way JavaScript does. Convert explicitly withint()orstr().
Pro tip
Name variables for what they contain, not their type: total_price beats tp beats x. Six months from now, the reader of your code is you.
Watch how it runs — line by line
Press play to watch Python execute this code, one line at a time.
Try it yourself
The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.
Output appears here…
Pass the challenge +20 XP
Create a variable language with the value "Python" and a variable year with the value 1991. Then print them both on one line with a single print, producing Python 1991.
Python 1991
print(language, year) automatically separates values with a space.
language = "Python" year = 1991 print(language, year)
Run your code to check it…
Check your understanding
Your notes (saved on this device)
Tip: use ← and → to move between lessons, ⌘K to search everything.