0
0
PythonProgramBeginner · 2 min read

Python Program to Find Volume of Sphere

You can find the volume of a sphere in Python using the formula volume = (4/3) * 3.14159 * radius**3, where radius is the sphere's radius.
📋

Examples

Inputradius = 1
OutputVolume of sphere with radius 1 is 4.188786666666666
Inputradius = 5
OutputVolume of sphere with radius 5 is 523.5983333333332
Inputradius = 0
OutputVolume of sphere with radius 0 is 0.0
🧠

How to Think About It

To find the volume of a sphere, you need to know its radius. The formula is four-thirds times pi times the radius cubed. You calculate the cube of the radius by multiplying the radius by itself three times, then multiply by pi and four-thirds to get the volume.
📐

Algorithm

1
Get the radius value from the user or input.
2
Calculate the cube of the radius by multiplying radius by itself three times.
3
Multiply the cubed radius by pi (3.14159) and then by 4/3.
4
Store the result as the volume of the sphere.
5
Print or return the volume.
💻

Code

python
radius = float(input("Enter the radius of the sphere: "))
volume = (4/3) * 3.14159 * radius**3
print(f"Volume of sphere with radius {radius} is {volume}")
Output
Enter the radius of the sphere: 5 Volume of sphere with radius 5.0 is 523.5983333333332
🔍

Dry Run

Let's trace the example where radius = 5 through the code

1

Input radius

User inputs radius = 5

2

Calculate radius cubed

5 ** 3 = 125

3

Calculate volume

(4/3) * 3.14159 * 125 = 523.5983333333332

4

Print result

Output: Volume of sphere with radius 5.0 is 523.5983333333332

StepOperationValue
1Input radius5
2radius ** 3125
3(4/3) * pi * radius^3523.5983333333332
4Print outputVolume of sphere with radius 5.0 is 523.5983333333332
💡

Why This Works

Step 1: Use the sphere volume formula

The formula volume = (4/3) * π * r³ calculates the space inside the sphere.

Step 2: Calculate radius cubed

Cubing the radius means multiplying it by itself three times: radius**3.

Step 3: Multiply by constants

Multiply the cubed radius by 4/3 and π to get the final volume.

🔄

Alternative Approaches

Using math.pi constant
python
import math
radius = float(input("Enter the radius of the sphere: "))
volume = (4/3) * math.pi * radius**3
print(f"Volume of sphere with radius {radius} is {volume}")
This uses Python's built-in math.pi for more accurate pi value.
Using a function to calculate volume
python
def sphere_volume(r):
    return (4/3) * 3.14159 * r**3
radius = float(input("Enter radius: "))
print(f"Volume: {sphere_volume(radius)}")
Encapsulates calculation in a function for reuse.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space needed is constant.

Which Approach is Fastest?

All approaches run in constant time; using math.pi is preferred for accuracy without affecting speed.

ApproachTimeSpaceBest For
Using 3.14159 constantO(1)O(1)Simple quick calculation
Using math.piO(1)O(1)More accurate pi value
Using functionO(1)O(1)Reusable code structure
💡
Use math.pi for a more precise value of pi instead of 3.14159.
⚠️
Forgetting to cube the radius and only multiplying by radius once.