Variables and Values
Everything in a program is data, and variables are how you store and name it. Master this and the basic types, and you have the raw material of all code.
This course is where you actually learn Python by writing it - ideally typing every example and running it (a free online editor or the sandbox works fine). We start with the most fundamental idea in all of programming: storing data in named variables. Get comfortable with variables and the basic types, and you have the raw material everything else is built from.
Variables: naming your data
A variable is a name that points to a value. You create one with = and use the name whenever you want the value.
name = 'Ada' # a variable named 'name' holding the text 'Ada'
age = 30
print(name) # Ada
print(age + 1) # 31
age = age + 1 # variables can change - now age is 31= states a fact; in code x = 5 is a command: 'put 5 into the box called x'. That's why age = age + 1 makes sense - it means 'take age, add 1, and store the result back in age'. Names should describe what they hold (total, not t).The basic types
Values come in types. The four you'll use constantly:
| Type | Is | Example |
|---|---|---|
| int | A whole number | 42, -7, 0 |
| float | A decimal number | 3.14, -0.5 |
| str | Text (a 'string') | 'hello', "Ada" |
| bool | True or False | True, False |
The type decides what you can do: you can add numbers, join strings, and test bools. Mixing them wrongly (like adding a number to text) causes an error - which is normal and fixable.
Try it and see
- Type and run every example - reading isn't enough; doing wires it in.
- Experiment - change values, combine variables, print things.
- Check a type with
type(x)if you're unsure what something is. - Errors are feedback - read them; they usually tell you what's wrong.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A variable is a name pointing to a value; create it with = (which means 'store', not 'equals').
- The core types: int (whole numbers), float (decimals), str (text), bool (True/False).
- The type determines what operations make sense; mixing them wrongly gives a normal, fixable error.
- Type and run every example - doing, not just reading, is how you actually learn.
Frequently asked
Why does age = age + 1 work? It looks like nonsense mathematically.
Because in programming = is an instruction to store, not a mathematical statement of equality. Read age = age + 1 as: 'calculate the right side (the current age plus 1), then store that result back into the variable age'. So if age was 30, the right side becomes 31, and age is updated to 31. It's an extremely common pattern (adding to a running total, counting, etc.). If the = symbol confuses you, mentally translate it to '<-' ('gets') - age <- age + 1 - which makes the 'store the new value' meaning obvious.
Do I need to declare a variable's type like in some other languages?
No - Python figures out the type from the value you assign, so you just write name = 'Ada' or age = 30 and Python knows one is a string and the other an int. This makes Python beginner-friendly. The type still matters (it decides what you can do with the value), and it can change if you reassign the variable to a different kind of value, but you never write the type out. If you're ever unsure what type a variable currently holds, type(variable) tells you.
Related lessons
Making Decisions with if
Programs get useful when they can decide. if / elif / else let your code do different thinโฆ
Python Fundamentals (Hands-On)Repeating with Loops
Computers are brilliant at doing the same thing many times. Loops let you repeat actions wโฆ
Learn to Code (from Zero)How Programs Think
Almost all programs are built from a few simple building blocks - values, decisions and reโฆ