Skip to content
Suggested Lab. Exercises · Q7

Q.Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use the appropriate functions.

Punjab PsebTextbookSubjective· 5mImportance★★★★★est
98% · 39/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Group one user-defined function per shape-measurement inside a module file shapes.py (each accepting the dimensions as parameters and returning the computed value), then import that module in a separate program and call the functions.

The idea. When several related functions will be reused across programs, we place them in our own module — a .py file — and import it. Every function here takes the values needed for the calculation as parameters and returns the result (it does not print), which keeps the functions reusable.

Step 1 — the module (save as shapes.py):

# shapes.py
"""Area, perimeter and surface-area functions for common shapes."""
import math

def area_square(side):
    return side * side

def perimeter_square(side):
    return 4 * side

def area_rectangle(length, breadth):
    return length * breadth

def perimeter_rectangle(length, breadth):
    return 2 * (length + breadth)

def area_triangle(base, height):
    return 0.5 * base * height

def perimeter_triangle(a, b, c):
    return a + b + c

def area_circle(radius):
    return math.pi * radius * radius

def circumference_circle(radius):
    return 2 * math.pi * radius

def surface_area_cylinder(radius, height):
    # total surface area = 2*pi*r*(r + h)
    return 2 * math.pi * radius * (radius + height)

Step 2 — the main program (save as main.py in the same folder):

import shapes

print("Square side 5      -> area:", shapes.area_square(5),
      " perimeter:", shapes.perimeter_square(5))
print("Rectangle 4 x 6    -> area:", shapes.area_rectangle(4, 6),
      " perimeter:", shapes.perimeter_rectangle(4, 6))
print("Triangle b=6, h=4  -> area:", shapes.area_triangle(6, 4),
      " perimeter(3,4,5):", shapes.perimeter_triangle(3, 4, 5))
print("Circle r=7         -> area:", round(shapes.area_circle(7), 2),
      " circumference:", round(shapes.circumference_circle(7), 2))
print("Cylinder r=3, h=5  -> surface area:",
      round(shapes.surface_area_cylinder(3, 5), 2))
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.