2.17. ExercisesΒΆ
-
Evaluate the following numerical expressions in your head, then use the active code window to check your results:
5 ** 2
9 * 5
15 / 12
12 / 15
15 // 12
12 // 15
5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7
5 ** 2 = 25
9 * 5 = 45
15 / 12 = 1.25
12 / 15 = 0.8
15 // 12 = 1
12 // 15 = 0
5 % 2 = 1
9 % 5 = 4
15 % 12 = 3
12 % 15 = 12
6 % 6 = 0
0 % 7 = 0
-
What is the order of the arithmetic operations in the following expression. Evaluate the expression by hand and then check your work.
-
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.
-
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.
-
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
.
-
Add parentheses to the expression
6 * 1 - 2
to change its value from 4 to -6.
-
Challenge: The formula for computing the final amount if one is earning compound interest is given on Wikipedia as
Write a Python program that assigns the principal amount of 10000 to variable
P
, assign ton
the value 12, and assign tor
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 aftert
years.
-
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.
-
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
andheight
. Print a nice message with the answer..
-
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.
-
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 indeg_c
. Output a message to the user giving the temperature in Celsius.
-
Piece together the code so that a user is asked for two numbers, and then the sum of those two numbers is printed out.
-
Write a program that will convert gallons to liters. This program will also need to get input from a user to see how many gallons should be converted and the result should be printed to the user.
-
Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.
2.17.1. Contributed ExercisesΒΆ
The formula for computing the final amount if one is earning compound interest is given on Wikipedia as

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.
-
Q-1: Match each component of the expression a = square(3) + 1 with the right box on the right.
- a
- variable (name)
- =
- assignment
- square
- function name
- square(3)
- function call
- square(3) + 1
- 10
Initialize two variables to values 9
and 2
respectively.
Then:
1. increment each variable first by value 2
(means you
update those variables)
2. integer divide the two variables (divide the larger value with
the lower one), and assign the result to another variable called
c
3. print the variable c
and make sure you have the desired
result
Q-1: Comments in Python are written with a special character, which one?
Create a variable named carname
and assign the value
Volvo
to it. Then print how many characters carname
has.
Warning: you cannot write print(5)
Consider the code below. From line 1-5
you have a function
called even
that checks whether a number is even or not. If it
is even, the function returns true, otherwise it returns false. In
line 7
there is a variable called a
and is initialized to value
4
. Modify the code below so to:
1. Call function even
to check whether a
is even or not
and print the result to make sure you have it right.
2. Increment a
by one and call the function again and print
the result again.
-
Q-1: Assume that variable a is initialized to 3 with this line of code: a = 3. Match each piece of code on the left, to the right final value of a on the right if you execute each of the expressions on the left independently.
- a = a + 1
- 4
- a = a
- 3
- a -= 2
- 1
- b = a + 1
- 3
- a = square(a)
- 9
- b = square(a)
- 3
Q-1: Evaluate the following numerical expressions in your head, then use the active code window to check your results:
5 ** 2
9 * 5
15 / 12
12 / 15
15 // 12
12 // 15
5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7
Evaluate the following numerical expressions in your head, then use the active code window to check your results:
5 ** 2
9 * 5
Add parentheses to the expression 6 * 1 - 2
to change its value from 4 to -6.
Optional. 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.
Challenge: Write a program that will convert degrees celsius to degrees fahrenheit.
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.
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.
Below the code already provided for you,
increment i
by 10, decrement j
by 200, use the *=
operator to increase k
by a factor of 1.5 and reassign l
to 42.
Write a program to print out the root mean squared speed of hydrogen molecules in a gas where
where \(k_\text{B}=1.381\times 10^{-23}~\text{J/K}\) is
Boltzmann’s constant, \(m_0=2(1.67\times10^{-27}~\text{kg})\)
and \(T\) is the temperature supplied by the user. Please use
the variable temperature
to hold the user supplied temperature as
a float
.
The wavelengths of the spectral lines from hydrogen are given by the Balmer series
where \(\lambda\) is the wavelength in m and \(R_\text{H}=1.0974\times10^7~\text{m$^{-1}$}\). Print out the wavelengths of the first five spectral lines.
Write a program that will compute the area of a circle. Prompt the
user to enter the radius and save it to a variable called
radius
. Use 3.14 for the value of \(\pi\). Print a nice
message back to the user with the answer.
The formula for computing the final amount if one is earning compound interest is given on Wikipedia as
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.
Write a program that will compute MPG for a car. Prompt the user to
enter the number of miles driven and the number of gallons
used. Store the values as type float
in variables call miles
and gallons
respectively. Print a nice message with the answer.
Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?
Write in Python the following expression: 2^3 (two raised to the power of 3) and assign it to a variable called result. Print the value of variable result. You should see 8 as your result.
- a function
- Yes, this represents function call. ``print`` function takes an argument that it prints on your screen.
- a value
- Value is something you cannot call like: "Hello, World", 3, 1.5, and so on.
Q-1: Consider the following Python command: print("Hello, World")
. What does print represent here?
- True
- No, variable name cannot use spaces, use _ instead.
- False
- Correct.
Q-1: Variable names can contain spaces?
- True
- No, variable name cannot use special character unless it's an ``_``.
- False
- Correct.
Q-1: Is me+you
a valid variable name?
Correct the code below that converts minutes into seconds. When a user provides a number of minutes, it outputs the minutes converted to seconds. E.g., when a user provides number 1, the output should be 60. Work until you get the right answer!
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 a 24 hour clock when the alarm goes off.
If this is a homework problem instead of an example in the text then the assignment text should go here. The assignment text ends with the line containing four tilde
Create a variable named carname
and assign the value
Honda
to it. Then print how many characters carname
has.
Warning: you cannot write print(5)
Write code that asks the user to input the year a person was born. Print what the person’s age will be after their birthday in 2019.
Write code to determine how many times “West Virginia” appears in the lyrics to Country Roads. Store the number in a variable named country_roads_times.
Q-1: Suppose Python gives you this error:
TypeError: cannot concatenate 'str' and 'int' objects on line 1
What does this error mean? How would you fix it?
Evaluate the following numerical expressions in your head, then use the active code window to check your results:
6 ** 2
9 * 5
15 / 12
12 / 15
15 // 12
12 // 15
5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7
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 a 24 hour clock when the alarm goes off.
Evaluate the following numerical expressions in your head, then use the active code window to check your results:
6 ** 2
9 * 5
15 / 12
12 / 15
15 // 12
12 // 15
5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7
Write code that asks users to enter the year they were born. Print out how many years old they will turn in 2019.
Write a program that asks for the user’s name and age (in years), then tells the user how old they are in days. Don’t worry about leap year.
Write a program that will compute the area of a trapezoid. Prompt the user to enter base 1, base 2 and height and save them to variables called a
, b
and h
. Print a nice message back to the user with the answer.
The degree Fahrenheit (symbol: °F) is widely used in the USA. However, Noah’s Dad is more used to the degree Celsius (symbol: °C). Help Noah’s Dad to write a program to convert the degree Fahrenheit to the degree Celsius. You can do some research for the conversion formula. Your code should: 1. Ask the user for the temperature in Fahrenheit and store it in a variable called fahrenheit
. 2. Calculate the equivalent temperature in degrees Celsius and store it in a variable called celsius
. 3. Output a message to the user giving the temperature in Celsius.
Write a program to accept an integer num
as the input and output a boolean value to determine whether it is an odd number or not.
Write a program to accept two integers a
and b
as the input. Output the lcm (least common multiple) of them. This program will not be auto graded (You tutor will grade it manually). To help you to verify the correctness of your program, here are some test cases you can use: lcm(16, 24) = 48, lcm(8, 12) = 24, lcm(6, 9) = 18, lcm(20, 28) = 140, lcm(18, 81) = 162
Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.
Write a program that will convert tablespoons to teaspoons. This program will also need to get input from a user to see how many tablespoons should be converted and the result should be printed to the user.
Q-1: Write a short essay on the meaning of life.
Correct the code below to output number 5.
Consider the initialized variables in the code below I created for you. Add code after line 7 to check and print their types (don’t touch/change lines 1 - 7).
- 4
- Correct. You first assign value 3 to the variable, and then you reasign it the old value + 1.
- 3
- Not quite. The value of glass_of_water changes.
- 1
- Not quite. The value of glass_of_water is initially 3, then you take its value (3) and add 1 to it and assign it back to glass_of_water.
- glass_of_water
- Hmm no. glass_of_water is a variable, and when we print variables we actually print their values. Compare to: print("glass_of_water")
Q-1: What value is printed in the final line of code below?
glass_of_water = 3 glass_of_water = glass_of_water + 1 print(glass_of_water)
- 1
- Not quite. Review 2.12 Reassignment section.
- 7
- Not quite. Review 2.12 Reassignment section.
- 5
- Correct. People variable is initially one, and its valued is increased by one 4 times.
- people
- Hmm no. people is a variable, and when we print variables we actually print their values. Compare to: print("people")
Q-1: What value is printed in the final line of code below?
people = 1 people = people + 1 people = people + 1 people = people + 1 people = people + 1 print(people)
Write a calculator or tool for another class. For example (you therefore can’t use this) for a history class you might make a calculator that is given a historical date and returns how many years since that event transpired. You may add a list of historical events that the user can select, that returns the years since those events. Your calculator should get user input, and the output should vary based on the users input. Demonstrate the use of conditionals. Points for creativity as well. Your calculator should have error messages if the input is invalid (i.e. if ‘’January’’ is given instead of a year.)
(2 points) Ada Lovelace and Charles Babbage are trapped in a dimensional warp and need your help to escape before they are eaten by a grue. They need you to write a program that will calculate the dimensional escape code they need to exit the warp.
Write a program that prompts the user to input their three dimensional coordinates (integers), storing them in the variables z
, q
, and d
.
To compute the dimensional escape code, multiply z
by 352, divide q
by 17, raise d
to the 7th power.
Add the result of all three of these operations together and store the result in a variable called escapeCode
.
Note that the result of this operation could be a floating-point number.
Do not alter what is stored in z
, q
, or d
during these operations, as this could break the space-time continuum. (It will also break my tests if you change them.)
Once you have completed this program and it passes our checks, our system will transmit and translate the instructions to the doomed pair. (In other words, I have included a runtime test so that you can check your work.) Click the Show Code button to begin.
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.
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.
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 (1) prompts the user for a starting day number
and the length of their stay, then (2) stores the values as type int
variables
start_day
and stay_length
respectively, and finally (3) prints the
number of the day of the week they will return in an appropriate message.
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.
Write a program that will compute MPG for a car. Prompt the user to
enter the number of miles driven and the number of gallons
used. Store the values as type float
in variables called miles
and gallons
respectively. Print a nice message with the answer.
The formula for computing the final amount if one is earning compound interest is given on Wikipedia as
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. Print a message to the user giving the final amount of money after \(t\) years.
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. Your program
should first prompt the user for the time now (in hours) and for the number of hours
to wait for the alarm, then store those values as type int
variables time_now
and wait_time
respectively, and finally print the time on the 24 hour clock when
the alarm will go off.
Ada Lovelace and Charles Babbage are trapped in a dimensional warp and need your help to escape before they are eaten by a grue. They need you to write a program that will calculate the dimensional escape code they need to exit the warp.
Write a program that asks the user to input their three dimensional coordinates, storing them in the variables z
, q
, and d
, storing each as a number, not as a string.
To compute the dimensional escape code, multiply z
by 352, divide q
by 17, raise d
to the 7th power.
Do not alter what is stored in z
, q
, or d
during these operations, as this could break the space-time contin… space-time continueinueiueum… look, I’m new to this. It will break our tests if you change them.
Add the result of all three numbers of these operations together and store the result in a variable called escapeCode.
Once you have completed this program and it passes our checks, our system will transmit and translate the instructions.
Arrange the code so that a
will store the value 30 and b
will store the value 10
a = 10 --- b = a + 20 --- a = a + b --- b = a - b --- a = a - b
Write code that asks the user the year of his/hers birthday. Then the code prints “You are #answer many years old”.
Write a program that will convert gallons to liters. This program will also need to get input from a user to see how many gallons should be converted and the result should be printed to the user.