Lesson · Python for Beginners
Variables and assignment
A variable lets a program refer to a value by a name that explains its role.
What you will learn
- Create a variable with assignment
- Choose readable names
- Update a value and print it
A name refers to a value
In Python, an assignment statement evaluates the expression on its right and binds the resulting value to the name on its left. The equals sign is an instruction to assign, not a claim that two sides are mathematically identical. A later statement can use the name instead of repeating the original value.
Choose useful names
Names may contain letters, digits, and underscores, but a name cannot begin with a digit or contain spaces. Use words that describe what a value means in the program. Names are case-sensitive, so score and Score are different names. Python keywords such as if and for already have grammatical meaning and cannot be used as variable names.
Assignment can happen again
Assigning a new value to an existing name changes what that name refers to from that point onward. Earlier output is not retroactively changed. When an expression uses names, Python looks up their current values at the time that statement runs, which makes the order of statements important.
Build a sentence from values
The example uses an f-string to place the current value of a name inside text. The f before the opening quote tells Python to evaluate expressions inside braces. This is useful for readable output, but it does not alter the stored value. Keep calculation and presentation separate when a program grows.
learner = 'Mina'
lessons = 3
print(f'{learner} completed {lessons} lessons.')
Expected output
Mina completed 3 lessons.
Practice exercise
Store a city and a number of study minutes in two variables, then print one sentence using both.
Show a hint
Use descriptive names and an f-string.
Show one possible solution
For example, city = 'Pune'; minutes = 25; print(f'I studied in {city} for {minutes} minutes.') uses both current values.
Quick check
A single equals sign performs assignment in this statement; comparison uses a different operator.