Link Search Menu Expand Document

Variables

Table of contents

  1. Description
  2. Assignment Statements
  3. Naming Rules
  4. Reassignment

Description

Variables are named areas of memory that we can use to store data. The Python interpreter categorizes all variables as Objects. All objects have three things: a name, a type, and a value.

Assignment Statements

Assignment statements bind a value with a name. Variable names must be to the left of the assignment operator; values must be to the right:

  • flavor = 'chocolate chip'
  • 'chocolate chip' = flavor

Naming Rules

  • Python PEP8: Variable Names
  • No spaces.
  • First character must be either an alphabetic character or an underscore.
  • Can only contain alphabetic characters, numeric characters, or underscores.
  • Cannot be a reserved word:
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not or
False class from  

Reassignment

When a value is bound to a variable, it is not permanent. Python code is executed sequentially, and keeps no record of prior variable values:

Watch on PythonTutor