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:

001 1050.0 
002 1102.5 
003 1157.625 
004 1215.50625 
005 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 ...

Get Python Essential Reference, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.