Variables and Assignment
Part of: Programming Lab: Python for CSP
Boxes With Names A variable is a name that holds a value. You create one with an assignment statement , which uses a single equals sign. The name goes on the left and the value goes on the right. The first line stores the number 10 under the name score. The second stores the text "Ada" under the name name. When you print a variable, Python looks up its current value and shows it. Reassigning A variable can change. Assigning again replaces the old value with a new one. Read the middle line right side first. Python computes score + 5, which is 10 + 5, gets 15, then stores 15 back into score. The old 10 is gone. This pattern, using a variable's current value to compute its next value, shows up constantly in real programs. Naming Well Good names describe what the value means. total and player name tell a reader what is inside. Names like x or data say nothing. Clear names make code read almost like plain English. Key takeaways: - A variable is a name bound to a value. - Assignment uses one equals sign, with the name on the left. - Reassigning replaces the old value. - Descriptive names make code easier to read.
Challenge: Add Ten