Home > Articles > Open Source > Python

  • Print
  • + Share This
Like this article? We recommend

Variables and Arithmetic Expressions

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

Listing 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:

1 1050.0
2 1102.5
3 1157.625
4 1215.50625
5 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 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 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 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:

1 1050.00
2 1102.50
3 1157.63
4 1215.51
5 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 printf() function.

  • + Share This
  • 🔖 Save To Your Account