1]
#python program
def printPicnic(itemsDict, leftWidth, rightWidth):
print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-'))
for k, v in [Link]():
print([Link](leftWidth, '.') + str(v).rjust(rightWidth))
# Defining the picnic items dictionary
picnicItems = {'sandwiches': 4,'apples': 12,'cups': 4,'cookies': 8000}
# Printing the picnic items with different widths
printPicnic(picnicItems, 12, 5)
printPicnic(picnicItems, 20, 6)
2]
#python program
import re
# Sample text for testing
text = """
From: alice@[Link]
To: bob@[Link]
"""
from_regex = [Link](r'From: (\S+@\S+)')
to_regex = [Link](r'To: (\S+@\S+)')
from_addresses = from_regex.findall(text)
print("From addresses:", from_addresses)
# Extract 'To' email addresses
to_addresses = to_regex.findall(text)
print("To addresses:", to_addresses)
3]
Class: A class in Python is a blueprint for creating objects. It defines a set of attributes
and methods that the created objects can use.
Object: An object is an instance of a class. When a class is defined, no memory is
allocated until an object of that class is instantiated.
Attribute: Attributes are variables that belong to a class or an
object. They are used to store data for objects created from the class
#python program
# Define a class named Person
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
def display(self):
print(f"Name: {[Link]}")
print(f"Age: {[Link]}")
person1 = Person("Alice", 30)
print("Person 1 Details:")
[Link]()
person2 = Person("Bob", 25)
print("\nPerson 2 Details:")
[Link]()
4]
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Rectangle:
def __init__(self, width=100.0, height=200.0, corner=None):
[Link] = width
[Link] = height
if corner is None:
[Link] = Point() # Default corner at (0.0, 0.0)
else:
[Link] = corner # Custom corner point
# Creating an instance of Rectangle
box = Rectangle()
# Displaying the attributes of the rectangle
print("Width:", [Link])
print("Height:", [Link])
print("Corner x:", [Link].x)
print("Corner y:", [Link].y)
OUTPUT:
Width: 100.0
Height: 200.0
Corner x: 0.0
Corner y: 0.0