find the surface area of a cylinder in python

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

main.py
surface_area = 2 * math.pi * radius * height + 2 * math.pi * radius**2
71 chars
2 lines

where radius is the radius of the cylinder's base and height is the height of the cylinder.

To implement this formula in Python, we first need to import the math module, which contains the constant math.pi and other useful math functions.

main.py
import math

def cylinder_surface_area(radius, height):
    # Calculate the surface area using the formula
    surface_area = 2 * math.pi * radius * height + 2 * math.pi * radius**2
    return surface_area
206 chars
7 lines

We can then use this function to find the surface area of any cylinder by passing in the radius and height as arguments:

main.py
# Example usage
radius = 3
height = 5
surface_area = cylinder_surface_area(radius, height)
print("The surface area of the cylinder is:", surface_area)
151 chars
6 lines

This will output: The surface area of the cylinder is: 150.79644737231007

gistlibby LogSnag