Linux/Unix > Python Essential Reference, Second Edition > 1. A Tutorial Introduction > Variables and Arithmetic Expressions | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137110163131203146201222029

### Variables and Arithmetic Expressions

The program in Listing 1.1 shows the use of variables and expressions by performing a simple compound-interest calculation:

##### Listing 1.1 Simple Compound-Interest Calculation

    principal = 1000        # Initial amount
    rate = 0.05             # Interest rate
    numyears = 5            # Number of years
    year = 1
    while year <= numyears:
            principal = principal*(1+rate)
            print year, principal
            year += 1

The output of this program is the following table:

    1050.0
    1102.5
    1157.625
    1215.50625
    1276.2815625

Python is a dynamically typed language in which names can represent values of different types during the execution of the program. In fact, the names used in a program are really just labels for various quantities and objects. The assignment operator simply creates an association between that name and a value. This is different from C, for example, in which a name represents a fixed size and location in memory into which results are placed. The dynamic behavior of Python can be seen in Listing 1.1 with the `principal` variable. Initially, it’s assigned to an integer value. However, later in the program it’s reassigned as follows:

    principal = principal*(1+rate)

This statement evaluates the expression and reassociates the name `principal` with the result. When this occurs, the original binding of `principal` to the integer `1000` is lost. Furthermore, the result of the assignment may change the type of a variable. In this case, the type of `principal` changes from an integer to a floating-point number because `rate` is a floating-point number.

A newline terminates each individual statement. You also can use a semicolon to separate statements, as shown here:

    principal = 1000; rate = 0.05; numyears = 5;

The `while` statement tests the conditional expression that immediately follows. If the tested expression is true, the body of the `while` statement executes. The condition is then retested and the body executed again until the condition becomes false. Because the body of the loop is denoted by indentation, the three statements following the `while` in Listing 1.1 execute on each iteration. Python doesn’t specify the amount of required indentation, as long as it’s consistent within a block.

One problem with the program in Listing 1.1 is that the output isn’t very pretty. To make it better, you could right-align the columns and limit the precision of `principal` to two digits by modifying the `print` to use a format string, like this:

    print "%3d %0.2f" % (year, principal)

Now the output of the program looks like this:

    1050.00
    1102.50
    1157.63
    1215.51
    1276.28

Format strings contain ordinary text and special formatting-character sequences such as `"%d"`, `"%s"`, or `"%f"`. These sequences specify the formatting of a particular type of data such as an integer, string, or floating-point number, respectively. The special-character sequences can also contain modifiers that specify a width and precision. For example, `"%3d"` formats an integer right-aligned in a column of width 3, and `"%0.2f"` formats a floating-point number so that only two digits appear after the decimal point. The behavior of format strings is almost identical to the C `sprintf()` function and is described in detail in Chapter 4, “Operators and Expressions.”

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.