LESSON 3 / 5 · 7 MIN
Variables
LET'S MAKE IT CLICK
Name your values
A variable is a name that refers to a value. Names let you reuse values and explain what your program is working with.
A number to work with.
Give that value a meaningful label.
Use the name to retrieve the current value.
A variable is a name that refers to a value.
Move through one idea at a time. Watch, experiment, and check your understanding along the way.
Give a value a name
Read the right-hand side.
Attach the name to the value.
print(score) displays that value.
Assignment evaluates the value before binding the name.
In score = 10, Python evaluates the value on the right, then binds the name score to it. Reading score later gives you the value it currently refers to.
See the Python code
score = 10
print(score)Keep in mind
A useful beginner picture is a label attached to a value. Python variables are names referring to objects, rather than fixed typed boxes.
Change what the name refers to
Read the current score.
Evaluate the right-hand side.
Rebind score to the new value.
The old value is used in the calculation before the binding changes.
Assigning again changes the binding. In score = score + 5, the right-hand side uses the current score, calculates a new number, then assigns that result back to score.
Assignment is not a mathematical equality test. A single = assigns; == compares two values.
YOUR TURN TO EXPLORE
Watch a score change, line by line.
Choose a starting score and an increase. Step through the assignments and watch the current binding update.
No assignments yet.
OUTPUT
No output yet.
Predict the result, then take the first step.
Choose names that explain
What does x represent?
The name tells you what the value means.
Digits are allowed after the first character.
Names should explain their purpose. total_score is valid; 2scores is not.
Use names such as total_price or user_name instead of mysterious letters when they clarify the purpose. Python names can contain letters, digits, and underscores, but cannot start with a digit or be a keyword such as for.
Names are case-sensitive: score and Score are separate names. Use lowercase words separated by underscores for ordinary variables.
See the Python code
user_name = "Asha"
lesson_count = 5Names are not copies
a refers to a list object.
b refers to that same list, without copying it.
There are two names and one list.
Giving an object another name is different from creating a copy.
For immutable numbers, reassignment gives a name a different value. With mutable objects such as lists, several names can refer to the same object; changing that object is different from rebinding a name.
We will explore shared references later. For now, distinguish creating a value, assigning a name, and reading that name.
TAKE IT WITH YOU
What you have learned
- Assignment evaluates the right-hand side before updating the name.
- Reassignment changes the value a name refers to.
- Descriptive, case-sensitive names make code easier to read.
Answer both quick checks correctly to mark this lesson complete. You can continue at any time.