Skip to Content
Python: Essential Reference, Third Edition
book

Python: Essential Reference, Third Edition

by David Beazley
February 2006
Intermediate to advanced content levelIntermediate to advanced
648 pages
14h 53m
English
Sams
Content preview from Python: Essential Reference, Third Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python: Essential Reference

Python: Essential Reference

David M. Beazley

Publisher Resources

ISBN: 0672328623Purchase book