0% found this document useful (0 votes)
3 views8 pages

PP Lab Programs

The document contains several Python programs demonstrating basic geometry calculations, error handling in division, and graphical representations using matplotlib. It includes functions to calculate the area of rectangles and circles, a safe division function with error handling, and various programs to draw rectangles, points, and circles on a plot. Each program is accompanied by example usage and expected output.

Uploaded by

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

PP Lab Programs

The document contains several Python programs demonstrating basic geometry calculations, error handling in division, and graphical representations using matplotlib. It includes functions to calculate the area of rectangles and circles, a safe division function with error handling, and various programs to draw rectangles, points, and circles on a plot. Each program is accompanied by example usage and expected output.

Uploaded by

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

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:

You might also like