0% found this document useful (0 votes)
3 views

PLC Lab Programs

The document contains multiple Python programming tasks, including reading student details and calculating total marks and percentage, determining if a person is a senior citizen, generating Fibonacci sequences, calculating factorials and binomial coefficients, analyzing a list of numbers for mean and variance, counting digit frequencies, and processing text files for word frequency and sorting. It also includes tasks for backing up folders into ZIP files, handling exceptions in division operations, adding complex numbers, and creating a student class to manage marks and display scorecards. Each task is presented with code snippets and explanations for implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PLC Lab Programs

The document contains multiple Python programming tasks, including reading student details and calculating total marks and percentage, determining if a person is a senior citizen, generating Fibonacci sequences, calculating factorials and binomial coefficients, analyzing a list of numbers for mean and variance, counting digit frequencies, and processing text files for word frequency and sorting. It also includes tasks for backing up folders into ZIP files, handling exceptions in division operations, adding complex numbers, and creating a student class to manage marks and display scorecards. Each task is presented with code snippets and explanations for implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.a.

Write a program to read the student details like Name, USN, and Marks in
three subjects.Display the student details, total marks and percentage with
suitable messages
name=input("Enter student name")
usn=input("Enter student USN")
sub1=input("Enter subject1 marks")
sub1=int(sub1)
sub2=input("Enter subject1 marks")
sub2=int(sub2)
sub3=input("Enter subject1 marks")
sub3=int(sub3)
total_marks=(sub1+sub2+sub3)
perce_marks=(total_marks/300)*100
print("-----------------------------------------------------")
print("Stuudent name :-", name)
print("Student USN :-",usn )
print("Total marks of all three subjects is:-",total_marks)
print("Average of all the three subject is:-",perce_marks)
if(perce_marks>=70):
print(name+" "+ "is passed in Distinction")
elif(perce_marks>=60 and perce_marks<=70):
print(name+" "+ "is passed in First Class")
elif(perce_marks>=50 and perce_marks<60):
print(name+" "+ "is passed in Second Class")
elif(perce_marks>=35 and perce_marks<50):
print(name+" "+ "is passed in below second class")
else:
print(name+" "+ "is Failed")
print("-----------------------------------------------------")

1.b. Develop a program to read the name and year of birth of a person.
Display whether the person is a senior citizen or not

name=input("Enter person name")


year=input("Enter birth year")
year=int(year)
age=2023-year
if(age>=60):
print(name+" "+"belongs to senior citizen")
else:
print(name+" "+ "not belongs to senior citizen")
2 a. Develop a program to generate Fibonacci sequence of length (N) . Read N
from the console.

n=input("Enter the range of fibonacci series")


n=int(n)
fib1=0
fib2=1
print("The following series is the fibonacci series")
print(fib1)
print(fib2)
fib3=fib1+fib2
while(fib3<=n):
print(fib3)
fib1=fib2
fib2=fib3
fib3=fib1+fib2

2.b. Write a function to calculate factorial of a number. Develop to


compute binomial coefficient (Given N and R)

def factorial(m):
fact=1
for i in range(1,m+1):
fact=fact*i
return fact

n=input("Enter n value")
n=int(n)
nfact=factorial(n)
r=input("Enter r value")
r=int(r)
rfact=factorial(r)
nr=n-r
nrfact=factorial(nr)
bin_coef=nfact/(rfact*nrfact)
print("The binimial coefficient result is :-", bin_coef)
3. Read N numbers from the console and create a list. Develop a program
print mean , variance and standard deviation with suitable message.

import numpy as np
l1=[] # initially l1 list is empty
n=1 # initial value of n=1
print(“Enter list elements until ‘0’ press”)
while (n!=0): # new elements are added at the end of the list
until n=0, breaks the while loop
n=input()
n=int(n)
if(n==0):
break
l1.append(n)
print("All the elements in the list are ")
print(l1)
print("Average of all the elements in the list are")
print(np.average(l1))
print("Variance of all the elements in the list are")
print(np.var(l1))
print("Standard deviation of all the elements in the list are")
print(np.std(l1))

4. Read a multi-digit number (as chars) from the console. Develop a program
to print the frequency of each digit with suitable message

str1=input("Enter a maximum number")


len(str1)
zero,one,two,three,four,five,six,seven,eight,nine=0,0,0,0,0,0,0,0
,0,0
for i in range(len(str1)):
if (str1[i]=='0'):
zero=zero+1
if (str1[i]=='1'):
one=one+1
if (str1[i]=='2'):
two=two+1
if (str1[i]=='3'):
three=three+1
if (str1[i]=='4'):
four=four+1
if (str1[i]=='5'):
five=five+1
if (str1[i]=='6'):
six=six+1
if (str1[i]=='7'):
seven=seven+1
if (str1[i]=='8'):
eight=eight+1
if (str1[i]=='9'):
nine=nine+1
print("Number of zeros in the digit is :-",zero)
print("Number of ones in the digit is :-",one)
print("Number of twos in the digit is :-",two)
print("Number of threes in the digit is :-",three)
print("Number of fours in the digit is :-",four)
print("Number of fives in the digit is :-",five)
print("Number of sixes in the digit is :-",six)
print("Number of sevens in the digit is :-",seven)
print("Number of eights in the digit is :-",eight)
print("Number of nines in the digit is :-",nine)

5. Develop a program to print 10 most frequently appearing words in a text


file. [Hint : Use Dictionary with distinct words and their frequency of
occurrences. Sort the dictionary in the reverse order of frequency and display
dictionary slice of first 10 items ]

# Open the file in read mode


text = open('D:\g.txt','r')

# Create an empty dictionary


d = dict()
text=text.readlines()
# Loop through each line of the file
for line in text:
# Remove the leading spaces and newline character
line = line.strip()

# Convert the characters in line to


# lowercase to avoid case mismatch

# Split the line into words


words = line.split(" ")
# Iterate over each word in line
for word in words:
# Check if the word is already in dictionary
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1

# Print the contents of dictionary


for key in list(d.keys()):
print(key, ":", d[key])
sort1 = sorted(d.items(), key=lambda x:x[1],reverse=True)
print(sort1)

# Initialize limit
N = 6

# Using items() + list slicing


# Get first K items in dictionary
out = dict(list(d.items())[0: N])

# printing result
print("First " + str(N) + " elements in dictionary is : ")
print(str(out))

6. Develop a program to sort the contents of a text file and


write the sorted contents into a separate text file

infile = open('D:\g.txt','r')
words = []
for line in infile:
temp = line.split()
for i in temp:
words.append(i)
infile.close()
print(words)
words.sort()
print(words)
outfile = open("result2.txt", "w")
for i in words:
outfile.writelines(i)
outfile.writelines(" ")
outfile.close()

7. Develop a program to backing up a given folder (Folder in a


current working directory ) into a ZIP file by using relevant
modules and suitable methods

import zipfile
newzip=zipfile.ZipFile('new6.zip','w')
newzip.write('D:\g.txt',compress_type=zipfile.ZIP_DEFLATED)
newzip.close()

8. Write a function named DivExp which takes TWO parameters a,b


and returns a value c(c=a/b). Write suitable assertion for a>0 in
function DivExp and raise an exception for when b=0. Develop a
suitable program which reads two values from the console and
calls a function DivExp

9. Define a function which takes two objects representing


complex numbers and return new complex number with a addition
of two complex numbers. Develop a program to read N (N>=2)
complex numbers and to compute the addition of N complex numbers

class Complex ():


def initComplex(self):
self.realPart = int(input("Enter the Real Part: "))
self.imgPart = int(input("Enter the Imaginary Part:"))

def display(self):
print(self.realPart,"+",self.imgPart,"i", sep="")

def sum(self, c1, c2):


self.realPart = c1.realPart + c2.realPart
self.imgPart = c1.imgPart + c2.imgPart

c1 = Complex()
c2 = Complex()
c3 = Complex()
print("Enter first complex number")
c1.initComplex()
print("First Complex Number: ", end="")
c1.display()

print("Enter second complex number")


c2.initComplex()
print("Second Complex Number: ", end="")
c2.display()
c3.sum(c1,c2)
c3.display()

10. Develop a program that uses class student which prompts the
user to enter marks in three subjects and calculate total marks
,percentage and displays the score card details.[Hint. Use list
to store the marks in three subjects and total marks.Use
__init__method to initialize name,USN,and the lists to store
marks and total.Use getMarks method to read marks into the list
and display() method to display the score card details]

class student:
def __init__(self,usn,name,marks):
self.usn=usn
self.name=name
self.marks=marks
def getMarks(self):
total_marks=0
print("The details of marks belongs to " + self.name + " is as
follows")
for i in self.marks:
print(i)
total_marks=total_marks+i
print("The total marks of all three subjects is :-
",total_marks)
def display(self):
print(self.usn)
print(self.name)
total_marks=0
avg_marks=0.0
print("The details of marks belongs to " + self.name + " is as
follows")
for i in self.marks:
print(i)
total_marks=total_marks+i
print("The total marks of all three subjects is
:- ",total_marks)
avg_marks=(total_marks/3)
print("The average of all three subjects is :-",avg_marks)

obj1=student('1DB22CS001','VEDANTA',[56,78,90])
obj1.getMarks()
obj1.display()

You might also like