Python remembers strings and numbers by assigning them to a “variable” with a relevant name. For example, a variable can be used to remember a long string:
In the example above
myname
is the variable. It has a relevant name, and no quotes- The
=
operator is used to assign a value (string or number) to the variable - The value of a variable can be reassigning, ie. it can vary (hence variable)
Variables can also be used to store and remember numbers, and they can be used to do calculations:
Variable names
Python variables can be called almost anything you want, with the following rules:
- Python variables must not include a space or other punctuation symbols, except the underscore character _ (sometimes used to represent a space), eg. my_name
- Must not begin with a number (0-9), eg. car100 is ok, but 100car is not
- Must not be a reserved word, ie. an existing Python command, such as print.
Variable Keywords
- assignment: defining a variable with a value, using the = operator, eg. myname = “BugZoid”
- variable: a way to represent, store and remember a value