Page 36 -
P. 36

2.3. Variable names and keywords                                             13

                           2.3   Variable names and keywords

                           Programmers generally choose names for their variables that are meaningful—they docu-
                           ment what the variable is used for.
                           Variable names can be arbitrarily long. They can contain both letters and numbers, but
                           they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to
                           begin variable names with a lowercase letter (you’ll see why later).
                           The underscore character, _, can appear in a name. It is often used in names with multiple
                           words, such as my_name or airspeed_of_unladen_swallow  .
                           If you give a variable an illegal name, you get a syntax error:

                           >>> 76trombones =  'big parade '
                           SyntaxError: invalid syntax
                           >>> more@ = 1000000
                           SyntaxError: invalid syntax
                           >>> class =  'Advanced Theoretical Zymurgy  '
                           SyntaxError: invalid syntax
                           76trombones is illegal because it does not begin with a letter.  more@ is illegal because it
                           contains an illegal character, @. But what’s wrong with class ?

                           It turns out that class is one of Python’s keywords. The interpreter uses keywords to
                           recognize the structure of the program, and they cannot be used as variable names.

                           Python 2 has 31 keywords:
                           and       del       from      not       while
                           as        elif      global    or        with
                           assert    else      if        pass      yield
                           break     except    import    print
                           class     exec      in        raise
                           continue  finally  is         return
                           def       for       lambda    try
                           In Python 3, exec is no longer a keyword, but nonlocal is.
                           You might want to keep this list handy. If the interpreter complains about one of your
                           variable names and you don’t know why, see if it is on this list.


                           2.4   Operators and operands


                           Operators are special symbols that represent computations like addition and multiplica-
                           tion. The values the operator is applied to are called operands.

                           The operators +, -, *, / and ** perform addition, subtraction, multiplication, division and
                           exponentiation, as in the following examples:
                           20+32   hour-1   hour*60+minute  minute/60  5**2   (5+9)*(15-7)
                           In some other languages, ^ is used for exponentiation, but in Python it is a bitwise operator
                           called XOR. I won’t cover bitwise operators in this book, but you can read about them at
                           http://wiki.python.org/moin/BitwiseOperators  .
                           In Python 2, the division operator might not do what you expect:
   31   32   33   34   35   36   37   38   39   40   41