Chapter 3

Basic Arithmetic with Python

Simple is Better than Complex.

—Tim Peters


Basic math
Figure 1.
An abacus
Figure 2.
Limits to integers
Figure 3.
More limits
Figure 4.
More limits
Figure 5.
Python longs
Figure 6.
Large floats
Figure 7.
Figure 8.
More division
Figure 9.
Division, modulo
Figure 10.
More modulo
Figure 11.
Figure 12.
Floor() and ceil()
Figure 13.
Two’s complement arithmetic
Figure 14.
A googol
Figure 15.
The importance of parentheses
Figure 16.
Putting it all together
Figure 17.

Example Programs

There are no example programs for chapter 3, but there is a text file: download chapter3.zip.


If you do happen to discover that you care about such things as floating-point accuracy and the loss of precision caused by computer representation of floating-point numbers, then by all means check out Donald Knuth’s The Art of Computer Programming, particularly Volume 2, Seminumerical Algorithms, chapter 4. It includes lots of history.

Art of Computer Programming, Volume 2

If the topic of large numbers like the googol interests you, here are a few web sites to investigate. Some of the algorithms given are amenable to implementation in Python. You can find some of them at this book’s web site.

   1. Large Finite and Infinite Numbers: http://www.sci.wsu.edu/math/faculty/hudelson/moser.html
   2. How Much is a Gazillion?: http://www.straightdope.com/mailbag/mgazilli.html
   3. How to Get a Googolplex: http://www.informatik.uni-frankfurt.de/~fp/Tools/GetAGoogol.html
   4. Names of large and small numbers: http://www.uni-bonn.de/~manfear/numbers_names.php
   5. Googolplex: http://www.informatik.uni-frankfurt.de/~fp/Tools/Googool.html (best)
   6. Ridiculously Enhanced Pi Page: http://www.exploratorium.edu/learning_studio/pi/
   7. Mayan Time Periods and Period Glyphs: http://www.pauahtun.org/Calendar/calglyph.html

Find out how big a number it takes to crash Python. How big must a number be to crash your computer? (Back up all your files first.) 

Valid HTML 4.01 Transitional