Exercise 2.2

n_grains = 2 ** 64 - 1
weight_per_grain_in_tons = 25e-3 * 1e-3 * 1e-3
weight_in_tons = n_grains * weight_per_grain_in_tons
print(weight_in_tons)

weight_earth_in_tons = 5.972e21
fraction_in_percent = weight_in_tons / weight_earth_in_tons * 100.0
print(fraction_in_percent)
461168601842.73883
7.722180205002324e-09

Exercise 3.2

import math
print(math.pi ** math.e, math.e ** math.pi)
22.45915771836104 23.140692632779263

Exercise 3.3

import math
print(math.sqrt(1.0 ** 2 + 2.0 ** 2))
2.23606797749979

As alternative the math module provides a special function for this:

print(math.hypot(1, 2))
2.23606797749979

Exercise 3.4

If we see terms like x.y in Python we call y an attribute of x. The first example for attributes we have seen so far are attributes of math, e.g. sin is an attribute of math.

Exercise 4.2

Both terms can not be evaluated in Python. For mixed int and str types the addition with + is not defined. The same happens if we want to evaluate "1" * "2", the multiplication of two strings is not defined.

This does not imply that operations for mixed types are not supported in general. For example we can multiply int and str values:

print(3 * "hi")
hihihi
print("ha " * 3)
ha ha ha 

This was not covered in the script.

Exercise 5.3

celsius = float(input("temperature in celsius ? "))
fahrenheit = celsius * 9 / 5 + 32
print("temperature in fahrenheit is", fahrenheit)
temperature in celsius ? 37.778
temperature in fahrenheit is 100.0004