find the surface area of a sphere in python

To find the surface area of a sphere in Python, we can use the following formula:

Surface Area = 4πr^2

where r is the radius of the sphere and π is the mathematical constant pi.

To implement this in Python, we can use the math library to access the value of pi and write a function to calculate the surface area:

main.py
import math

def sphere_surface_area(radius):
    surface_area = 4 * math.pi * radius**2
    return surface_area
113 chars
6 lines

We can then call this function with a radius value to get the surface area of the sphere:

main.py
radius = 5
surface_area = sphere_surface_area(radius)
print("Surface area of sphere with radius", radius, "is", surface_area)
126 chars
4 lines

Output:

main.py
Surface area of sphere with radius 5 is 314.1592653589793
58 chars
2 lines

gistlibby LogSnag