Sunday, June 13, 2010

Variables — Names for Values

 Assigning Values to Names

These names are commonly called variables, which indicates that the data to which they refer can vary
(it can be changed), while the name remains the same. You’ll see them referred to as names, as well,
because that is what you are presented with by Python.
     >>> first_string = “This is a string”
     >>> second_string = “This is another string”
     >>> first_number = 4
     >>> second_number = 5
     >>> print “The first variables are %s, %s, %d, %d” % (first_string, second_string,
     first_number, second_number)
     The first variables are This is a string, This is another string, 4, 5


Altering Named Values
Every operation you’ve learned for numbers and strings can be used with a variable name so that you
can treat them exactly as if they were the numbers they referenced:
    >>> proverb = “A penny saved”
    >>> proverb = proverb + “ is a penny earned”
    >>> print proverb
    A penny saved is a penny earned

Names You Can’t Use and Some Rules
 Python uses a few names as special built-in words that it reserves for special use to prevent ambiguity.
 The following words are reserved by Python and can’t be used as the names for data:
     and, assert, break, class, continue, def, del, elif, else, except, exec, finally,
     for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return,
     try, while, yield

No comments:

Post a Comment