Note: Click on "Kernel" > "Restart Kernel and Run All" in JupyterLab after finishing the exercises to ensure that your solution runs top to bottom without any errors. If you cannot run this file on your machine, you may want to open it in the cloud .
The exercises below assume that you have read the first part of Chapter 2.
The ...
's in the code cells indicate where you need to fill in code snippets. The number of ...
's within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas.
Q1: The volume of a sphere is defined as 43∗π∗r3. Calculate this value for r=10.0 and round it to 10 digits after the comma.
Hints:
import ... # you may drop this cell and use your own approximation for Pi
...
Q2: Encapsulate the logic into a function sphere_volume()
that takes one positional argument radius
and one keyword-only argument digits
defaulting to 5
. The volume should be returned as a float
object under all circumstances.
def sphere_volume(...):
"""Calculate the volume of a sphere.
Args:
radius (float): radius of the sphere
digits (optional, int): number of digits
for rounding the resulting volume
Returns:
volume (float)
"""
return ...
Q3: Evaluate the function with radius = 100.0
and 1, 5, 10, 15, and 20 digits respectively.
radius = ...
sphere_volume(...)
sphere_volume(...)
sphere_volume(...)
sphere_volume(...)
sphere_volume(...)
Q4: What observation do you make?
< your answer >
radius = ...
for ... in ...:
...
Q6: What lesson do you learn about the float
type?
< your answer >