• Home
  • Textbooks
  • The Practice of Computing Using Python
  • Beginnings

The Practice of Computing Using Python

William F. Punch, Richard Enbody

Chapter 2

Beginnings - all with Video Answers

Educators


Chapter Questions

01:15

Problem 1

What is a program?

Adam Conner
Adam Conner
Numerade Educator
01:53

Problem 2

Python is an interpreted language. What does interpreted mean in this context?

Adam Conner
Adam Conner
Numerade Educator

Problem 3

What is a Python comment? How do you indicate a comment? What purpose does it serve?

Check back soon!

Problem 4

What is a namespace in Python?

Check back soon!

Problem 5

Whitespace:
(a) What is whitespace in Python?
(b) When does whitespace matter?
(c) When does whitespace not matter?

Check back soon!
00:35

Problem 6

Explain the difference between a statement and an expression. Give an example of both, and explain what is meant by a statement having a side effect.

Kerry Thornton-Genova
Kerry Thornton-Genova
Numerade Educator
01:13

Problem 7

Mixed operations:
(a) What type results when you divide an integer by a float? A float by an integer?
(b) Explain why that resulting type makes sense (as opposed to some other type).

Ernest Castorena
Ernest Castorena
Numerade Educator
00:33

Problem 8

Consider integer values of $\mathrm{a}, \mathrm{b}$, and $\mathrm{c}$ and the expression $(\mathrm{a}+\mathrm{b}) * \mathrm{c}$. In mathematics, we can substitute square brackets, [ ], or curly braces, {} , for parentheses, ( ). Is that same substitution valid in Python? Try it.

Nick Johnson
Nick Johnson
Numerade Educator
02:23

Problem 9

Write a Python program that prompts for a number. Take that number, add 2 , multiply by 3 , subtract 6 , and divide by 3 . You should get the number you started with.

James Kiss
James Kiss
Numerade Educator
01:45

Problem 10

A nursery rhyme: As I was going to St. Ives, I met a man with seven wives. Every wife had seven sacks, and every sack had seven cats, and every cat had seven kittens. Kittens, cats, sacks, and wives, how many were going to St. lves? There are interesting aspects to this puzzle, such as who is actually going to St. Ives. For our purposes, assume that everyone and everything is headed to St. Ives. Write a program to calculate that total.

Hunza Gilgit
Hunza Gilgit
Numerade Educator
01:15

Problem 11

Assignment:
my_int $=5$
my_int $=$ my_int +3
print (my-int)
(a) If you execute the three lines of code, what will be printed? Explain your answer using the rules of assignment.
(b) Rewrite my_int $=$ my_int +3 using the $+=$ symbol.

Willis James
Willis James
Numerade Educator
05:13

Problem 12

Assignment:
my_var $1=7.0$
my_var2 $=5$
print (my_var1 of my_var2)
If you execure these three lines of code, what will be printed?

Kumareshwaran Rathinasabapathy
Kumareshwaran Rathinasabapathy
Numerade Educator
01:21

Problem 13

Try to predict what will be printed by the following:
$$
\begin{aligned}
& x=4 \\
& y=5 \\
& \text { print }(x / / y)
\end{aligned}
$$

Kimberly Konzack
Kimberly Konzack
Numerade Educator
03:44

Problem 14

Given the expression $30-3 * * 2+8 / / 3 * 2 * 10$,
(a) What is the output of the expression? (You can check your answer in the Python shell.)
(b) Based on precedence and associativity of the operators in Python, correctly parenthesize the expression such that you get the same output as above.

Natalie Euley
Natalie Euley
Numerade Educator
01:04

Problem 15

One example expression was $2+3 * 5$, the value of which was 17. Using the same expression, include parentheses to arrive at a different value for the expression.

Leslie Deeb
Leslie Deeb
Numerade Educator
08:29

Problem 16

Predict the output (check your answer in the Python shell):
(a) $2 * * 2 * * 3$
(b) $2 * \star(2 * \star 3)$
(c) $(2 * * 2) * * 3$
Why do two of the expressions have the same output?
Rewrite expression (c) with one exponention operator ( $\star \star$ ) and one multiplication operator (*).

RO
Reynald Oliveria
Numerade Educator

Problem 17

Prompt for input and then print the input as a string, an integer, and a float-point value. What values can you input and print without errors being generated?

Check back soon!

Problem 18

In a Python shell, try some of the illegal expressions mentioned in the chapter and observe the error messages. For example, try assigning a value to a keyword, such as and = 4. Each error message begins with the name of the error, and that name is useful in checking for errors. Generate at least five different error messages.

Check back soon!

Problem 19

Table 2 lists Python operators, some of which may have been unfamiliar to you. Open up the Python shell and experiment with unfamiliar operators and see how many you can define.

Check back soon!
01:04

Problem 20

We know from mathematic that parentheses can change the ordet of operations. For example, consider $a+b * c,(a+b) * c$, and $a+(b * c)$. In general, two of those expressions will be the same and one will be different. Through trial and error, find one set of integer values for $a, b$, and $c$ so that all three expressions have the same value and $\mathrm{a} !=\mathrm{b} !=\mathrm{c}$.

Carson Merrill
Carson Merrill
Numerade Educator

Problem 21

Consider the expression $(a+b) * c$, but with string values for $\mathrm{a}, \mathrm{b}$, and $\mathrm{c}$. Enter that into the Python shell. What happens? Why?

Check back soon!
02:03

Problem 22

One way to determine whether an integer is even is to divide the number by 2 and check the remainder. Write a three-line program that prompts for a number, converts the input to an integer, and prints a 0 if the number is even and a 1 if the number is odd.

Willis James
Willis James
Numerade Educator
02:29

Problem 23

Write a program to calculate the volume of water in liters when 1 centimeter of water falls on 1 hectare.

Matthew Baker
Matthew Baker
Numerade Educator
02:45

Problem 24

Using Turtle Graphics, draw a six-pointed star.

MS
Malcom Smith
Numerade Educator
02:07

Problem 25

A day has $86,400 \operatorname{secs}(24 * 60 * 60)$. Given a number in the range 1 to 86,400 , output the current time as hours, minutes, and seconds with a 24-hour clock. For example: $70,000 \mathrm{sec}$ is 19 hours, 26 minutes, and 40 seconds.

Bruce Edelman
Bruce Edelman
Numerade Educator
00:26

Problem 26

A telephone directory has $N$ lines on each page and each page has exactly $C$ columns. An entry in any column has a name with the corresponding telephone number. On which page, column, and line is the $X$ th entry (name and number) present? (Assume that page, line, column numbers, and $X$ all start from 1.)

James Kiss
James Kiss
Numerade Educator
01:34

Problem 27

If the lengths of the two parallel sides of a trapezoid are $X$ meters and $Y$ meters, respectively, and the height is $H$ meters, what is the area of the trapezoid? Write Python code to output the area.

Sarah Lewites
Sarah Lewites
Numerade Educator
01:12

Problem 28

Simple interest is calculated by the product of the principal, number of years, and interest, all divided by 100 . Write code to calculate the simple interest on a principal amount of $$\$ 10,000$$ for a duration of 5 years with the rate of interest equal to $12.5 \%$.

Hossam Mohamed
Hossam Mohamed
Numerade Educator
05:01

Problem 29

Consider a triangle with sides of length 3,7 , and 9. The law of cosines states that given three sides of a triangle $(a, b$, and $c$ ) and the angle $C$ between sides $a$ and $b$ : $c^2=a^2+b^2-2 * a * b * \cos (C)$. Write Python code to calculate the three angles in the triangle.

Sandip Ranjan
Sandip Ranjan
Numerade Educator

Problem 30

Checking the user input for errors is a vital part of programming. The simple program below attempts to take a string input and convert it into an integer. What will happen if the user enters "Hello World" at the prompt rather than a number? Can you think of a way that the program can be altered to handle this input? (Hint: Think about adjusting how the program handles different types of input.)
Raw1 = input ('Please enter a number:')
Int $1=$ int (Raw1)

Check back soon!
01:34

Problem 31

The radius and mass of the Earth are $r=6378 \times 10^3$ meters and $m 1=5.9742 \times 10^{2 i} \mathrm{~kg}$, respectively. Mr. Jones has a mass of $X \mathrm{~kg}$. Prompt the user to input $X$ and then calculate the gravitational force $(F)$ and acceleration due to gravity $(g)$ caused by the gravitational force exerted on him by the Earth. Remember, $F=G(m 1)(m 2) /\left(r^2\right)$ and $F=m g$. Let the universal gravitational constant $G=6.67300 \times 10^{-11}$ (in units of $m^3 \mathrm{~kg}^{-1} s^{-2}$ assuming the MKS [meter-kilogram-second] system). Check that the resulting value of $g$ is close to $9.8 \mathrm{~m} / \mathrm{s}^2$.

Anurag Kumar
Anurag Kumar
Numerade Educator
02:19

Problem 32

Python comes with hundreds of modules. Here is a challenge for you: find a module that you can import that will generate today's date so you can print it. Use your favorite search engine for help in finding which module you need and how to use it. In the end, your task is to do the following:
》> print ("Today's date is:", $\mathrm{X}$ )
Today's date is: 2009-05-22

Willis James
Willis James
Numerade Educator
03:24

Problem 33

In football, there is a statistic for quarterbacks called the passer nating. To calculate the passer rating, you need five inputs: pass completions, pass attempts, total passing yards, touchdowns, and interceptions. There are five steps in the algorithm. Write a program that asks for the five inputs and then prints the pass rating:
(a) $\mathrm{C}$ is the "completions per attempt" times 100 minus 30 , all divided by 20.
(b) $\mathrm{Y}$ is the "yards per attempt" minus 3 , all divided by 4.
(c) $\mathrm{T}$ is the "touchdowns per attempt" times 20 .
(d) I is 2.375 minus ("interceptions per attempts" times 35).
(e) The pass rating is the sum of C, Y, T, and I, all divided by 6 and then multiplied by 100 .

Jennifer Stoner
Jennifer Stoner
Numerade Educator
01:42

Problem 34

Body mass index (BMI) is a number calculated from a person's weight and height. According to the Centers for Disease Control and Prevention, the BMI is a fairly reliable indicator of body fatness for most people. BMI does not measure body fat directly, but research has shown that BMI correlates to direct measures of body fat, such as underwater weighing and dual-energy X-ray absorptiometry. The formula for BMI is
$$
\text { weight/beight } t^2
$$
where weight is in kilograms and height is in meters.
(a) Write a program that prompts for metric weight and height and outputs the BMI.
(b) Write a program that prompts for weight in pounds and height in inches, converts the values to metric, and then calculates the BMI.

Erika Bustos
Erika Bustos
Numerade Educator