python tutorial - Python Program to find Volume and Surface Area of Sphere - learn python - python programming
- To write Python Program to find Volume and Surface area of Sphere with example.
- Before we step into the program, Let see the definitions and formulas behind Surface area of Sphere and Volume of Sphere
Learn Python - Python tutorial - Python Program to find Volume and Surface Area of Sphere - Python examples - Python programs
Surface Area of Sphere
- A Sphere looks like a basketball or we can say the three-dimensional view of a circle.
- If we know the radius of the Sphere then we can calculate the Surface Area of Sphere using formula:
- From the above formula, If we know the Surface Area of a sphere then we can calculate the radius of a Sphere using the formula:
Volume of Sphere
- Amount of space inside the sphere is called as Volume. If we know the radius of the Sphere then we can calculate the Volume of Sphere using formula:
Python Program to find Volume and Surface Area of Sphere
- We defined pi as global variable and assigned value as 3.14.
- This program allows user to enter the value of a radius and then it will calculate the Surface Area and Volume of a Sphere as per the formula.
Sample Code
Output
Learn Python - Python tutorial - Python Program to find Volume and Surface Area of Sphere - Python examples - Python programs
Analysis
- We have entered the Radius of a Sphere = 5
The Surface Area of a Sphere is
Surface Area = 4πr²
Surface Area = 4 * PI * radius * radius;
Surface Area = 4 * 3.14 * 5 * 5
Surface Area = 314
The Volume of a Sphere is
Volume = 4πr³
Volume = (4.0 / 3) * PI * radius * radius * radius;
Volume = (4.0 / 3) * 3.14 * 5 * 5 * 5;
Volume = 523.33333
- Let us calculate the Radius of a Sphere using the Surface Area:
- In the above example we got Surface area of a Sphere = 314 when the radius = 5. Let us do the reverse approach (Calculate the radius from Surface area = 5)
radius of a Sphere = √sa / 4π
radius of a Sphere = √314 / 4 * 3.14
radius of a Sphere = √314 / 12.56
radius of a Sphere = √25
radius of a Sphere = 5
Python Program to find Volume and Surface Area of Sphere using functions
- This program allows user to enter the value of a radius.
- We will pass the radius value to the function argument and then it will calculate the Surface Area and Volume of a Sphere as per the formula.
Sample Code
Output
Learn Python - Python tutorial - Python Program to find Volume and Surface Area of Sphere using functions - Python examples - Python programs
Analysis
- First, We imported the math library using the following statement. This will allow us to use the mathematical functions like math.pi
- Next, We defined the function with one argument using def keyword. It means, User will enter the radius of a sphere.
- Calculating the Surface Area and Volume of a Sphere as per the formula
NOTE: Please be careful while placing the open and close brackets, it may change the entire calculation if you place it wrong