0% found this document useful (0 votes)
2 views5 pages

Python Lab Programs

The document contains a series of Python lab programs that demonstrate various programming concepts such as classes, inheritance, method overloading, and file handling. Each program includes user input functionality and showcases different functionalities like calculating power, word frequency, and geometric area. The examples range from basic operations to more complex structures like abstract classes and operator overloading.

Uploaded by

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

Python Lab Programs

The document contains a series of Python lab programs that demonstrate various programming concepts such as classes, inheritance, method overloading, and file handling. Each program includes user input functionality and showcases different functionalities like calculating power, word frequency, and geometric area. The examples range from basic operations to more complex structures like abstract classes and operator overloading.

Uploaded by

chp5494
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Lab Programs (User Input Based)

1A. Power Function


class Power:
def power(self, x, n):
return x ** n

x = int(input("Enter base: "))


n = int(input("Enter power: "))

obj = Power()
print("Result =", [Link](x, n))

1B. Word Frequency in File


file = open("[Link]", "w")
text = input("Enter text: ")
[Link](text)
[Link]()

file = open("[Link]", "r")


data = [Link]().split()

freq = {}

for word in data:


freq[word] = [Link](word, 0) + 1

print(freq)
[Link]()

2A. Student Class


class Student:
def __init__(self, name, roll):
[Link] = name
[Link] = roll

def dataprint(self):
print("Name:", [Link])
print("Roll:", [Link])

name = input("Enter name: ")


roll = input("Enter roll: ")

s = Student(name, roll)
[Link]()

3A. Three Elements Sum to Zero


class SumZero:
def find(self, arr):
result = []

for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
if arr[i] + arr[j] + arr[k] == 0:
[Link]([arr[i], arr[j], arr[k]])
return result

n = int(input("Enter number of elements: "))


arr = []

for i in range(n):
[Link](int(input("Enter element: ")))
obj = SumZero()
print([Link](arr))

4A. Car Class


class Car:
def __init__(self, model, year, price):
[Link] = model
[Link] = year
[Link] = price

def cost(self):
print("Price =", [Link])

model = input("Enter model: ")


year = input("Enter year: ")
price = int(input("Enter price: "))

c = Car(model, year, price)


[Link]()

5A. Method Overloading


class Demo:
def add(self, a, b=0, c=0):
print("Sum =", a+b+c)

obj = Demo()

[Link](10, 20)
[Link](10, 20, 30)

6A. Bank Account


class Account:
def __init__(self):
[Link] = 0

def deposit(self):
amount = int(input("Enter deposit amount: "))
[Link] += amount

def withdraw(self):
amount = int(input("Enter withdraw amount: "))
[Link] -= amount

def display(self):
print("Balance =", [Link])

obj = Account()

[Link]()
[Link]()
[Link]()

7A. Derived Class Top10 Students


class Student:
def details(self):
print("Student Details")

class Top10(Student):
def topstudents(self):
for i in range(1, 11):
print("Top Student", i)

obj = Top10()
[Link]()
[Link]()
8A. Single Inheritance
class Parent:
def show1(self):
print("Parent Class")

class Child(Parent):
def show2(self):
print("Child Class")

obj = Child()
obj.show1()
obj.show2()

9A. Multiple Inheritance


class A:
def showA(self):
print("Class A")

class B:
def showB(self):
print("Class B")

class C(A, B):


def showC(self):
print("Class C")

obj = C()
[Link]()
[Link]()
[Link]()

10A. Multilevel Inheritance


class A:
def showA(self):
print("Class A")

class B(A):
def showB(self):
print("Class B")

class C(B):
def showC(self):
print("Class C")

obj = C()
[Link]()
[Link]()
[Link]()

11A. Hierarchical Inheritance


class Parent:
def show(self):
print("Parent")

class Child1(Parent):
pass

class Child2(Parent):
pass

obj1 = Child1()
obj2 = Child2()

[Link]()
[Link]()

12A. Polygon Abstract Class


from abc import ABC, abstractmethod

class Polygon(ABC):
@abstractmethod
def area(self):
pass

class Rectangle(Polygon):
def area(self):
l = int(input("Enter length: "))
b = int(input("Enter breadth: "))
print("Area =", l*b)

class Triangle(Polygon):
def area(self):
b = int(input("Enter base: "))
h = int(input("Enter height: "))
print("Area =", 0.5*b*h)

r = Rectangle()
[Link]()

t = Triangle()
[Link]()

13A. Operator Overloading


class Demo:
def __init__(self, x):
self.x = x

def __add__(self, other):


return self.x + other.x

a = Demo(10)
b = Demo(20)

print("Sum =", a + b)

14A. Method Overriding


class Parent:
def show(self):
print("Parent Method")

class Child(Parent):
def show(self):
print("Child Method")

obj = Child()
[Link]()

15A. Constructor Overriding


class Parent:
def __init__(self):
print("Parent Constructor")

class Child(Parent):
def __init__(self):
print("Child Constructor")

obj = Child()
16A. Rectangle Area
class Rectangle:
def __init__(self, length, width):
[Link] = length
[Link] = width

def area(self):
return [Link] * [Link]

l = int(input("Enter length: "))


w = int(input("Enter width: "))

obj = Rectangle(l, w)

print("Area =", [Link]())

17A. Reverse String Word by Word


class Reverse:
def reverse_words(self, text):
words = [Link]()
[Link]()
return " ".join(words)

text = input("Enter string: ")

obj = Reverse()

print(obj.reverse_words(text))

You might also like