A variable is a name that refers to a value. Use one whenever a program needs to remember a value for later, such as a function result or a user’s name. section 4.3.2 section 3.1.2
In Python, = assigns the value calculated on the right to the name on the left. It means assignment, not mathematical equality, and a later assignment can change what the variable refers to. section 4.3.2
Variables commonly help you:
- Store a function’s returned value so it can be printed or used later. section 3.1.2
- Hold input, such as text entered by a user. section 4.3.1
- Update a value with assignments such as age = age + 5, age += 5, or age *= 2. section 4.3.2
Remembering and changing a value
age = 20 + 4 age = age + 5 print(age) # 29 The first assignment gives age the value 24. The second calculates 24 + 5 and assigns 29 to age. Simply evaluating age + 3 would not change age; another assignment is required. section 4.3.2
# call the larger function with the values 3 and 5 # store the result in a variable called result # then print result result = larger(3, 5) #1 print(result)
See a variable capture a function’s result before print displays it. section 3.1.2










