Python Practice › Basic Programs › Program 39
Volume of a sphere
Write a Python program to calculate the volume of a sphere. Volume = 4/3 × π × r³.
Input
r = 6
Output
Volume: 904.78
Approach
- Apply the formula with
math.pi.
Solution
import math r = 6volume = 4 / 3 * math.pi * r ** 3print("Volume:", round(volume, 2))Output
Volume: 904.78
Line by line
Line 4
The radius is cubed (r ** 3).
Note
The original exercise wrote the formula as 4/3 × π × r² with π = 3. The correct formula uses r³, and math.pi gives an accurate π.
Learn the concept
New to this topic? Read the lesson, then come back.