The Tutorial:
 
 


    Lesson 2: Variables in Python

    Now that we have seen how a simple CGI program works, let's inspect how variables work in Python.  All assignments have the syntax identifier = value.  Identifier names can be begin with either an underscore or letter followed by any combination of letters, numbers, and underscores.  In addition, names are case sensitive (i.e., name is different from Name).  Variables have dynamic type in Python, so there is no need to specify types in declarations [4].

    Table 2.1 shows the variable types of Python with examples of each.

    Table 2.1: Built-in Object Types [2: 28]
    TypeExample
    Numbers
    Strings
    Lists
    Dictionaries
    Tuples
    Files
    12345, 1.5, 2e+10, 2e-10, 2+4j
    "Python", "CGI"
    ['this', ['is', 1], 'example']
    {'Python':'good', 'CGI':'useful'}
    (1, 'two', 3, 'IV')
    input_file = open('data.txt','r').read()

    The code examples below demonstrate various assignment methods and results.

    Code Example 2.1: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Initialize a few variables.
    first = 5
    middle = 7.5
    last = 10

    # Print the values.
    print "first:", first
    print "middle:", middle
    print "last:", last

    Multiple assignments can be done at once.  Python handles a = b = c assignments in a right associative manner. In addition, Python performs a swap operation on the assignment:

    identifier_1, identifier_2  =  identifier_2, identifier_1

    Code Example 2.2: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"


    # Initialize a few variables.
    first, middle, last = 5, 7.5, 10

    # Print the values.
    print "first:", first
    print "middle:", middle
    print "last:", last, "\n"

    # Swap the values.
    first, last = last, first

    # Print the values.
    print "first:", first
    print "last:", last, "\n"

    # Right associative assignment.
    first = last = middle

    # Print the values.
    print "first:", first
    print "middle:", middle
    print "last:", last

    Python contains various operators for number, string, and list operand types.  Table 2.2 shows the most common.

    Table 2.2: Python Operators [2: 30]
    OperatorDescription
    or
    and
    not
    ==, !=, <, <=, >, >=, <>
    in, not in
    +, -
    *, /
    %
    a[i], a[i:n], a.b
    Logical OR
    Logical AND
    Logical NOT
    Comparison
    Sequence Membership
    Addition/String Concatenation, Subraction
    Multiplication/String Repetition, Division
    Modulus
    Indexing, Slicing, Qualification

    Code Example 2.3 demonstrates the functionality of a few of the operators above.  Pay close attention to the dynamic typing as a result of integer and decimal value calculations. Note that specifying a negative index works like counting back from the end of the string.

    Code Example 2.3: 
    #!/usr/bin/python

    # Print the required header that tells the browser how to render the text.
    print "Content-Type: text/plain\n\n"

    # Integer division resulting in a truncated value.
    a = 5/2

    # Print the value.
    print "5/2 =", a

    # Decimal division.
    a = 5/2.0

    # Print the value.
    print "5/2.0 =", a, "\n"

    # String addition and multiplication.
    python_words = "python is" + 3*" really" + " neat."

    # Print the string.
    print python_words, "\n"

    # Print parts of the string.
    print "The first character is:", python_words[0]
    print "The last character is:", python_words[-1]
    print "In the middle is: \n", python_words[1:-1]

    So far, we have covered numbers and strings fairly well.  As we use more advanced features of the Python language, we will demonstrate the capabilities of lists, dictionaries, tuples, and files.

    To Lesson 3 ...

 © 2000 Department of Computer Science, University of Virginia