Exercise 1 - Solutions

# 1) The following line causes a SyntaxError. Please correct the line so that it's output is 'Hallo Welt!' [1P]
print("Hallo Welt!")
Hallo Welt!

# 2) Calculate the difference between a and b and assign the result to a variable called 'v_1'. 
#    What are the datatypes of a, b and 'v_1'? [2P]
a = 976.543
b = 345
v_1 = a-b # Data-Types: a: float, d: int, v_1: float
v_1
631.543
print("The data type of a is: {}".format(type(a)))
print("The data type of b is: {}".format(type(b)))
print("The data type of v_1 is: {}".format(type(v_1)))
The data type of a is: <class 'float'>
The data type of b is: <class 'int'>
The data type of v_1 is: <class 'float'>

# 3) Calculate the remainder of the division 100/17 by only using one operator 
#    and save the result in v_2 (maybe google helps?) [1P]
v_2 = 100%17
v_2
15
# 4) The speed of light is about 300'000 km/s. What is the wavelength (in nanometers) of a light wave 
#    with a frequency of 5.0E+14 per second? Can we see this light? [4P]
#    If you don't know how to convert frequencies to wavelengths, google can help you!
#    Save the result in v_3.

c = 300000 * 1000 # conversion into meters/s
f = 5.0E+14

wv = c/f

v_3 = wv*1.0E+9 # conversion into nanometers
v_3 # Yes we can see the light at 600nm (red/orange)
600.0
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20,10))
ax = fig.add_subplot(111)
rect1 = matplotlib.patches.Rectangle((-200,-100), 400, 200, color=('#c94000'))
ax.add_patch(rect1); plt.xlim([-200, 200]); plt.ylim([-100, 150]); plt.axis("off")
plt.text(-200,120,"#c94000 'Diese Farbe hat eine ungefähre Wellenlänge von 600 nm'\n- https://encycolorpedia.de/c94000",size=20)
plt.show()

png

# 5) Print the following string to the console using the format-function and the variables 'n' and 'pi': "Pi rounded to the first 10 decimals is: 3.1415926536" [2P]
n  = 10
pi = 3.14159265358979323846264338

print("Pi rounded to the first {0:1d} decimals is: {1:1.10f}".format(n,pi))
Pi rounded to the first 10 decimals is: 3.1415926536