What is a Variable?
Up until now, every piece of text we printed was hardcoded. If we wanted to change the message, we had to go back into the code, rewrite it, and run it again.
Real software doesn't work that way. Applications need to remember information dynamically like a user's score, their username, whether they are logged in, or items in a shopping cart.
To do this, we use variables.
Think of a variable as a labeled storage box in your computer's memory. You write a label on the outside of the box so you know what is inside, and then you store a piece of data inside it. Whenever you need that data later, you just look at the label instead of digging through the contents.
Creating and Assigning Variables
In Python, creating a variable and putting data inside it is called assignment. We use a single equals sign (=) to do this.
Note: In mathematics, = means "equals" (both sides are balanced). In programming, = means "take whatever is on the right side and store it inside the container on the left."**
Look at how this works in code:
The Power of Reassignment
The real magic of variables is that they can change or reassign over time as your program runs.
Try running the code below to see how a variable's contents can be updated:
When Python reads player_score = player_score + 10, it evaluates the right side first (taking the current value of player_score, which is 50, and adding 10 to it) and then stores the new total (60) back into the player_score box!
Variable Naming Rules (The Law)
You can name your storage boxes almost anything you want, but Python enforces a few strict technical laws. If you break these rules, Python will throw a SyntaxError.
Here are the laws of variable naming:
- Must start with a letter or an underscore (_): You can never start a variable name with a number.
score1is legal;1scoreis illegal and will crash your program. - Can only contain alphanumeric characters and underscores: Variable names can include letters (
A-z), numbers (0-9), and underscores (_). You cannot use spaces, hyphens, or special symbols like@,$, or%. - Case-sensitive: Just like function names,
player,Player, andPLAYERare three completely separate variables in Python.
Let's look at examples of valid versus invalid names:
LEGAL (These work perfectly)
user_name = "Augustine" score_99 = 100 _secret = "hidden"
ILLEGAL (These will crash your program with a SyntaxError)
2nd_player = "Alice" # Starts with a number user-name = "Bob" # Uses a hyphen instead of an underscore my score = 50 # Contains a space
Reserved Keywords
There is one final rule: You cannot use Python's built-in keywords as variable names.
Keywords are special words that Python has already reserved for its own core commands. For example, you cannot name a variable print, if, for, or class. If you try to use them as storage containers, Python will get confused and crash.
Variable Naming Best Practices (PEP 8)
Knowing what Python allows you to do is only half the battle. Professional developers follow strict industry style guidelines (known in Python as PEP 8) to make sure their code is readable for humans.
Here are the golden rules of clean variable naming:
- Use
snake_case: In Python, multi-word variable names should always be written in lowercase letters with words separated by underscores (_). - Good:
user_profile_score - Bad:
userProfileScoreorUserProfileScore(other languages use different styles, but Python loves snake_case). - Be Descriptive: Never use single-letter variables like
x,a, ortempunless you are doing basic math loops. A variable name should immediately tell anyone reading your code what is inside the box. - Good:
student_age = 25 - Bad:
a = 25(What doesastand for? You will forget by tomorrow!) - Keep it Concise: While descriptive is good, don't write a whole sentence for a variable name.
user_total_scoreis great;the_total_score_of_the_currently_active_user_playing_the_gameis way too long.
When you write clean, well-named variables, your code becomes self-documenting meaning other people (and your future self) can understand it at a single glance!
Lesson Summary
You have unlocked dynamic data storage! Let's review what you accomplished in this lesson:
- Variables as Containers: You learned to view variables as labeled storage boxes in your computer's memory rather than abstract math symbols.
- Assignment (
=): You mastered the assignment operator, understanding that it takes the value on the right and stores it inside the variable on the left. - Reassignment: You saw how variables can be updated and modified dynamically as a program runs.
- Technical Laws: You learned the hard rules of naming variables (no spaces, no starting with numbers, case-sensitivity, and avoiding reserved keywords).
- PEP 8 Best Practices: You adopted professional habits like using descriptive names and
snake_case.
With variables in your toolkit, your programs can finally start remembering information, tracking states, and acting like real software!