Python Names and Assignment: Store Values
Give a value a name
Assignment binds a name to a value. In price = 8, Python evaluates the right side and binds the name price to the number eight. You can then use the name in an expression. The equals sign here is an assignment symbol, not a mathematical claim that both sides are forever equal.
The names make the calculation easier to read. Python looks up the current values of price and quantity, multiplies eight by three and displays 24. Names are case-sensitive: price and Price are different names. Prefer descriptive names with underscores between words when needed.
Update a value by rebinding a name
The second line first reads the old value, four, and calculates six. It then binds count to six. The output is 6. This is not a permanent equation or an automatic link to an earlier calculation.
An earlier result does not recalculate itself
quantity = 2total = quantity * 5quantity = 4print(total)total = quantity * 5print(total)The first printed value is still 10. Changing quantity does not automatically run the earlier assignment to total again. The new assignment recomputes the result, so the second printed value is 20. Assign a name before using it; an undefined name causes an error.
Practice with a running total
Begin with stock = 12, add four new items and display the updated count. In the coding challenge keep the supplied function wrapper and return the updated value instead of printing it. The grader supplies the starting number.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Names, assignments and updates
Check your understanding of names and assignment.
What does price = 7 do?
What is printed?
count = 4count = count + 5print(count)What is printed?
x = 2total = x * 4x = 3print(total)Which name is bound by the statement stock_count = 3?
Which assignment adds six to the current total and preserves the new result under the same name?