find the volume of a cone in python

To find the volume of a cone in Python, we need to use the formula V = (1/3) * pi * r^2 * h, where V is the volume of the cone, r is the radius of the circular base of the cone, h is the height of the cone and pi is a constant value of 3.141592653589793.

We can use the math module in Python to access the value of pi and perform the necessary calculations. Here's the code:

main.py
import math

def cone_volume(radius, height):
    volume = (1/3) * math.pi * (radius**2) * height
    return volume

# Example usage
print(cone_volume(5, 10))
159 chars
9 lines

In this example, we define a function cone_volume that takes in the radius and height of a cone as parameters, and calculates and returns its volume using the formula we mentioned earlier.

When we call cone_volume(5, 10), we get the result 261.79938779914943. This means that a cone with radius 5 units and height 10 units has a volume of approximately 261.8 cubic units.

gistlibby LogSnag