Variables and Assignment

Variables and Assignment

Part of: Variables, Lists & Procedures

Every program needs a way to remember things, a player's score, a user's name, the running total of a bill. In AP CSP that memory comes from variables . A variable is a named box that holds a single value, and the value can change as the program runs. Assignment You put a value into a variable using an assignment statement . In Python the operator is =, read as "gets" or "is set to", not "equals" in the math sense. The right side is evaluated first , then the result is stored in the name on the left. So score = score + 10 looks at the current value of score, adds 10, and writes the new value back. The old value is overwritten. On the AP exam the equivalent pseudocode is a ← expression. The arrow makes the direction obvious: the box on the left receives the value. Naming and data types - Variable name : a label you choose. Use clear names like total, userAge, firstName. - A variable's value has a data type . Common ones: integers (42), floating-point numbers (3.14), strings ("hi"), and Booleans (True/False). - Reassigning a variable replaces its value; the variable does not remember its history. Reading input Programs become useful when they react to data they did not know in advanc

Challenge: Running Total