Python Basics #4 — Variables
This lesson is about variables.
A variable is one of the most fundamental and most important concepts in programming. A variable is a memory location where you can store data. That memory location — the variable — is given a name so you can easily tell what’s in it.
When you write programs, you often use the same data many times. Instead of typing the data in every time, it’s more effective and flexible to store the data in a memory location (a variable) and reference it by name wherever needed. The name acts as a label that you and other developers can read and understand.
If you’re new to programming, this might still be abstract. Here’s an analogy.
Imagine you’re using Excel to compute the price of a hamburger. A bad approach: type the hamburger price directly into every formula that needs it. If the menu is small and the price never changes, that “works.” But the moment you have many items or the price changes, typing the data in by hand is inefficient and error-prone. The fix is to put the data into one cell — say, C2 — and reference C2 everywhere. If the price changes, you change C2 once and every formula updates automatically.
But that approach has another problem: with dozens of menu items, you can’t realistically remember which cell stores which price. The fix is to give the cell a meaningful name (like hamburger_price) and reference the cell by name.
This Excel analogy maps closely to variables in programming. The cell holding the hamburger price is the memory location (the variable), and the name attached to the cell is the variable’s name.
In real code we use syntax like:
age = 25The left side (age) is the variable that will hold the data; the right side (25) is the data being stored. The = in the middle is the assignment operator — not mathematical equality. It means “assign 25 into the variable age.”
Conceptually: when Python sees age = 25, it stores 25 somewhere in memory at an address like 0x00000007. To read 25 again later, you don’t reach for the cryptic memory address — you reach for the friendly name age, which is attached to that address like a tag.
In PyCharm or any Python REPL, you can write age = 25 and then print(age) to confirm 25 is shown.
Now let’s look at a tiny Python program that computes hamburger prices — the same logic as the Excel example. Without variables, every time you wanted the hamburger price you’d type the literal number, and if the price changed from 1000 to 1100 KRW you’d have to update every place that uses it. That drives up coding time and almost guarantees you’ll miss a spot, returning incorrect results. And these kinds of bugs — unlike syntax errors — take a long time to debug.
The fix, as discussed, is variables. Define a variable, replace the literal numbers with the variable name, and run the code — same result. Now if you need a 15% discount, you simply multiply the existing price variable by 0.85 to compute the discounted price.
Another good use of variables is interactive programs that talk to the user. A small program that reads a name and age from the user, stores them in variables, and then prints them out — easy.
Variables are also useful for caching database results: store data fetched from a database in a variable and reference it later, reducing repeated queries and lowering database / network load.
Naming rules #
Variable naming follows specific rules. These apply not only to Python but to most programming languages.
Rule 1. Variable names cannot contain spaces. To use multiple words, connect them with underscores or run them together with no spaces.
Rule 2. Variable names cannot contain special characters other than digits, letters, and the underscore.
Rule 3. Variable names cannot start with a digit. The first character must be a letter or an underscore.
Rule 4. You cannot use Python keywords as variable names. Python’s keywords include things like if, else, class, def, for, while, etc.
Rule 5. Variable names are case-sensitive. X and x are two different variables.
Good and bad names #
Let’s look at examples. The bad names in Naming Example 1 all raise syntax errors.
- First example: using two or more words separated by a space. Connect them with underscores or remove the spaces.
- Second example: using a special character other than underscore (e.g., a hyphen) — not allowed.
- Third example: starting with a digit — not allowed.
- Last example: using a Python keyword like
classas a variable name — not allowed.
The bad names in Naming Example 2 don’t raise errors, but they violate PEP 8 (Python’s style guide), so you should avoid them.
- First bad example: starting a variable name with an uppercase letter. Class names start with uppercase, but regular variable and function names should start with lowercase.
- Second bad example: connecting three words with each subsequent word’s first letter uppercase — that’s camelCase. Python uses camelCase only for class names; variables and functions use snake_case.
- Third bad example: a constant. Constants — variables that always hold the same value — should be ALL_UPPERCASE with underscores between words.
- Fourth bad example: a single-letter or unclear variable name like
x. Even though it saves typing, use descriptive names so anyone (including future-you) can understand what’s stored. Code is often shared, and even your own code can confuse you a few months later. - Last bad example: using the name of a Python built-in type or standard module like
int,float,str. Avoid shadowing built-in names.
What can a variable store? #
To wrap up: variables can store integers, floating-point numbers, strings (text — wrapped in single or double quotes, unlike numbers), booleans, lists, tuples, dictionaries, class instances, and even functions. In Python, almost any object can be stored in a variable.
Use variables well to reduce unnecessary repetition and write flexible, performant programs.
That’s it for this lesson. Thanks for reading!