A variable name is a label that allows your code to refer to a value. Think of it as a named piece of information rather than a box that permanently owns one number.
Understanding the Concept
When you write price = 12, Python binds the name price to the integer object 12. You can later rebind price to another value. A descriptive name helps you remember what that value means. Beginners commonly start with names representing quantities, people, totals, and flags. A variable should describe the role of its value in the program, not just its current contents.
When and Why You Would Use This
Use an inventory calculation. The unit_price and quantity names help the multiplication expression explain itself. Contrast that with vague names such as a and b, which make the same arithmetic harder to understand.
Practical Example
unit_price = 14
quantity = 3
inventory_value = unit_price * quantity
print("Inventory value:", inventory_value)
Understanding the Example
The result is 42. The names make it clear why the numbers are multiplied. The variables do not need type declarations for this introductory program.
Example Output
Inventory value: 42
Try It Yourself
Rename quantity to item_quantity and inventory_value to stock_total. Confirm that the calculation still works.
Common Beginner Mistakes
- Using generic one-letter names in a beginner project with several values.
- Expecting variable names to change their values automatically when related values change.
- Using a name before it has been assigned.
Key Takeaways
- Variable names let code refer to values.
- Choose names that explain the business meaning of a value.
- Expressions can combine named values into new results.