Week 5 2nd program:
# simple_geometry module
def area_rectangle(length,width):
return length*width
def area_circle(radius):
return 3.14*radius*radius
import simple_geometry
rect_len=4
rect_wid=6
area_rec=simple_geometry.area_rectangle(rect_len,rect_wid)
print(f"rectangle area:{area_rec}")
circ_rad=3
area_circ=simple_geometry.area_circle(circ_rad)
print(f"circle area:{area_circ}")
Output:
rectangle area:24
circle area:28.259999999999998
Week 5 3rd program:
def safe_divide(a, b):
try:
result = a / b
print(f"Result: {result}")
except ZeroDivisionError:
print("Error: Cannot divide by zero.")
except TypeError:
print("Error: Both inputs must be numbers.")
except Exception as e:
print(f"Unexpected error: {e}")
finally:
print("Operation complete.")
safe_divide(10,2)
safe_divide(10,0)
safe_divide(10,'a')
Output:
Result: 5.0
Operation complete.
Error: Cannot divide by zero.
Operation complete.
Error: Both inputs must be numbers.
Operation complete.
Week 6 1st bit:
a.
program:
import matplotlib.pyplot as plt
def draw_rectangle(x, y, width, height):
fig, ax = plt.subplots()
rectangle = plt.Rectangle((x, y), width, height, fill=None, edgecolor='blue', linewidth=2)
ax.add_patch(rectangle)
ax.set_xlim(x - 10, x + width + 10)
ax.set_ylim(y - 10, y + height + 10)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
# Example usage
draw_rectangle(2, 3, 5, 8)
Output:
b.
program:
import matplotlib.pyplot as plt
# Create a figure and axis
fig, ax = plt.subplots()
# Draw a rectangle (x, y, width, height)qq
rectangle = plt.Rectangle((0.2, 0.2), 0.5, 0.3, color='blue')
# Add the rectangle to the plot
ax.add_patch(rectangle)
# Set the limits of the plot
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
# Display the plot
plt.show()
Output:
c.
program:
import matplotlib.pyplot as plt
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def draw_point_colab(point: Point, color='black', size=50):
plt.figure(figsize=(3, 2))
plt.scatter(point.x, point.y, color=color, s=size)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Point Visualization")
plt.grid(True)
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.show() # Make sure this line is here
if __name__ == '__main__':
point1 = Point(5, 5)
draw_point_colab(point1, color='red', size=100)
point2 = Point(10, 7)
draw_point_colab(point2, color='green', size=100)
output:
d.
program:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
class Circle:
def __init__(self, x, y, r, color='blue'):
self.x = x
self.y = y
self.r = r
self.color = color
def draw_circle(circle, ax):
patch = patches.Circle((circle.x, circle.y), circle.r, facecolor=circle.color, edgecolor='black')
ax.add_patch(patch)
if __name__ == '__main__':
c1 = Circle(5, 5, 2, 'red')
c2 = Circle(10, 7, 3, 'green')
fig, ax = plt.subplots(figsize=(6, 6))
ax.set_aspect('equal')
ax.set_xlim(0, 15)
ax.set_ylim(0, 15)
draw_circle(c1, ax)
draw_circle(c2, ax)
plt.grid(True)
plt.show()
output: