Question

Write a simple Python module that solves a problem.



Answers

There are no human answers yet.
Answered by GPT-4o mini
Sure! Below is a simple Python module that provides functionality to calculate the area and perimeter of common geometric shapes: a rectangle and a circle.

### Module: `geometry.py`

```python
import math

class Geometry:
@staticmethod
def rectangle_area(length, width):
"""Calculate the area of a rectangle."""
return length * width

@staticmethod
def rectangle_perimeter(length, width):
"""Calculate the perimeter of a rectangle."""
return 2 * (length + width)

@staticmethod
def circle_area(radius):
"""Calculate the area of a circle."""
return math.pi * (radius ** 2)

@staticmethod
def circle_circumference(radius):
"""Calculate the circumference of a circle."""
return 2 * math.pi * radius


# Example usage
if __name__ == "__main__":
# Example for rectangle
length = 5
width = 3
print(f"Rectangle Area: {Geometry.rectangle_area(length, width)}")
print(f"Rectangle Perimeter: {Geometry.rectangle_perimeter(length, width)}")

# Example for circle
radius = 4
print(f"Circle Area: {Geometry.circle_area(radius)}")
print(f"Circle Circumference: {Geometry.circle_circumference(radius)}")
```

### How to Use the Module

1. **Save the above code into a file named `geometry.py`.**
2. **To use this module, you can import it in another Python script.** Here’s a quick example of how you might do so:

```python
from geometry import Geometry

# Using the Geometry module
length = 10
width = 5
print("Rectangle Area:", Geometry.rectangle_area(length, width))
print("Rectangle Perimeter:", Geometry.rectangle_perimeter(length, width))

radius = 7
print("Circle Area:", Geometry.circle_area(radius))
print("Circle Circumference:", Geometry.circle_circumference(radius))
```

### Explanation

- The module contains a class `Geometry` with static methods for calculating:
- Area and perimeter of a rectangle.
- Area and circumference of a circle.
- Each method takes necessary parameters and returns calculated values.
- The module also includes a simple example to demonstrate its functionality when run directly.

You can further expand this module by adding more shapes or functionalities as needed!

Related Questions