tips.md basics.html variables.html functions.html loops.html lists.html project.html
// lesson 2

Variables and Data Types

~/lessons/variables.html

A variable is just a named box that holds a value. Instead of writing the number 20 over and over in five different places, you store it once under a name — say age — and use that name everywhere instead. If the value needs to change later, you change it in one place, and everywhere that uses the name automatically gets the new value.

age = 20 print(age)

That's the entire idea. The name on the left, an equals sign, the value on the right. The equals sign here doesn't mean "equal to" the way it does in math class — it means "store this value under this name." That trips people up constantly at first, so it's worth saying plainly: age = 20 is not a statement that age equals 20, it's an instruction to store 20 in a box labeled age.

Different kinds of values

Not all data is the same shape, and the computer treats different shapes differently. The common ones you'll run into constantly:

age = 20 # a whole number (integer) price = 4.99 # a decimal number (float) name = "Alex" # text (string) is_admin = False # true or false (boolean)

Why this matters: you can add two numbers together and get a bigger number, but if you try to "add" a number and a piece of text, most languages either throw an error or do something you didn't intend, like sticking them together as text instead of doing math. Knowing what type a value is tells you what you're allowed to do with it.

Naming variables well

You get to pick these names, and the name you pick is doing real work — it's the explanation of what the value is for. A variable named x tells the next person reading your code (often you, in three weeks) absolutely nothing. A variable named failedLoginAttempts tells them everything they need to know without a single comment.

Most languages also have rules: names usually can't start with a number, can't contain spaces, and can't be one of the language's reserved words (like if or for). Beyond the rules, though, clarity is a choice you make every time you name something, and it's one of the cheapest ways to make your code easier to work with later.

Variables can change (that's the point)

The word "variable" means the value is allowed to vary. You can reassign it later in the program, and from that point on, the name points to the new value.

score = 0 print(score) # 0 score = score + 10 print(score) # 10

That second line, score = score + 10, looks strange if you're reading it as math — a number can't equal itself plus ten. But remember, it's not math, it's an instruction: take whatever score currently holds, add 10 to it, and store the result back into score. This exact pattern, updating a variable based on its own current value, shows up constantly once you get to loops.