O'Reilly logo

Python: Essential Reference, Third Edition by David Beazley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

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 a program. In fact, the names used in a program are really just labels for various quantities and objects. The assignment operator ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required