Learn how to store information in Python using variables and the rules for naming them.
Imagine you are playing a video game and you find a legendary sword; how does the game remember you have it, what its name is, and how much damage it does even after you move to a new level?
In Python, a variable is like a labeled box or a backpack. It is a reserved memory location to store values. Instead of remembering a complex memory address, you give that location a name that makes sense to humans. To create a variable, you use the assignment operator, which is the equals sign . For example, if you write , you are telling the computer: 'Take the number and put it inside a box labeled .' From that point on, whenever you use the name , Python knows you are talking about the value .
1. Choose a name for your data: `gold_coins`. 2. Use the assignment operator: . 3. Provide the value: . 4. Full code: `gold_coins = 50`. 5. Now, if you type `print(gold_coins)`, the computer outputs .
Quick Check
In the line of code 'score = 10', which part is the variable name and which part is the value?
Answer
The variable name is 'score' and the value is 10.
Python is picky about names! To keep your code running smoothly, follow these rules: 1. Start Right: Names must start with a letter or an underscore (_). They cannot start with a number. 2. Characters: Names can only contain letters, numbers, and underscores (). No spaces or symbols like or . 3. Case Sensitivity: `Player` and `player` are two completely different variables! 4. Snake Case: Python developers use snake_case, where words are lowercase and joined by underscores (e.g., `user_high_score`). This makes long names easy to read.
Identify why these names are invalid: 1. `2nd_player`: Invalid because it starts with a number. 2. `user name`: Invalid because it contains a space. 3. `total$).
Quick Check
Is 'highScore' or 'high_score' the preferred naming style in Python?
Answer
high_score (snake_case) is the preferred Python convention.
The word variable comes from 'vary,' which means to change. You can change the value inside a variable at any time by assigning it a new value. When you do this, the old value is thrown away and replaced. This is essential for things like keeping score. If you have and the player earns more points, you can update it. Python evaluates the right side of the first, then stores the result back into the variable name on the left.
1. Start with an initial value: `xp = 100`. 2. The player defeats a boss and earns XP. 3. Update the variable: `xp = xp + 50`. 4. Python looks at the current `xp` (), adds , and gets . 5. It then stores back into `xp`. The old is gone!
Which of the following is a VALID Python variable name?
If and then you run , what is the new value of ?
In Python, the variable names 'User_Name' and 'user_name' are considered the exact same variable.
Review Tomorrow
In 24 hours, try to recall the three main rules for naming a variable in Python without looking at your notes.
Practice Activity
Open a Python editor and create three variables: one for your name, one for your age, and one for your favorite number. Then, try to update your age variable by adding 1 to it.