PDA Lab Prog (Short)
PDA Lab Prog (Short)
LAB PROGRAM 1 Write a python program to find sum of n natural numbers using recursive function
def naturalSum(n):
if n == 1:
return 1
else:
return n+naturalSum(n-1)
res = naturalSum(n)
LAB PROGRAM 2 Write a Python Program to Create a Dictionary with Key as First Character and Value
as Words Starting with that Character.
words = ["apple","banana","coconut","durian","apricot","corn","berry","dalgona"]
d = {}
fchar = word[0]
if fchar in d:
d[fchar].append(word)
else:
d[fchar] = [word]
print(d)
LAB PROGRAM 3 Implement a Python program to count the numbers of characters in the string and
store them in a dictionary data structure
length = 0
d = {}
for ch in string:
continue
length += 1
for ch in string:
continue
if ch not in d:
d[ch] = 1
else:
d[ch] += 1
LAB PROGRAM 4 Design and Develop a Python Program to Append, Delete and Display Elements of a
List Using Classes and Objects.
class ListOperation:
def __init__(self):
self.List = []
if x in self.List:
self.List.remove(x)
else:
def Display(self):
if not self.List:
print("Empty List")
else:
for x in self.List:
print(x, "\t")
def main():
Operation = ListOperation()
isRunning = True
while isRunning:
if choice == 1:
Operation.Append(x)
elif choice == 2:
Operation.Delete(x)
elif choice == 3:
Operation.Display()
elif choice == 4:
isRunning = False
else:
print("Invalid choice")
if __name__ == "__main__":
main()
LAB PROGRAM 5 Demonstrate the concept of Method Resolution order in multiple inheritance in
Python Program.
class A:
def show(self):
class B(A):
def show(self):
class C(A):
def show(self):
class D(B,C):
pass
d = D()
d.show()
print(D.mro())
LAB PROGRAM 6 Design and Implement a Python Program to perform addition, subtraction,
multiplication of two complex numbers using binary operators overloading.
class complexnumber:
def __init__(self,real,imag):
self.real = real
self.imag = imag
def __add__(self,other):
def __sub__(self,other):
def __mul__(self,other):
def __str__(self):
c1 = complexnumber(3,7)
c2 = complexnumber(1,10)
LAB PROGRAM 7 Demonstrate with a python program to show the speed of execution is more when
using numpy array
import numpy as np
import time
size = 1000000
list1 = range(size)
list2 = range(size)
array1 = np.arange(size)
array2 = np.arange(size)
initialtime = time.time()
initialtime = time.time()
resultantArr = array1*array2
LAB PROGRAM 8 Write a program to read the data and perform correlation, Two way conditional
probability, joint probability and marginal probability.
import pandas as pd
import numpy as np
data = {
'A' : [1,1,0,1],
'B' : [0,1,1,0]
df = pd.DataFrame(data)
correlation = df.corr()
print("\nCorrelation:\n",correlation)
print("\nJoint probability:\n",jointProb)
margProbA = jointProb.sum(axis = 1)
margProbB = jointProb.sum(axis = 0)
lab program 9. Write a program to analyse the given data and perform the operation to find the
missing data.
import pandas as pd
data = {
'Name':['Aditya','Akash',None,'Ayush'],
'city':['Bihar','Blr','Delhi', None]
df = pd.DataFrame(data)
print(df)
print("\n")
a = df.isnull().any(axis=0)
b = df.isnull().sum(axis=0)
print(a)
print("\n")
print(b)
c = df.isnull().any(axis = 1)
d = df.isnull().sum(axis = 1)
print("\n")
print(c)
print("\n")
print(d)
LAB PROGRAM 10 :Read the data set and perform scatter plot, Histogram and Bar plot using
Matplotlib in libraries
import pandas as pd
data =
pd.read_csv('https://raw.githubusercontent.com/Adityarish/pythonlabExternal/refs/heads/main/datase
t.csv')
print(data.head())
plt.figure(figsize = (8,6))
plt.title("Scatter plot")
plt.xlabel("Column 1")
plt.ylabel("Column 2")
plt.grid(True)
plt.show()
plt.figure(figsize = (8,6))
plt.title("Histogram")
plt.xlabel('Column 3')
plt.ylabel('frequency')
plt.show()
plt.figure(figsize = (8,6))
plt.title("Bar Plot")
plt.xlabel("Category")
plt.ylabel("frequency")
plt.xticks(rotation = 45)
plt.show()
LAB PRO 11 Read the data set and perform scatter plot, Histogram and Bar plots using seaborn library
import pandas as pd
df =
pd.read_csv('https://raw.githubusercontent.com/Adityarish/pythonlabExternal/refs/heads/main/data.c
sv')
plt.figure(figsize = (8,6))
plt.grid(True)
plt.show()
plt.figure(figsize = (8,6))
plt.show()
plt.figure(figsize = (8,6))
plt.show()
LAB PROGRAM 12 Read the data set and perform Box and whiskers plot using seaborn library
import pandas as pd
data =
pd.read_csv('https://raw.githubusercontent.com/Adityarish/pythonlabExternal/refs/heads/main/tips.cs
v')
plt.figure(figsize = (8,6))
plt.show()