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)
import math
print(math.pi ** math.e, math.e ** math.pi)
import math
print(math.sqrt(1.0 ** 2 + 2.0 ** 2))
As alternative the math
module provides a special function for this:
print(math.hypot(1, 2))
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
.
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")
print("ha " * 3)
This was not covered in the script.
celsius = float(input("temperature in celsius ? "))
fahrenheit = celsius * 9 / 5 + 32
print("temperature in fahrenheit is", fahrenheit)