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. What usually goes wrong. What you see What caused it How to fix it --- --- --- TypeError: can only concatenate str (not "int") to str The value read from input was still text when a number was added to it Convert it to a whole number before doing any arithmetic NameError: n

Challenge: Add Ten