1.9. Exercise Solutions#

Solution to Exercise 1.1

In [1]: 2 - (3 + 6)
Out[1]: -7

In [2]: 2 * (5 - 8 * (3 + 6))
Out[2]: -134

In [3]: 2 * (2 - 2 * (3 - 6 + 5 * (4 - 7)))
Out[3]: 76

In [4]: (2 * (5 - 4 * (3 + 8))) / (3 * (4 - (3 - 5)))
Out[4]: -4.333333333333333

In [5]: 2 * 4 ** 5 / (81 - 5 ** 2)
Out[5]: 36.57142857142857

In [6]: 14151 % 571
Out[6]: 447

In [7]: 1111 // 14
Out[7]: 79

Solution to Exercise 1.2

In [8]: centigrade = 37.8

In [9]: fahrenheit = 9 / 5 * centigrade + 32

In [10]: fahrenheit
Out[10]: 100.03999999999999

In [11]: centigrade = 100

In [12]: fahrenheit = 9 / 5 * centigrade + 32

In [13]: fahrenheit
Out[13]: 212.0

In [14]: centigrade = -40

In [15]: fahrenheit = 9 / 5 * centigrade + 32

In [16]: fahrenheit
Out[16]: -40.0

Solution to Exercise 1.3

In [17]: string1 = "Your mother was a hamster"

In [18]: string2 = "and your father smelt of elderberries!"

In [19]: print(string1.lower())
your mother was a hamster

In [20]: print(string2.upper())
AND YOUR FATHER SMELT OF ELDERBERRIES!

In [21]: print(string2.replace("elderberries", "roses"))
and your father smelt of roses!

In [22]: string3 = string1 + " " + string2

In [23]: print(string3)
Your mother was a hamster and your father smelt of elderberries!

In [24]: print(len(string3))
64

In [25]: print(string3[34:64])
 father smelt of elderberries!

Solution to Exercise 1.4

# Temperature in Fahrenheit
fahrenheit = 100

# Convert to centigrade
centigrade = 5 / 9 * (fahrenheit - 32)

# Print the result
print(f"{fahrenheit} degrees Fahrenheit is equivalent to {centigrade:0.2f} degrees centigrade")

# Temperature in Fahrenheit
fahrenheit = 0

# Convert to centigrade
centigrade = 5 / 9 * (fahrenheit - 32)

# Print the result
print(f"{fahrenheit} degrees Fahrenheit is equivalent to {centigrade:0.2f} degrees centigrade")

Output

100 degrees Fahrenheit is equivalent to 37.78 degrees centigrade
0 degrees Fahrenheit is equivalent to -17.78 degrees centigrade

Solution to Exercise 1.5

# Exercise 1.4
print("Number of seconds\n-----------------")

# Enter the number of seconds
initial_seconds = 1000000000;

# Calculate conversion values
seconds = initial_seconds;
seconds_in_a_minute = 60;
seconds_in_an_hour = 60 * seconds_in_a_minute;
seconds_in_a_day = 24 * seconds_in_an_hour;
seconds_in_a_week = 7 * seconds_in_a_day;
seconds_in_a_year = 365 * seconds_in_a_day;

# Number of years in the seconds
years = seconds // seconds_in_a_year; 
seconds -= years * seconds_in_a_year; # Subtract years from seconds

# Number of weeks in the seconds remaining
weeks = seconds // seconds_in_a_week;
seconds -= weeks * seconds_in_a_week;

# Number of days in the seconds remaining
days = seconds // seconds_in_a_day;
seconds -= days * seconds_in_a_day;

# Number of hours in the seconds remaining
hours = seconds // seconds_in_an_hour;
seconds -= hours * seconds_in_an_hour;

# Number of minutes in the seconds remaining
minutes = seconds // seconds_in_a_minute;
seconds -= minutes * seconds_in_a_minute

# Print the result
print(f"There are {years} years, {weeks} weeks, {days} days, {hours} hours, " \
      f"{minutes} minutes and {seconds} seconds in {initial_seconds} seconds.")

Output

There are 31 years, 37 weeks, 0 days, 1 hours, 46 minutes and 40 seconds in 1000000000 seconds.

Solution to Exercise 1.6

# Exercise 1.5

# Details of the loan
value = 200000
years = 20
interest = 4

# Calculate number of months and monthly interest
months = 12 * years
monthly_interest = interest / 100 / 12

# Calculate repayment
repayment = monthly_interest * value / (1 - (1 + monthly_interest) ** (-months))

# Print loan details and repayment amount
print("\nLoan repayment calculator\n-------------------------")
print(f"Loan amount          : £{value:0.2f}")
print(f"Loan duration        : {years} years")
print(f"Annual interest rate : {interest}%")
print(f"\nMonthly repayment    : £{repayment:0.2f}")

Output

Loan repayment calculator
-------------------------
Loan amount          : £200000.00
Loan duration        : 20 years
Annual interest rate : 4%

Monthly repayment    : £1211.96