Variables

A variable is the name used to access a value, a lot like the variables in algebra. Python has one variable type, called a reference. The easiest way to see this is in the Idle debugger.

Variables must start with a letter (uppercase or lowercase) or an underscore. This may be followed by any amount of letter, digits or underscores. (Variables with leading and trailing underscores, like __import__, are reservered for the language.) These are all valid variable names

You assign a reference using the = statement (not to be confused with the == operator). You can assign it any object.

Code:


p = "Potato"   # p now refers to the string "Potato"
bushels = 3    # bushels refers to the value 3
potatoes_per_bushel = 80  # potatoes_per_bushel refers to the value 80

total_potatoes = bushels * potatoes_per_bushel   # 240

print "I have", total_potatoes, p

Output:


I have 240 Potato

This page based on Variables


Andrew Dalke
Last modified: Tue Feb 3 12:27:27 SAST 2004