Advanced Python Programming –Practical List
Lab Exercises:
1. Write a python program for implementing class with minimum of two
functions.
2. Write a python program involving the usage of Method Overloading.
3. Write a python program involving the usage of Operator Overloading.
4. Write a python program involving the usage of Inheritance.
5. Write a python program in involving the usage of Abstraction and
Encapsulation.
6. Write a python program to extract the Mailid from the String.
7. Write a python program to Create User defined exception.
8. Write a python program parse the string and the Inputdata.
9. Write a python program to extract dataframe using pandas.
10.Write a python program to do slicing with numpy for arrays.
# -----------------------------------------------
# Lab 1: Class with Minimum Two Functions
# -----------------------------------------------
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
print("Lab 1: Class Functions")
calc = Calculator()
print("Addition:", calc.add(5, 3))
print("Multiplication:", calc.multiply(5, 3))
print("\n")
# -----------------------------------------------
# Lab 2: Method Overloading using default arguments
# -----------------------------------------------
class Greet:
def hello(self, name=None):
if name:
print(f"Hello, {name}!")
else:
print("Hello!")
print("Lab 2: Method Overloading")
g = Greet()
g.hello()
g.hello("Alice")
print("\n")
# -----------------------------------------------
# Lab 3: Operator Overloading
# -----------------------------------------------
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __str__(self):
return f"({self.x}, {self.y})"
print("Lab 3: Operator Overloading")
p1 = Point(1, 2)
p2 = Point(3, 4)
print("Sum:", p1 + p2)
print("\n")
# -----------------------------------------------
# Lab 4: Inheritance
# -----------------------------------------------
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
print("Lab 4: Inheritance")
d = Dog()
d.speak()
print("\n")
# -----------------------------------------------
# Lab 5: Abstraction and Encapsulation
# -----------------------------------------------
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.__radius = radius # Encapsulation
def area(self):
return 3.14 * self.__radius ** 2
print("Lab 5: Abstraction & Encapsulation")
c = Circle(5)
print("Area of Circle:", c.area())
print("\n")
# -----------------------------------------------
# Lab 6: Extract Email ID from String
# -----------------------------------------------
import re
print("Lab 6: Extract Email")
text = "Please contact us at support@example.com for assistance."
emails = re.findall(r'\b[\w.-]+@[\w.-]+\.\w+\b', text)
print("Extracted Email:", emails)
print("\n")
# -----------------------------------------------
# Lab 7: User-defined Exception
# -----------------------------------------------
print("Lab 7: User-defined Exception")
class NegativeNumberError(Exception):
pass
try:
num = int(input("Enter a positive number: "))
if num < 0:
raise NegativeNumberError("Negative numbers are not allowed.")
else:
print("You entered:", num)
except NegativeNumberError as e:
print("Error:", e)
print("\n")
# -----------------------------------------------
# Lab 8: Parse String Input Data
# -----------------------------------------------
print("Lab 8: Parse String Data")
data = "Name: John, Age: 25, City: New York"
parsed = dict(item.split(": ") for item in data.split(", "))
print("Parsed Data:", parsed)
print("\n")
# -----------------------------------------------
# Lab 9: Extract DataFrame using Pandas
# -----------------------------------------------
print("Lab 9: Pandas DataFrame")
import pandas as pd
data = {
'Name': ['Alice', 'Bob'],
'Age': [24, 27],
'City': ['New York', 'London']
}
df = pd.DataFrame(data)
print("DataFrame:\n", df)
print("\n")
# -----------------------------------------------
# Lab 10: NumPy Array Slicing
# -----------------------------------------------
print("Lab 10: NumPy Slicing")
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original Array:\n", arr)
print("Second Row:", arr[1])
print("First 2 rows and 2 columns:\n", arr[:2, :2])
print("\n")