Variables and types
Almost everything a program does comes down to one idea: take a value, remember it, and use it again later. To remember a value, the computer needs two things — a place to keep it and a name to call it by. That pairing of a name with a stored value is a variable. This lesson builds the idea from nothing: what a variable really is, how you put values in and swap them out, the basic kinds of values (their types), and why mixing types the wrong way breaks your program.
Start with the picture. Inside the computer there is a vast wall of tiny storage slots — its memory. Each slot can hold a value. A variable is a name tag stuck onto one of those slots. Think of a labeled box on a shelf: the label is the name, whatever you drop inside is the value. Once the box is labeled, you never have to remember where on the shelf it sits — you just say the name and the computer fetches what's inside. The whole point of a name is that you, the programmer, work with a meaningful word like age instead of a raw memory location you'd never want to track by hand.
:::note One sentence to hold onto A variable is a name that refers to a value stored in memory. The name and the value are two separate things — the name is yours to choose, the value is whatever you put there. :::
You create a variable with an assignment: a name, then an = sign, then a value.
age = 30
print(age)
# output: 30
Here is exactly what happens, in order. First Python looks at the right side and works out its value (here it's just 30). Then it takes the name on the left and binds it to that value — it sticks the label age onto a slot holding 30. The direction is fixed and it matters: value flows right to left, into the name. Read age = 30 as the command "store 30 in a box called age," not as the math sentence "age equals 30." The = here means store, it is an instruction, not a statement of fact. (The math-style "are these equal?" test is a different symbol, ==, which you'll meet later.)
:::tip Right side first
Because the right side is evaluated first, you can put a whole calculation there and only the result gets stored: total = 2 + 3 stores 5, not the text "2 + 3". The name on the left never appears on the right of its own first assignment, because there's nothing to read yet.
:::
A variable is not locked to one value forever. You can reassign it — point the same name at a new value. The label moves to a new box; the old value is dropped and forgotten. Trace these three lines top to bottom:
age = 30 # age now refers to 30
age = 31 # the name age is re-pointed at 31; the 30 is gone
print(age)
# output: 31
Only one value lives under the name at a time, and it's always the most recent one. Reassignment can even use the name's own current value on the right — remember, the right side is computed first, before the name is rebound:
score = 10
score = score + 5 # right side first: 10 + 5 = 15, then store 15 in score
print(score)
# output: 15
Types: what kind of thing a value is
Every value has a type — what kind of thing it is. The type is not decoration; it decides what operations the value supports. You can subtract two numbers, but subtracting two pieces of text is meaningless, and the type is how Python knows the difference. Here are the basic types you'll meet first, each created just by writing a literal value:
count = 30 # int — a whole number
price = 3.14 # float — a number with a decimal point
name = "Ada" # str — text, always in quotes
is_open = True # bool — only True or False
result = None # None — "nothing here yet"
- Integer (
int) — a whole number, no decimal point:30,-7. You can do exact arithmetic with them:30 + 7is37,30 * 2is60. Counting things (people, items, clicks) almost always uses ints. - Floating point (
float) — a number that can carry a fractional part:3.14,-0.5. Use it for measurements and prices. One caution: floats are approximate. The computer stores them in binary, and some decimals don't fit exactly, so0.1 + 0.2prints0.30000000000000004rather than a clean0.3. Fine for most work, but never assume a float is to-the-last-digit exact. - String (
str) — text inside quotes:"Ada","hello world". The quotes are how Python knows it's text and not a variable name. You can join strings with+:"Ada" + "lovelace"is"Adalovelace". - Boolean (
bool) — a yes/no value, only everTrueorFalse(capital first letter, no quotes). Comparisons produce booleans:5 > 3isTrue. Booleans drive every decision a program makes. - None (
NoneType) — a special value meaning "no value yet" or "intentionally nothing." It is not0and not the empty text""; it's a deliberate placeholder for an answer you don't have.winner = Nonesays "no winner has been decided."
When you want to check what type a value has, ask Python directly with type():
print(type(30)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("Ada")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
Dynamic typing
Notice you never had to tell Python the type — it works it out from the value you wrote. That's called dynamic typing: the value carries its own type, and a name simply takes on the type of whatever it currently holds. Store a number, the name is a number; reassign it to text, and now it's text:
x = 5
print(type(x)) # <class 'int'>
x = "five"
print(type(x)) # <class 'str'> (same name, new type)
Some other languages work the opposite way: they make you declare a type up front (you'd write something like "this box may only ever hold integers"), and they refuse to let it hold anything else. Python keeps it loose — you just assign and go. That's convenient, but it puts the burden on you to keep track of what each variable holds, because the types still matter at the moment you use them.
Why types matter: TypeError and conversion
Here is exactly why they matter. Each type supports its own set of operations, and Python will not silently mix them. Try to add text to a number and it stops with an error rather than guessing:
print("3" + 5)
# TypeError: can only concatenate str (not "int") to str
A TypeError means "you used a value of the wrong type for this operation." The trouble with "3" + 5 is that + means two different things: for numbers it adds (giving 8), for strings it glues text together (giving "35"). Python can't tell which you wanted, so — true to the rule from the last lesson, the computer never guesses your intent — it refuses and reports the error. The fix is to make both sides the same type first, by converting one of them.
Type conversion turns a value of one type into another. Each type name doubles as a converter you call like a function: int(...), float(...), str(...). Pick the type you actually want, then do the operation:
# want a number? turn the text "3" into the int 3
print(int("3") + 5) # 8 (number addition)
# want text? turn the number 5 into the str "5"
print("3" + str(5)) # "35" (text glued together)
print(float("2.5")) # 2.5 (text to float)
print(str(3.14)) # "3.14" (number to text)
:::tip Two gotchas worth memorizing
int("3.5")raises aValueError.int()only accepts text that looks like a whole number; the decimal point throws it. If you want the number, go throughfloatfirst:int(float("3.5"))gives3(it chops the fraction off, it does not round)."3" + "5"is"35", not8. Both sides are text, so+glues them. To add the numbers you must convert both:int("3") + int("5")gives8. :::
Naming variables
Now the rules for naming variables. A name must be made of letters, digits, and underscores (_) only — no spaces, no punctuation like - or !. It cannot start with a digit (2nd is illegal; second or v2 are fine). And names are case-sensitive: age, Age, and AGE are three different variables.
# legal names
total_price = 42
user2 = "Ada"
# illegal — each of these is a SyntaxError
# 2nd_place = 1 (starts with a digit)
# total-price = 1 (the - is not allowed)
Beyond the hard rules, follow the Python convention: lowercase words joined by underscores — called snake_case — and names that describe what they hold. Compare a bad name with a good one:
x = 19.99 * 3 # bad: what is x? you'll forget by tomorrow
total_price = 19.99 * 3 # good: reads like plain English
Name versus string
Finally, the single most common beginner mix-up: a variable name versus a string that contains those same letters. Quotes make all the difference. Without quotes, Python reads a word as a name to look up; with quotes, it's literal text.
name = "Ada"
print(name) # Ada (no quotes: look up the variable, print its value)
print("name") # name (quotes: this is just the 4 letters n-a-m-e)
name and "name" are unrelated: one is a label pointing at the value "Ada", the other is the literal four-character text. Mix them up and you'll print the wrong thing — or hit a NameError if you drop the quotes off text that was never a variable.
Why it matters
You now have values, names to hold them, and types that decide what's allowed. The next step is doing things with them — adding, comparing, combining — using operators and expressions. Everything there builds on one fact from this lesson: the operation you can run depends entirely on the types involved.
Where this leads: dictionaries are the workhorse of real software and the interview pattern called the hash map. See Where this leads next.
Checkpoint
Variables and types
Pass to unlock the Next button belowNext: Operators →