2.17 Exercises

From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython2 class periods11 exercises6 graded tests

README.md

2.17 Exercises

Read first in the book: 2.1 Introduction, 2.2 Values and Data Types, 2.3 Operators and Operands, 2.4 Function Calls, 2.5 Data Types, 2.6 Type conversion functions, 2.7 Variables, 2.8 Variable Names and Keywords, 2.9 Choosing the Right Variable Name, 2.10 Statements and Expressions, 2.11 Order of Operations, 2.12 Reassignment, 2.13 Updating Variables, 2.14 Hard-Coding, 2.15 Input, 2.16 Glossary.

Exercise 1

Evaluate the following numerical expressions in your head, then use the active code window to check your results:

  1. 5 ** 2
  2. 9 * 5
  3. 15 / 12
  4. 12 / 15
  5. 15 // 12
  6. 12 // 15
  7. 5 % 2
  8. 9 % 5
  9. 15 % 12
  10. 12 % 15
  11. 6 % 6
  12. 0 % 7

Open the 01 folder and edit main.py. This one is not graded, so check it yourself.

Exercise 2

What is the order of the arithmetic operations in the following expression. Evaluate the expression by hand and then check your work.

Open the 02 folder and edit main.py. This one is not graded, so check it yourself.

Exercise 3

Challenge: Many people keep time using a 24 hour clock (11 is 11am and 23 is 11pm, 0 is midnight). If it is currently 13 and you set your alarm to go off in 50 hours, it will be 15 (3pm). Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and then ask for the number of hours to wait for the alarm. Your program should output what the time will be on the clock when the alarm goes off.

Open the 03 folder and edit main.py. This one is not graded, so check it yourself.

Exercise 4

It is possible to name the days 0 thru 6 where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights you would return home on a Saturday (day 6). Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.

Open the 04 folder and edit main.py. This one is not graded, so check it yourself.

Challenge: Take the sentence: All work and no play makes Jack a dull boy. Store each word in a separate variable, then print out the sentence on one line using print.

Exercise 5

Add parentheses to the expression 6 * 1 - 2 to change its value from 4 to -6.

Open the 05 folder and edit main.py. Run Tests checks it.

Exercise 6

Challenge: The formula for computing the final amount if one is earning compound interest is given on Wikipedia as

formula for compound interest

Write a Python program that assigns the principal amount of 10000 to variable P, assign to n the value 12, and assign to r the interest rate of 8% (0.08). Then have the program prompt the user for the number of years, t, that the money will be compounded for. Calculate and print the final amount after t years.

Open the 06 folder and edit main.py. Run Tests checks it.

Exercise 7

Write a program that will compute the area of a circle. Prompt the user to enter the radius and save it to avariable called radius. Print a nice message back to the user with the answer.

Open the 07 folder and edit main.py. Run Tests checks it.

Exercise 8

Challenge: Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle and store the values in variables called width and height. Print a nice message with the answer..

Open the 08 folder and edit main.py. Run Tests checks it.

Exercise 9

Write a program that will compute MPG for a car. Prompt the user to enter the number of miles driven and store it in a variable called miles and the number of gallons used stored in a variable gallons. Print a nice message with the answer.

Open the 09 folder and edit main.py. Run Tests checks it.

Exercise 10

Challenge: Write a program that will convert degrees celsius to degrees fahrenheit.

Open the 10 folder and edit main.py. This one is not graded, so check it yourself.

Exercise 11

Ask the user for the temperature in Fahrenheit and store it in a variable call deg_f. Calculate the equivalent temperature in degrees Celsius and store it in deg_c. Output a message to the user giving the temperature in Celsius.

Open the 11 folder and edit main.py. Run Tests checks it.

Check your understanding: a mixed-up code puzzle in the book.

Check your understanding: a mixed-up code puzzle in the book.

Check your understanding: a mixed-up code puzzle in the book.

For teachers

2.17 Exercises

Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 11 Python coding exercises (6 graded).

  • Time: 2 class periods
  • Tests: 6 in 6 graded exercises, set up for you when you import the lesson

Reading first

Exercises

FolderGradingTests
01Not graded; students check it themselves0
02Not graded; students check it themselves0
03Not graded; students check it themselves0
04Not graded; students check it themselves0
05Autograded1
06Autograded1
07Autograded1
08Autograded1
09Autograded1
10Not graded; students check it themselves0
11Autograded1