Variables are data stored in computer memory to be used later. They are called variables because they are able to be varied (changed.) Variables are useful when a certain value will be different each time you run the program, or change over the course of running the program.
To store a variable in memory, use the assignment operator, which is the equal sign, = , and this syntax:
variable_name = variable_value
*Note: Order matters! You put the name on the left, and the value to be assigned on the right!*
Here's an example where we store 3 pieces of information about Bob in memory:
first_name = "Bob"
age = 25
is_married = False
To use a variable after it is defined, just use its name (NO QUOTES) any time you would use something of the same data type.
For example:
print("Your name is")
print("Bob")
could be replaced with:
print("Your name is")
print(first_name)
Numeric variables (ints and floats) can be used in calculations:
25 + 1
could be replaced with:
age + 1
To change a variable's stored value, simply store a different value.
For example, the following code will print Robert, then Bob:
first_name = "Robert"
print(first_name)
first_name = "Bob"
print(first_name)
For now you need to know 4 data types and their abbreviations: