0% found this document useful (0 votes)
6 views2 pages

Python_OOP_Cylinder_Tutorial

The document provides a tutorial on creating a Cylinder class using Object-Oriented Programming (OOP) in Python. It covers the basics of OOP, the creation of a Cylinder class with attributes and methods, instantiation of multiple Cylinder objects, and extending the class with functions to calculate surface area and volume. Additionally, it demonstrates how to sum the lateral surface area of cylinders with a radius greater than 2.

Uploaded by

nisssrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python_OOP_Cylinder_Tutorial

The document provides a tutorial on creating a Cylinder class using Object-Oriented Programming (OOP) in Python. It covers the basics of OOP, the creation of a Cylinder class with attributes and methods, instantiation of multiple Cylinder objects, and extending the class with functions to calculate surface area and volume. Additionally, it demonstrates how to sum the lateral surface area of cylinders with a radius greater than 2.

Uploaded by

nisssrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python OOP - Cylinder Class Tutorial

1A: Object Orientation vs Procedural Programming (4 points)


Object-Oriented Programming (OOP) organizes code around objects, which encapsulate data
(attributes) and behavior (methods), while Procedural Programming follows a linear approach
structured as functions and statements.

A class is a blueprint for creating objects. It defines properties (attributes) and behaviors (methods).
class Example:
def __init__(self, value):
self.value = value
def show_value(self):
print('Value:', self.value)

obj = Example(10)
obj.show_value() # Output: Value: 10

1B: Creating a Cylinder Class (2 points)


class Cylinder:
def __init__(self, radius, height):
self.radius = radius
self.height = height

1C: Creating 50 Cylinder Instances (6 points)


cylinders = []

for radius in range(1, 6):


for height in range(1, 11):
cylinders.append(Cylinder(radius, height))

print(f'Cylinder 1: radius={cylinders[0].radius}, height={cylinders[0].height}')


print(f'Cylinder 50: radius={cylinders[-1].radius}, height={cylinders[-1].height}')

1D: Extending Class with Functions (6 points)


import math

class Cylinder:
def __init__(self, radius, height):
self.radius = radius
self.height = height
def calculate_surface_area(self):
return 2 * math.pi * self.radius * (self.radius + self.height)

def calculate_volume(self):
return math.pi * (self.radius ** 2) * self.height

def calculate_lateral_surface(self):
return 2 * math.pi * self.radius * self.height

# Calling functions for first and last cylinder


print(f'First Cylinder Surface Area: {cylinders[0].calculate_surface_area()}')
print(f'Last Cylinder Surface Area: {cylinders[-1].calculate_surface_area()}')

1E: Summing Lateral Surface for Radius > 2 (2 points)


total_lateral_surface = sum(c.calculate_lateral_surface() for c in cylinders if c.radius
> 2)
print(f'Total Lateral Surface Area (radius > 2): {total_lateral_surface}')

You might also like