Python Code Example: volume and surface area of a ball

This code snippet calculates the volume and surface area of a sphere given its radius, and then truncates the results to a specified number of decimal places before printing them.

Code Example

import math

def getVolume(r):
    return ((4 * math.pi * pow(r, 3)) / 3)


def getSurfaceArea(r):
    return 4 * math.pi * pow(r, 2)


def truncate(n, d=0):
    m = 10 ** d
    return int(n * m) / m


radius = int(input("Please enter radius: "))
volume = truncate(getVolume(radius), 2)
area = truncate(getSurfaceArea(radius), 2)

print("Volume: " + repr(volume))
print("Surface: " + repr(area))

Output

Please enter radius: 4
Volume: 268.08
Surface: 201.06

Code Explanation

Import Statement

import math
  • Purpose: This line imports the math module, which provides access to mathematical functions like math.pi and pow().

Function getVolume(r)

def getVolume(r):
    return ((4 * math.pi * pow(r, 3)) / 3)
  • Purpose: This function calculates the volume of a sphere.
  • Parameter: The function takes one parameter r, which is the radius of the sphere.
  • Formula: The volume of a sphere is calculated using the formula Volume=43πr3Volume=34​πr3.
    • math.pi provides the value of π (pi).
    • pow(r, 3) raises the radius r to the power of 3.
  • Return Value: The function returns the calculated volume.

Function getSurfaceArea(r)

def getSurfaceArea(r):
    return 4 * math.pi * pow(r, 2)
  • Purpose: This function calculates the surface area of a sphere.
  • Parameter: The function takes one parameter r, which is the radius of the sphere.
  • Formula: The surface area of a sphere is calculated using the formula Surface Area=4πr2Surface Area=4πr2.
    • math.pi provides the value of π (pi).
    • pow(r, 2) raises the radius r to the power of 2.
  • Return Value: The function returns the calculated surface area.

Function truncate(n, d=0)

def truncate(n, d=0):
    m = 10 ** d
    return int(n * m) / m
  • Purpose: This function truncates a number n to d decimal places.
  • Parameters:
    • n: The number to be truncated.
    • d: The number of decimal places to keep (default is 0).
  • Calculation:
    • m = 10 ** d: Raises 10 to the power of d to create a multiplier.
    • int(n * m) / m: Multiplies n by m, converts it to an integer to drop any fractional part, and then divides by m to scale it back down.
  • Return Value: The function returns the truncated number.

Main Program

radius = int(input("Please enter radius: "))
  • User Input: The program prompts the user to input the radius of the sphere. The input is read as a string, converted to an integer using int(), and stored in the variable radius.
volume = truncate(getVolume(radius), 2)
area = truncate(getSurfaceArea(radius), 2)
  • Function Calls and Truncation:
    • getVolume(radius) calculates the volume of the sphere using the input radius.
    • truncate(..., 2) truncates the volume to 2 decimal places.
    • getSurfaceArea(radius) calculates the surface area of the sphere using the input radius.
    • truncate(..., 2) truncates the surface area to 2 decimal places.
print("Volume: " + repr(volume))
print("Surface: " + repr(area))
  • Output:
    • The truncated volume is converted to a string using repr() and concatenated with the label “Volume: “, then printed.
    • The truncated surface area is converted to a string using repr() and concatenated with the label “Surface: “, then printed.