O'Reilly logo

Python Programming in Context, 2nd Edition by David L. Ranum, Bradley N. Miller

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

“91974˙CH02˙final” 2012/12/14 13:58 page 45 #1
CHAPTER
2
othon
2.1 Objectives
To understand how computers can help solve real problems
To further explore numeric expressions, variables, and assignment
To understand the accumulator pattern
To utilize the math library
To further explore simple iteration patterns
To understand simple selection statements
To use random numbers to approximate an area
2.2 What Is Pi?
In this chapter we will continue to explore computer science, problem solving, and the
Python programming language by considering one of the most famous numbers of all: the
number pi, often represented by the Greek letter π. Almost everyone has at one time or
another used the value pi. In fact, we used π in Chapter 1 in our circle calculations.
Pi is defined to be the ratio of a circle’s circumference to its diameter (see Figure 2.1).
This relationship, π = C/d, gives rise to the familiar equation C = πd used to compute
the circumference of a circle given the diameter. Since the diameter of a circle is twice the
radius, this can also be written C =2πr where r is the radius.
45
“91974˙CH02˙final” 2012/12/14 13:58 page 46 #2
46 CHAPTER 2
π
thon
C
d
Figure 2.1 Pi—the ratio of circumference (C ) to diameter (d)
Other common formulas that utilize pi include the area of a circle with radius r, A = πr
2
,
the volume of a sphere with radius r, V =
4
3
πr
3
, and the surface area of a sphere with
radius r, S =4πr
2
. In this chapter, we are more interested in the value of pi itself, not in
the way that pi is used.
The value of pi has been a matter of interest for thousands of years. Writings from ancient
Egypt and Babylon as well as the Bible contain references to this mystical number. In
your math class, it is likely that you were told to use 3.14 or 3.14159 as the value of pi. In
fact, these values worked fairly well for many of the problems that you were asked to solve.
However, it turns out that the exact value of pi is not as simple as this would make it seem.
Pi is known in mathematics as an irrational number. This means that pi is a floating-point
number with an infinite, nonrepeating pattern of decimal digits. The actual value of pi is
3.14159265358979323846264338327950288419716939937510...
where the ... indicates the infinite digits.
Because pi cannot be stated exactly, the best that we can do is provide an approximation,
for example 3.14. Other usual approximations are
22
7
,
355
113
, and the more complex
9801
2206
2
.

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