+
Addition-
Subtraction*
Multiplication/
Division//
Integer Division**
Exponent%
ModulusWrite a function called square_area
that takes in one argument (side length) and calculates the area of a square (a quadrilateral with two sides parallel that are of equal length). The area of a square is calculated by multiplying the side by itself (\(area = side^2\)). Your function should return the value for calculated square area.
Name your file square.py
and submit it to attendance on gradescope. Add a comment to your code with a TA name.
round()
round()
Use the round() function to get a floating-point number rounded to the specified number of decimals.
Syntax:
The number of digits (ndigits
) is optional, but we will often round number to two decimals:
round()
Use the round() function to get a floating-point number rounded to the specified number of decimals.
Syntax:
Rounding is done toward the even choice. What would these round to? (discuss with your group)
round()
Use the round() function to get a floating-point number rounded to the specified number of decimals.
Syntax:
Rounding is done toward the even choice. What would these round to:
Write a function that calculates the volume of a sphere:
sphere_volume
radius
3.1415
for \(\pi\)):\[ v = {4 / 3} \cdot \pi \cdot radius^3 \]
sphere_volume(.75)
should return 1.77
.Write a function that calculates the area of a sphere:
sphere_area
radius
3.1415
for \(\pi\)):\[ a = 4 \cdot \pi \cdot radius^2 \]
sphere_area(.75)
should return 7.07
.def sphere_volume(radius):
"calculates the volume of a sphere of given radius"
volume = (4 / 3) * 3.1415 * radius**3
return round(volume, 2)
def sphere_area(radius):
"calculates the area of a sphere of given radius"
area = 4 * 3.1415 * radius**2
return round(area, 2)
def main():
r = .75
v = sphere_volume(r)
a = sphere_area(r)
print(v, a)
main()
1.77 7.07