Solutions to Exercises

Chapter 1
Solutions to Exercises
Exercises
1.1 Find the sum of the numbers 8, 9, and 10.
>>> 8 + 9 + 10
27
1.2 Find the product of the numbers 8, 9, and 10.
>>> 8 * 9 * 10
720
1.3 Compute the number of seconds in a year.
>>> 365 * 24 * 60 * 60
31536000
1.4 Compute the number of inches in 1 mile.
>>> 5280 * 12
63360
1
2
CHAPTER 1. SOLUTIONS TO EXERCISES
1.5 Compute the number of 2 ft square tiles to cover the floor of a 10 by
12 ft room.
>>> 10//2 * 12//2
30
1.6 Compute the number of handshakes required to shake all the hands of
your classmates. Assuming that you have 18 classmates:
>>> 18 * 17 // 2
153
1.7 Find the average age of five people around you using integer division.
Double-check your answer.
>>> (18 + 19 + 19 + 20 + 24) // 5
20
1.8 Find the average age of five people around you using floating-point
division. Double-check your answer.
>>> (18 + 19 + 19 + 20 + 24) / 5
20.0
1.9 Find the volume of a sphere with a radius of 1 using the formula
4/3πr3 .
>>> 4/3 * 3.1415 * 1**3
4.1886666666666663
1.10 Compute 1/3 of 15. Did you get the right answer?
>>> 1/3 * 15
5.0
3
1.11 The Andromeda galaxy is 2.9 million light-years away. There are
5.878 × 1012 miles per light-year. How many miles away is the Andromeda galaxy?
>>> 2900000 * 5.878e12
1.70462e+19
1.12 How many years would it take to travel to the Andromeda galaxy at
65 miles per hour?
>>> 1.70462e19 / 65
2.6224923076923078e+17
>>> 2.6224923076923078e+17 / (365 * 24)
29937126800140.5
1.13 Compute the factorial of 13.
>>> fact = 1
>>> for i in range(1,14):
...
fact = fact * i
...
>>> fact
6227020800
1.14 Compute 2 to the 120th power.
>>> 2 ** 120
1329227995784915872903807060280344576
1.15 If the universe is 15 billion years old, how many seconds old is it?
1.16 How many handshakes would it take for each person in Chicago to
shake hands with every other person?
1.17 Given the following Python statements:
4
CHAPTER 1. SOLUTIONS TO EXERCISES
a = 79
b = a
a = 89
(a) Draw a reference diagram to show the labels and objects after
the first two statements.
79
a
b
(b) Draw a reference diagram to show the labels and objects after
the last statement.
79
a
b
1.18 Which of the following are legal variable names:
(a) abc123
(b) 123abc
(c) abc123
(d) 123
All except b are legal.
1.19 Consider the following statements:
89
5
a
b
c
d
=
=
=
=
10
20
a * b
a + b
6
CHAPTER 1. SOLUTIONS TO EXERCISES
Draw a reference diagram to show all the objects and names after
evaluating these statements
10
a
20
200
b
30
c
d
1.20 What are the values of a and b after Python evaluates each of the
following four statements?
a
b
a
b
=
=
=
=
>>>
>>>
>>>
>>>
>>>
20
>>>
15
10
20
b
15
a
b
a
b
a
b
=
=
=
=
10
20
b
15
7
1.21 Consider the following statements:
idx = 0
idx = idx + 1
idx = idx + 2
What is the value of idx after Python evaluates each of the three
statements?
>>>
>>>
>>>
>>>
3
idx = 0
idx = idx + 1
idx = idx + 2
idx
1.22 Create a turtle called sven. Now tell sven to go forward 10. What is
sven’s position now?
import turtle
sven = turtle.Turtle()
sven.forward(10)
svn.position()
1.23 Create a turtle called ole and tell ole to turn right 45 degrees and go
forward 50. Notice that you now have two turtles in the same window.
ole = turtle.Turtle()
sven.right(45)
sven.forward(50)
1.24 On a sheet of graph paper sketch out a simple line drawing of something. Using the turtle methods in Table 1.3, recreate your line drawing.
1.25 Modify the drawSquare function to draw a rectangle whose width is
twice
the
sideLength.
8
CHAPTER 1. SOLUTIONS TO EXERCISES
def drawSquare(myTurtle,sideLength):
myTurtle.forward(2*sideLength)
myTurtle.left(90)
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.forward(2*sideLength)
myTurtle.left(90)
myTurtle.forward(sideLength)
myTurtle.left(90)
1.26 Create a new function called drawRectangle that takes three parameters: myTurtle, width, and height.
def drawRectangle(myTurtle, width, height):
myTurtle.forward(width)
myTurtle.left(90)
myTurtle.forward(height)
myTurtle.left(90)
myTurtle.forward(width)
myTurtle.left(90)
myTurtle.forward(height)
myTurtle.left(90)
1.27 Suppose that in Session 1.8 we had used the expression import ds
instead of
from ds import *. Continue the rest of the session. Draw a reference
diagram that illustrates this session.
The Name space would be similar to figure 1.11 but would need a name
ds that points to the ds module. The function drawSquare would be
contained inside the ds module rather than in the global namespace.
1.28 Call your function as follows: drawRectangle(t,50,300).
1.29 Draw a reference diagram for the previous problem. Starting from
Figure 1.11 there would be a new name drawRectangle that points to
a function. The function would have parameter names corresponding
to myTurtle, width, and height.
1.30 Use the range function to create a sequence of the multiples of 5 up
to 50.
9
range(5,51,5)
1.31 Use the range function to create a sequence of numbers from −10 to
10.
range(-10,11,1)
1.32 Use the range function to create a sequence of numbers from 10 to
−10.
range(10,-11,-1)
1.33 Modify the spiral function to turn more than 90 degrees for each iteration.
def drawSpiral(myTurtle, maxSide):
for sideLength in range(1,maxSide+1,5):
myTurtle.forward(sideLength)
myTurtle.right(140)
1.34 Modify the spiral function to turn less than 90 degrees for each iteration.
def drawSpiral(myTurtle, maxSide):
for sideLength in range(1,maxSide+1,5):
myTurtle.forward(sideLength)
myTurtle.right(75)
1.35 Modify the spiral function to use the loop variable as the number of
degrees to turn.
def drawSpiral(myTurtle, maxSide):
for sideLength in range(1,maxSide+1,5):
myTurtle.forward(sideLength)
myTurtle.right(sideLength)
10
CHAPTER 1. SOLUTIONS TO EXERCISES
1.36 Modify the spiral function to use a second turtle and create two spirals
in opposite directions.
def drawSpiral(turtle1, turtle2, maxSide):
for sideLength in range(1,maxSide+1,5):
turtle1.forward(sideLength)
turtle2.forward(sideLength)
turtle1.right(140)
turtle2.left(140)
1.37 Write a function drawTriangle that takes two side lengths and an
angle. The function should first calculate the length of the third side
and then draw the triangle.
Using the law of cosines c2 = a2 + b2 − 2ab cos θ where θ is the angle
between the two known sides a, and b we can come up with the length
of side c. Now we need to use the law of sines to find the other interior
angles. a/ sin α = b/ sin β = c/ sin γ Note: This is a more challenging
program than some students may be ready for at this point. But would
still make a good in-class example for the instructor.
import turtle
import math
def drawTriangle(myTurtle,a,b,angle):
c = math.sqrt(a*a + b*b - 2*a*b*math.cos(math.radians(angle)))
alpha = math.degrees(math.asin(a / (c/math.sin(math.radians(angl
print(alpha)
myTurtle.forward(a)
myTurtle.right(180-angle)
myTurtle.forward(b)
myTurtle.right(180-alpha)
myTurtle.forward(c)
wn = turtle.Screen()
t = turtle.Turtle()
drawTriangle(t,100,150,40)
wn.exitonclick()
11
1.38 Write a function that draws a series of 10 squares, with each square
being 5 pixels smaller on each side. The squares should all start in the
same location.
def drawSquare(myTurtle,sideLength):
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.forward(sideLength)
myTurtle.left(90)
myTurtle.forward(sideLength)
myTurtle.left(90)
def drawNestedSquares(myTurtle):
for i in range(200,200-5*10,-5):
drawSquare(myTurtle,i)
1.39 Redo the last question so that the squares are all centered.
def drawNestedSquares(myTurtle):
for i in range(200,200-5*10,-5):
drawSquare(myTurtle,i)
myTurtle.up()
myTurtle.forward(2.5)
myTurtle.left(90)
myTurtle.forward(2.5)
myTurtle.right(90)
myTurtle.down()
1.40 Use the turtle to plot the function y = x2 .
def plotParabola(myTurtle):
myTurtle.up()
myTurtle.goto(-20,400)
myTurtle.down()
for x in range(-20,21,1):
12
CHAPTER 1. SOLUTIONS TO EXERCISES
y = x**2
myTurtle.goto(x,y)
Note: This exercise could be greatly expanded on to include some simple scaling calculations in order to draw the plot over a wider range. It
could also be expanded to add parameters for minimum and maximum
x values.
1.41 Use the turtle to plot the function y =
x
2
+3
def plotSimple(myTurtle):
myTurtle.up()
startx = -100
endx = 101
myTurtle.goto(startx,startx/2+3)
myTurtle.down()
for x in range(startx,endx,1):
y = x/2.0+3
myTurtle.goto(x,y)
1.42 Modify the drawCircle function so that the circle is drawn with the
center at the turtle’s present position.
def plotCircle(myTurtle,radius):
circumference = 2 * 3.1415 * radius
sideLength = circumference / 360
myTurtle.up()
myTurtle.left(90)
myTurtle.forward(radius)
myTurtle.right(90)
myTurtle.down()
drawPolygon(myTurtle,sideLength,360)
myTurtle.up()
myTurtle.right(90)
myTurtle.forward(radius)
myTurtle.left(90)
myTurtle.down()
13
1.43 The drawCircle function is somewhat inefficient: for small circles 360
sides is really overkill and for very large circles 360 sides might be too
few. See if you can devise a way to make the number of sides and
turning angle dependent on the radius so that smaller circles use fewer
sides and larger circles use more.