Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views12 pages

PP Part A

The document contains a series of programming tasks for a class assignment, including creating unique lists, calculating areas of shapes, manipulating tuples, counting letter frequencies, and handling exceptions. It also includes examples of using pandas to join data frames and reading from a text file to count words, lines, and characters. Each part features code snippets along with sample outputs demonstrating the expected results.

Uploaded by

sharank23416
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)
9 views12 pages

PP Part A

The document contains a series of programming tasks for a class assignment, including creating unique lists, calculating areas of shapes, manipulating tuples, counting letter frequencies, and handling exceptions. It also includes examples of using pandas to join data frames and reading from a text file to count words, lines, and characters. Each part features code snippets along with sample outputs demonstrating the expected results.

Uploaded by

sharank23416
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/ 12

Name:

Class: II BCA ‘A’


Roll No:224
Part A 1: Write a program to create List N elements. Find all the unique
elements in the list. If an element is found only in the list, then add that
element to the unique list.
b = ["Quora", "Amazon", "Amazon", "Google", "Microsoft",
"Quora", "Google", 1]
uniq_list = []
for companies in b:
if companies not in uniq_list:
uniq_list.append(companies)
else:
continue
print(uniq_list)

OUTPUT:

['Quora', 'Amazon', 'Google', 'Microsoft', 1]

Process finished with exit code 0


Part A2: Using user defined functions to find the area of rectangle,
square, circle and triangle by accepting Suitable Input parameters from
the user.
import math
def rec(l, b):
area = l*b
return area

def squ(s):
area=s*s
return area

def cir(r):
area=math.pi*r*r
return area

def tri(b, h):


area = 0.5*b*h
return area

print("Rectangle")
l=float(input("Enter length of the rectangle:"))
b=float(input("Enter breadth of the rectangle:"))
print("The area of the rectangle is:",rec(l, b))

print("Square")
s=int(input("Enter sides of the square:"))
print("The area of the square is:", squ(s))

print("Circle")
r=int(input("Enter radius of the circle:"))
print("The area of the square is:",'%.2f'%cir(r))

print("Triangle")
b=float(input("Enter the base off the triangle:"))
h=float(input("Enter height of the triangle:"))
print("The area of the triangle is:",tri(b, h))
OUTPUT:
Rectangle
Enter length of the rectangle:10
Enter breadth of the rectangle:5
The area of the rectangle is: 50.0
Square
Enter sides of the square:3
The area of the square is: 9
Circle
Enter radius of the circle:6
The area of the square is: 113.10
Triangle
Enter the base of the triangle:10
Enter height of the triangle:5
The area of the triangle is: 25.0
Part A3: Consider a tuple T1= (1, 2, 5, 7, 9, 2, 4, 6, 8, 10)
Write a program to perform following operation.
a. print half the values of tuple in one line and the other half in the
next line.
b. Print another tuple whose values are even numbers in the given
tuple.
c. Concatenate a tuple t2=(11,13,15) with t1.
d. Return maximum and minimum value from this tuple.
t1=(1,2,5,7,9,2,4,6,8,10)
print("The first half of t1 is:",t1[:5])
print("The second half of t1 is:",t1[5:])
le=list()
for x in t1:
if x%2==0:
le.append(x)
t1even=tuple(le)
print("The even numbers of t1 is:",t1even)
t2=(11,13,15)
t1+=t2
print("Concatenation of t1 and t2 is:",t1)
print("Maximum of t1 tuple is:",max(t1))
print("Minimum of t1 tuple is:",min(t1))

OUTPUT:
The first half of t1 is: (1, 2, 5, 7, 9)
The second half of t1 is: (2, 4, 6, 8, 10)
The even numbers of t1 is: (2, 2, 4, 6, 8, 10)
Concatenation of t1 and t2 is: (1, 2, 5, 7, 9, 2, 4, 6, 8, 10, 11, 13, 15)
Maximum of t1 tuple is: 15
Minimum of t1 tuple is: 1
Part A4: Write a function that takes a sentence as input from the user
and calculates the frequency of each letter. Use a variable of dictionary
type to maintain the count.
def cal(str):
str=input("Enter the string:")
f={}
for s in str:
if s in f:
f[s]+=1
else:
f[s]=1
return f
print("The frequency of each letter is")
for key, value in cal(str).items():
print(key,"repeated for",value,"times")

OUTPUT:
The frequency of each letter is
Enter the string: Python
P repeated for 1 times
y repeated for 1 times
t repeated for 1 times
h repeated for 1 times
o repeated for 1 times
n repeated for 1 times
Part A5: Write a function nearly equal to test whether two strings are
nearly equal. Two strings A and B are nearly equal if the one-character
change in B results in string A.
def nearlyequal(str1,str2):
count=0
i=j=0
while(i<len(str1) and j<len(str2)):
if(str1[i]!=str2[j]):
count=count+1
if(len(str1)>len(str2)):
i=i+1
elif(len(str1)==len(str2)):
pass
else:
i=i-1
if(count>1):
return False
i=i+1
j=j+1
if(count<2):
return True
str1=input("Enter first string::\n")
str2=input("Enter second string::\n")
boolean=nearlyequal(str1,str2)
if(boolean):
print("Strings are nearly equal")
else:
print("Strings are not equal")

OUTPUT:
enter first string::
python
enter second string::
jython
strings are nearly equal
OUTPUT 2:
enter first string::
python
enter second string::
pearl
strings are not equal
Part A6: Write a program to create a text file and compute the number
of characters, words and lines in a file.
numberofwords=0
numberoflines=0
numberofcharacters=0
with open("data.txt",'r')as file:
for line in file:
numberofwords+=len(line.split())
numberoflines+=1
for letter in line:
for i in letter:
if(i!="" and i!="\n"):
numberofcharacters+=1
print("no of words:", numberofwords)
print("no of lines:",numberoflines)
print("no of characters:",numberofcharacters)

OUTPUT:
Hello
world
no of words: 2
no of lines: 2
no of characters: 10
Part A7: Using user defined exception class that will ask the user to
enter a number until he guesses a stored number correctly. To help
them figure out, a hint is provided whether they are guess is greater
than or less than the stored number using the user defined exception.
import random
class user_err(Exception):
def __init__(self,msg,n):
self.msg=msg
self.n=n
storenum=random.randint(1,10)
chance=0
while chance<4:
n=int(input("Enter your guess:"))
try:
if n>storenum:
msg="Number should be lesser"
elif n<storenum:
msg="Number should be greater"
elif n==storenum:
print("Your guess is correct")
break
raise user_err(msg,n)
except user_err as ue:
print(f"Number entered:{ue.n}\n exception:
{ue.msg} than{ue.n}")
chance+=1
if chance>3:
print(f"You have exceed your choice\n The answer
is :{storenum}")

OUTPUT 1:
Enter your guess:5
Number entered:5
exception:Number should be greater than 5
Enter your guess: 6
Number entered:6
exception: Number should be greater than6
Enter your guess:8
Your guess is correct

OUTPUT 2:
Enter your guess:10
Number entered:10
exception:Number should be lesser than10
Enter your guess:9
Number entered:9
exception:Number should be lesser than9
Enter your guess:6
Number entered:6
exception:Number should be greater than6
Enter your guess:8
Number entered:8
exception:Number should be lesser than8
You have exceeded your choice
The answer is :7
Part A 8: Write a panda’s program to join the two given data frames
along the row. Sample data frame may contain details of student like
roll number named total mass.
import pandas as pd
studentdata1=pd.DataFrame({
'studentid':
['s1001','s1002','s1003','s1004','s1005'],
'name':['Roopa','Reema','Anand','Shekar','Thanuja'],
'mark':[200,210,190,222,199]})
studentdata2=pd.DataFrame({
'studentid':
['s1006','s1007','s1008','s1009','s10010'],
'name':['Raj','Charles','Rita','William','Joe'],
'mark':[201,200,198,219,201]})
print("Original data frames:")
print(studentdata1)
print("---------------------")
print(studentdata2)
print("\nJoin the said two dataframes along rows:")
resultdata=pd.concat([studentdata1,studentdata2])
print(resultdata)

OUTPUT:
Original data frames:
studentid name mark
0 s1001 Roopa 200
1 s1002 Reema 210
2 s1003 Anand 190
3 s1004 Shekar 222
4 s1005 Thanuja 199
-----------------------------------
studentid name mark
0 s1006 Raj 201
1 s1007 Charles 200
2 s1008 Rita 198
3 s1009 William 219
4 s10010 Joe 201

Join the said two data frames along rows:


studentid name mark
0 s1001 Roopa 200
1 s1002 Reema 210
2 s1003 Anand 190
3 s1004 Shekar 222
4 s1005 Thanuja 199
0 s1006 Raj 201
1 s1007 Charles 200
2 s1008 Rita 198
3 s1009 William 219
4 s10010 Joe 201

Process finished with exit code 0

You might also like