Think Python: How to Think Like a Computer Scientist

Start the Python interpreter and type help() to start the online help utility. Or you can type help('print') to get information about the print statement.

If this example doesn’t work, you may need to install additional Python documentation or set an environment variable; the details depend on your operating system and version of Python.

Practice using the Python interpreter as a calculator:

1 The volume of a sphere with radius r is 4/3 π r³. What is the volume of a sphere with radius 5? Hint: 392.7 is wrong!

2 Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?

If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again, what time do I get home for breakfast?


Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5.
Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.
>>> right_justify('allen')

A function object is a value you can assign to a variable or pass as an argument. For example, do_twice is a function that takes a function object as an argument and calls it twice:

def do_twice(f):

Here’s an example that uses do_twice to call a function named

print_spam twice.
def print_spam():
    print 'spam'


3 Type this example into a script and test it.

4 Modify do_twice so that it takes two arguments, a function object and a value, and calls the function twice, passing the value as an argument.

5 Write a more general version of print_spam, called print_twice, that takes a string as a parameter and prints it twice.

6 Use the modified version of do_twice to call print_twice twice, passing 'spam' as an argument.

7 Define a new function called do_four that takes a function object and a value and calls the function four times, passing the value as a parameter. There should be only two statements in the body of this function, not four.

If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are one inch long, it is clear that you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle:

If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can. (If the sum of two lengths equals the third, they form what is called a “degenerate” triangle.)

1. Write a function named is_triangle that takes three integers as arguments, and that prints either “Yes” or “No,” depending on whether you can or cannot form a triangle from sticks with the given lengths.

2. Write a function that prompts the user to input three stick lengths, converts them to integers, and uses is_triangle to check whether sticks with the given lengths can form a triangle.

To test the square root algorithm in this chapter, you could compare it with math.sqrt. Write a function named test_square_root that prints a table like this:
1.0 1.0                      1.0                                 0.0
2.0 1.41421356237   1.41421356237             2.22044604925e-16
3.0 1.73205080757 1.73205080757             0.0
4.0 2.0                      2.0                                 0.0
5.0 2.2360679775    2.2360679775               0.0
6.0 2.44948974278   2.44948974278             0.0
7.0 2.64575131106    2.64575131106             0.0
8.0 2.82842712475   2.82842712475             4.4408920985e-16
9.0 3.0                      3.0                                 0.0

The first column is a number, a; the second column is the square root of a computed with the function from Section 7.5; the third column is the square root computed by math.sqrt; the fourth column is the absolute value of the difference between the two estimates.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import math

print(">>> help('print')")


r = 5
V = (4.0/3)*math.pi*(r**2)
print("V =","{0:.2f}".format(V))

cost = (24.95*0.6+3)+59*(24.95*0.6+0.75)
print("Cost =","$"+"{0:.2f}".format(cost))

time = 6*60+52 + (8+15/60) + 3*(7+12/60) + (8+15/60)
time = int(time)
h = math.floor(time / 60)
m = time - h*60
print("Time =",str(h)+":"+str(m))


By purchasing this solution you'll be able to access the following files:
Case3-Exercises-YourFirstNameLastName.txt and

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats