{article Dive into Python}{title} {text}{/article}

Python has local and global variables like most other languages, but it has no explicit variable declarations.

Variables spring into existence by being assigned a value, and they are automatically destroyed when they go out of scope.

Python will not allow you to reference a variable that has never been assigned a value; trying to do so will raise an exception.

You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

That the variable assignment is one command split over several lines, with a backslash ("\") serving as a
line−continuation marker.

Strictly speaking, expressions in parentheses, straight brackets, or curly braces (like defining a dictionary) can be split into multiple lines with or without the line continuation character ("\"). I like to include the backslash even when it's not required because I think it makes the code easier to read, but that's a matter of style.

Referencing an Unbound Variable

Assigning Multiple Values at Once

v = ('a', 'b', 'e')

  1. (x, y, z) = v
  1. v is a tuple of three elements, and (x, y, z) is a tuple of three variables. Assigning one to the other
    assigns each of the values of v to each of the variables, in order.
    This has all sorts of uses. I often want to assign names to a range of values. In C, you would use enum and manually list each constant and its associated value, which seems especially tedious when the values are consecutive. In Python, you can use the built−in range function with multi−variable assignment to quickly assign consecutive values.

Assigning Consecutive Values

  1. range(7)
  2. (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
  3. >>> MONDAY
  4. 0
  1. The built−in range function returns a list of integers. In its simplest form, it takes an upper limit and returns a zero−based list counting up to but not including the upper limit. (If you like, you can pass other parameters to specify a base other than 0 and a step other than 1. You can print range.__doc__ for details.)
  2. MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, and SUNDAY are the variables you're defining. (This example came from the calendar module, a fun little module that prints calendars, like the UNIX program cal. The calendar module defines integer constants for days of the week.)
  3. Now each variable has its value: MONDAY is 0, TUESDAY is 1, and so forth.

Autocompletion

A handy short cut. Idle remembers all the variables you have defined at any moment. This is
handy when editing. Without pressing Enter, type into the Shell just
f
Then hold down the Alt key and press the / key. This key combination is abbreviated Alt-/. (On a Mac, that may give you a funny character: In that case you need to hold down both the control key and the alt/option key when pressing the ‘/’. This may hold in other places the Alt key is called for in Windows.)
You should see f autocompleted to be
first