Variables are stored with assignment statements.
An assignment statement consists of a variable name and an assignment operator (equal sign).
spam = 42
Variables are initialized the first time a value is stored in it. When a variable is assigned a new value the old value is overwritten.
>>> eggs = 'red'
>>> eggs
'red'
>>> eggs = 'green'
>>> eggs
'green'
Variable Names #
A good variable name describes the data it contains.
Variable names must follow these three rules:
- One word no spaces
- Only use letters, numbers and the
_
character. - Can’t begin with a number
Variables are case sensitive. It’s python convention to start your variables lowercase.
Use either camelCase or underscore_variables. PEP 8 specifies underscores, but consistency is more important than which style you choose.
A foolish consistency is the hobgoblin of little minds
Variables and References #
Variables store a reference to the data not the actual data itself. References can be copied to other variables with things like
spam = ['eggs', 42, 'bacon']
cheese = spam
See: references