Python Practice › Basic Programs › Program 39

Volume of a sphere

EasyBasic Programs · 39 of 40

Write a Python program to calculate the volume of a sphere. Volume = 4/3 × π × r³.

Advertisement
Input
r = 6
Output
Volume: 904.78

Approach

  1. Apply the formula with math.pi.

Solution

Python
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.

More basic programs

← All 40 Basic Programs

Leave a Comment