# 2.17. Exercises¶

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

1. 5 ** 2  = 25

2. 9 * 5 = 45

3. 15 / 12 = 1.25

4. 12 / 15 = 0.8

5. 15 // 12 = 1

6. 12 // 15 = 0

7. 5 % 2 = 1

8. 9 % 5 = 4

9. 15 % 12 = 3

10. 12 % 15 = 12

11. 6 % 6 = 0

12. 0 % 7 = 0

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

1. 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.

1. 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.

1. 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.

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

1. 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 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.

1. 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.

1. 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..

1. 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.

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

1. 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 def_c. Output a message to the user giving the temperature in Celsius.

1.         : Piece together the code so that a user is asked for two numbers, and then the sum of those two numbers is printed out.num_one = input("Please enter your first number: ")
---
---
sum_of_input = int(num_one) + int(num_two)
---
print(sum_of_input)

1.         : 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.user_gallons = input("How many gallons should be converted?: ")
---
num_gallons = int(user_gallons)
---
liters = num_gallons * 3.785
---
print("Number of liters: " + str(liters))

1.         : Write a program that will convert table spoons to teaspons. 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.user_tablespoons = float(input("How many tablespoons should be converted?: "))
---
teaspoons = user_tablespoons * 3
---
print("Number of teaspoons: " + str(teaspoons))


## 2.17.1. Contributed Exercises¶

Write a program that inputs the number of seconds, and returns the number of minutes and seconds. For example, “130” should return “2 minutes and 10 seconds”.
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.

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.

Escribe un programa que calcule el consumo de un coche, medido en litros de gasolina por cada 100 kilómetros. preguntará al usuario el número de kilómetros recorridos y el número de litros consumidos, y mostrará un mensaje con el consumo del coche.
Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.
Escribe un programa que calcule el consumo de un coche, medido en litros de gasolina por cada 100 kilómetros y en kilómetros que se pueden recorrer con un litro de gasolina. Preguntará al usuario el número de kilómetros recorridos y el número de litros consumidos, y mostrará un mensaje con el consumo del coche, tanto en litros por cada 100 km como en kilómetros que se pueden recorrer con 1 litro de gasolina..
Write a program that will convert degrees celsius to degrees fahrenheit.
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.
baypath15: 67%, baypath20: 37%, baypath12: 67%, baypath18: 73%, baypath11: 63%, baypath14: 53%, baypath17: 53%, baypath13: 40%
Write a short program that will assign 71 to a variable that represents current temperature, and then increment its value first by one, and then by two. Display the value of the temperature variable (should be 74), and its type.
For the three expressions below in the box (line 5, line 7 and line 9), write their shorthand notations.
Write a program that will compute the area of a rectangle. Prompt the user to enter the width and height of the rectangle. Print a nice message with the answer.
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.

Write a program that converts a user’s input of a number of hours (for example, 5) into minutes (for example, 300).
Write a program that asks a user for an input sentence (for example, I love Python!) and prints the number of characters in the sentence (for example, 14).
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: Junte os pares corretamente. Feedback that is displayed if things are incorrectly matched
• Draggable element
• Dropzone to be matched with text
• Draggable text
• Text of dropzone

Q-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

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

1. 5 ** 2

2. 9 * 5

Write Python expressions and print the result for the following mathematical expressions. The first one is done for you.

Do not evaluate the expressions by hand.

$\begin{split}&3+6\\ &-0.35 / 4\\ &5 / 6\\ &123456789 / 13\text{ (integer division)}\\ &2^3 \\ &\text{remainder of }123456789 / 13 \\ &(116 -3.2)/(2\times7.75)\\ &(978 + -3\times-4)^{0.5}\\\end{split}$

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

$v_\text{rms} = \sqrt{\frac{3k_\text{B}T}{m_0}}$

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

$\frac{1}{\lambda} = R_\text{H}\left(\frac{1}{2^2} - \frac{1}{n^2}\right)\quad n=3,4,5,\dots$

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

$A = P\left(1+\frac{r}{n}\right)^{nt}.$

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?

Correct the code below to output number 5.

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.

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).

Q-1: Consider the following Python command: print("Hello, World"). What does print represent here?

• 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: Variable names can contain spaces?

• True
• No, variable name cannot use spaces, use _ instead.
• False
• Correct.

Q-1: Is me+you a valid variable name?

• True
• No, variable name cannot use special character unless it's an _.
• False
• Correct.

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:

1. 6 ** 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

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:

1. 6 ** 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

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.

Let’s follow up on the previous question. Suppose the same virus spread model applies, as stated in the previous question (i.e., one infected person can pass the virus to a healthy person in 24 hours). If the virus can spread to the whole population in n days, how many days does it need to spread to half of the population? Do it mentally first. Then write a program to verify your answer. Your program should accept an input of the population (store it to a variable named population) and output the days for the half population to get infected.

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.