Introduction to Python

Activity 1: Properties of a Cube

Lesson 5: Functions in Python

In this activity, we will use user-defined functions to calculate the properties of a cube — its volume, surface area, and the length of its face diagonal. This is a great example of how functions make code clean and reusable.

A 3D illustration of a cube with labeled sides showing the edge length, and arrows indicating volume, surface area, and face diagonal

Cube Property Formulas

PropertyFormula
VolumeV = side³
Surface AreaSA = 6 × side²
Face Diagonald = side × √2

Writing the Script

import math # Function to calculate volume def cube_volume(side): return side ** 3 # Function to calculate surface area def cube_surface_area(side): return 6 * side ** 2 # Function to calculate face diagonal def cube_face_diagonal(side): return side * math.sqrt(2) # Main program side = float(input("Enter the side length of the cube: ")) volume = cube_volume(side) surface_area = cube_surface_area(side) diagonal = cube_face_diagonal(side) print("Volume: " + str(volume)) print("Surface Area: " + str(surface_area)) print("Face Diagonal: " + str(round(diagonal, 2)))

Testing the Program

  • Run with side = 4 → Volume: 64.0, Surface Area: 96.0, Face Diagonal: 5.66
  • Run with side = 3 → Volume: 27.0, Surface Area: 54.0, Face Diagonal: 4.24
Python console showing the output of the cube properties calculator with volume, surface area, and face diagonal printed for two test runs
Notice how clean the main program looks! Each formula is neatly wrapped in its own function. If you need to use any of these formulas again elsewhere, you can just call the function.
Save your file with the name Cube Properties before moving on!

Subscribe to our newsletter.

Get updates to news and events.