This topic material is based on the Python Programming for Data Science book and adapted for our purposes in the course.
import math
The function area()
accepts the argument radius
and calculates the area of a circle. Write three tests using assert
statements for the following conditions:
area(1)
returns a float
;area(0)
returns a value of 0;area(5)
is approximately equal to 78.5 (hint: math.isclose(..., abs_tol=0.1)
)def area(radius):
"""Calculate the area of a circle based on the given radius."""
return math.pi * radius ** 2
# Your answer here.
In the spirit of the EAFP (easier to ask for forgiveness than permission) philosophy. Modify the code of the function area()
and add a try
/except
statement to catch the type error raised by passing a string to area()
as shown below:
area('10')
def area(radius):
"""Calculate the area of a circle based on the given radius."""
pass # Remove this line and add your answer here.
In the spirit of the LBYL (look before you leap) philosophy. Modify the code of the function area()
and add a conditional if
/else
statement to make sure that a user has passed a number (int
or float
) to the area()
function. If they pass something else, raise a TypeError
.
def area(radius):
"""Calculate the area of a circle based on the given radius."""
pass # Remove this line and add your answer here.
For this exercise I want you to create a class called Circle
. It should have the following characteristics:
radius
and store this as an instance attribute.area()
which calculates the area of the circle.circumference()
which calculates the circumference of the circle.__str__()
which is a special method in Python and controls what is output to the screen when you print()
an instance of your class (learn more here). The print()
statement should print the string f"A Circle with radius {self.radius}"
.I've provided some tests for you to check your class.
class Circle:
"""A circle with a radius r."""
pass # Remove this line and add your answer here.
assert Circle(3).radius == 3, "Test 1 failed."
assert math.isclose(Circle(3).area(), 28.3, abs_tol=0.1), "Test 2 failed."
assert math.isclose(Circle(3).circumference(), 18.8, abs_tol=0.1), "Test 3 failed."
assert Circle(3).__str__() == "A Circle with radius 3", "Test 4 failed."
Now, let's create a new class sphere
that inherits from the circle
class we created above. It should have the following characteristics:
Circle
was, with the single argument radius
which is stored as an instance attribute.volume()
which calculates the volume of the sphere ($\frac{4}{3}{\pi}{r^3}$).f"A Sphere with volume 4.19"
when you call print(Sphere(1))
(hint: recall the __str__()
method from the previous question).I've provided some tests for you to check your class.
# Your answer here.
assert Sphere(3).radius == 3, "Test 1 failed."
assert math.isclose(Sphere(3).area(), 28.3, abs_tol=0.1), "Test 2 failed."
assert math.isclose(Sphere(3).circumference(), 18.8, abs_tol=0.1), "Test 3 failed."
assert math.isclose(Sphere(3).volume(), 113.1, abs_tol=0.1), "Test 3 failed."
assert Sphere(1).__str__() == "A Sphere with volume 4.19", "Test 4 failed."
Imagine that users of our Sphere
class often want to instantiate our class with a circumference
instead of a radius
. Add a class method called from_circ()
to the Sphere
class that allows users to do this. The method should calculate the radius
from the passed circumference
, and then use that radius
to make an instance of Sphere
.
I've provided some tests for you to check your modified class.
# Your answer here.
assert Sphere.from_circ(0).radius == 0, "Test 1 failed."
assert Sphere.from_circ(3 * math.pi).radius == 1.5, "Test 2 failed."
assert math.isclose(Sphere.from_circ(6).radius, 0.95, abs_tol=0.1), "Test 3 failed."
assert math.isclose(Sphere.from_circ(6).volume(), 3.65, abs_tol=0.1), "Test 4 failed."
assert Sphere.from_circ(6).__str__() == "A Sphere with volume 3.65", "Test 5 failed."
!jupyter nbconvert _03-py-tests-classes-practice.ipynb --to html --template classic --output 03-py-tests-classes-practice.html