[go: up one dir, main page]

0% found this document useful (0 votes)
37 views35 pages

Python Lab-1-1

Uploaded by

iamavp1234
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)
37 views35 pages

Python Lab-1-1

Uploaded by

iamavp1234
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
You are on page 1/ 35

RECORD NOTE BOOK

MASTER OF COMPUTER APPLICATIONS

SUBMITTED BY

NAME

REGISTER NO

SEMESTER III-SEMESTER

SUBJECT CODE 541302

SUBJECT LAB : PYTHON PROGRAMMING

DEPARTMENT OF COMPUTER APPLICATIONS


ALAGAPPA UNIVERSITY
(A State university Accredited with A+ Grade by NAAC (CGPA: 3.64) in the Third Cycle and
Graded as Category-I University by MHRD –UGC)

KARAIKUDI-630003
NOVEMBER-2022
ALAGAPPA UNIVERSITY
(A State university Accredited with A+ Grade by NAAC (CGPA: 3.64) in the Third Cycle and
Graded as Category-I University by MHRD –UGC)

KARAIKUDI-630003
DEPARTMENT OF COMPUTER APPLICATIONS

CERTIFICATE

REGISTER NO
COURSE CODE 541
COURSE M.C.A

Certified that this is the bonafide record work done by


in III-semester of MCA at the Department of Computer Applications,
Alagappa University, Karaikudi - NOVEMBER 2022.

HEAD OF THE DEPARTMENT STAFF-IN- CHARGE

Submitted for the university Practical examinations held on_____________at the


Department of Computer Applications, Alagappa University, Karaikudi.

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

SL.NO DATE NAME OF THE PROGRAM PAGE NO STAFF SIGN

1 QUADRATIC EQUATION

2 FIBONACCI SERIES USING RECURSION

3 CALCULATE FINE AMOUNT OF LIBRARY IN


RETURNING BOOKS

4 LIST OPERATIONS USING (APPEND, INSERT,


EXTEND)

5 MULTIPLE OPERATIONS IN DICTIONARY

6 REVERSE THE STRING FOR GIVEN WORD


WHICH IS NOT A PALINDROME

7 FIND 5TH MAXIMUM OF GIVEN NUMBERS

8 MAXIMUM OF THREE NUMBERS

9 FINDING NUMBER OF EVEN AND ODD


NUMBERS IN A DICTIONARY

10 FINDING NUMBER OF DAYS OCCUR IN A YEAR

11 DISPLAY EMPLOYEE DETAILS USING


INHERITANCE

12 METHOD OVERLOADING FOR DIFFERENT


TYPES OF ARGUMENTS

13 ADD TWO NUMBERS USING CONSTRUCTOR

14 ADDING POINTS USING OPERATOR


OVERLOADING

15 DISPLAY STUDENTS DETAILS USING CSV


QUADRATIC EQUATION:

Quadratic.py
import cmath
a=int(input("Enter a value:"))
b=int(input("Enter b value:"))
c=int(input("Enter c value:"))
dis=(b**2)-(4*a*c)
#print(dis)
if dis==0:
print("Roots are real & equal")
ans1=-b/(2*a)
print(ans1)
elif dis>0:
print("roots are real & different")
ans1=(-b+cmath.sqrt(dis))/(2*a)
ans2=(-b-cmath.sqrt(dis))/(2*a)
print(ans1)
print(ans2)
else:
print("roots are complex")
ans1=(-b/(2*a),"+i",cmath.sqrt(dis)/(2*a))
ans2=(-b/(2*a),"-i",cmath.sqrt(dis)/(2*a))
print(ans1)
print(ans2)
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
FIBONACCI SERIES USING RECURSION:

Fibonacci.py
def recur_fibo(n):
if n<=1:
return n
else:
return(recur_fibo(n-1)+recur_fibo(n-2))
nterms=int(input("How many terms?"))
if nterms<=0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
CALCULATE FINE AMOUNT OF LIBRARY IN RETURNING
BOOKS:

Display the fine.py


d=int(input("Enter number of days:"))
fine=0
if(d<=5):
fine=d*0.50
print("Fine:",float(fine))
elif(d>5 and d<=10):
i=d-5
fine=(i*1)+(5*0.5)
print("Fine:",float(fine))
elif(d>10 and d<=30):
i=d-10
fine=(i*5)+(5*0.5)+(5*1)
print("Fine:",float(fine))
else:
i=d-10
fine=(i*5)+(5*0.5)+(5*1)
print("Your Membership is cancelled")
print("Fine:",float(fine))
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
LIST OPERATIONS USING (APPEND, INSERT, EXTEND):

Append.py
names=["Hari","Ravi","Vasanth","Bhuvi"]
print('Current names list is:',names)
new_name=input("please enter a name:\n")
names.append(new_name)
print('Updated name list is:',names)
Insert.py
list1=[10,20,30,40,50]
print('Current Number List:',list1)
el=list1.insert(3,77)
print("The new list is:",list1)
n=int(input("Enter a number to add to list:\n"))
index=int(input('Enter the index to add the number:\n'))
list1.insert(index,n)
print('Updated Numbers List:',list1)
Extend.py
list1=[10,20,30]
list1.extend(["52.10","43.12"])
print(list1)
list1.extend((40,30))
print(list1)
list1.extend("Apple")
print(list1)
OUTPUT:

Append.py

Insert.py

Extend.py

Result:
The above program is executed successfully and the output is verified.
MULTIPLE OPERATIONS IN DICTIONARY:
MultipleOperations.py
print("Empty Dictionary is:")
print(dict)
dict1=({1:'Hello',2:'Hi',3:'Hey'})
print("\nCreate Dictionary by using the dict() method:")
print(dict1)
dict2=dict([('Arun',90014),('Ravi',90015)])
print("\nDictionary with each item as a pair:")
print(dict2)

#creating a dictionary
college= {
"name": "MCA",
"code": "541",
"id": "x3"
}
print(college)
#adding items to dictionary
college["location"] = "Science campus"
print(college)
#changing values of a key
college["location"] = "Karaikudi"
print(college)
# to remove items
college.pop("code")
print(college)
#know the length using len()
print("length of college is:",len(college))
#to copy the same dictionary use copy()
mycollege= college.copy()
print(mycollege)
OUTPUT:
MultipleOperations.py

Result:
The above program is executed successfully and the output is verified.
REVERSE THE STRING FOR GIVEN WORD WHICH IS NOT
A PALINDROME:
Palindrome.py
def palindrome(a):
mid=(len(a)-1)//2
start=0
last=len(a)-1
flag=0
while(start < mid):
if (a[start]==a[last]):
start+=1
last-=1
else:
flag=1
break
if flag==0:
print("The entered string is palindrome")
else:
reverse_String=""
count=len(str)
while count>0:
reverse_String+=str[count-1]
count=count-1
print("The reversed string using a while loop is ",reverse_String)
str=input("Enter a string")
palindrome(str)
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
FIND 5TH MAXIMUM OF GIVEN NUMBERS:
Sort and find 5th maximum.py
arr=[5,2,8,7,1]
temp = 0;
print("Elements of original array: ");
for i in range(0, len(arr)):
print(arr[i], end=" ");
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if(arr[i] > arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
print();
print("Elements of array sorted in ascending order: ");
for i in range(0, len(arr)):
print(arr[i], end=" ");
print("\n5th Maximum:")
print(arr[4])
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
MAXIMUM OF THREE NUMBERS:

Maximum of three numbers.py


def bigOf3(a,b,c):
if(a>b):
if(a>c):
print("a is greater than b and c")
else:
print("c is greater than a and b")
elif(b>c):
print("b is greater than a and c")
else:
print("c is greater than a and b")
txt= input("enter a,b,c values:")
a,b,c= txt.split()
bigOf3(int(a),int(b),int(c))
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
FINDING NUMBER OF EVEN AND ODD NUMBERS IN A
DICTIONARY:
Count odd and even numbers.py
def countEvenOdd(dict):
even=0
odd = 0
for i in dict.values():
if i % 2 == 0:
even = even + 1
else:
odd = odd + 1
print("Even Count:", even)
print("Odd Count:", odd)
dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
countEvenOdd(dict)
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
FINDING NUMBER OF DAYS OCCUR IN A YEAR:

Days occur.py
import datetime
import calendar
def day_occur_time(year):
days = [ "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" ]
L = [52 for i in range(7)]
pos = -1
day = datetime.datetime(year, month = 1, day = 1).strftime("%A")
for i in range(7):
if day == days[i]:
pos=i
if calendar.isleap(year):
L[pos] += 1
L[(pos+1)%7] += 1
else:
L[pos] += 1
for i in range(7):
print(days[i], L[i])
year = 2022
day_occur_time(year)
OUTPUT:

Year:2022

Year:2024

Result:
The above program is executed successfully and the output is verified.
DISPLAY EMPLOYEE DETAILS USING INHERITANCE:

inheritance.py
class Person( object ):
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
class Employee( Person ):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post
Person.__init__(self, name, idnumber)
def dis(self):
print(self.name)
print(self.idnumber)
print(self.salary)
print(self.post)
a = Employee('Rahul', 886012, 200000, "Intern")
a.display()
a.dis()
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
METHOD OVERLOADING FOR DIFFERENT TYPES OF
ARGUMENTS:
Methodoverloading.py
# Function to take multiple arguments
def add(datatype, *args):
# if datatype is int
# initialize answer as 0
if datatype =='int':
answer = 0
# if datatype is str
# initialize answer as ''
if datatype =='str':
answer =''
# Traverse through the arguments
for x in args:
# This will do addition if the
# arguments are int. Or concatenation
# if the arguments are str
answer = answer + x
print(answer)
# Integer
add('int', 5, 6)
# String
add('str', 'Hi ', 'MCA')
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
ADD TWO NUMBER USING CONSTRUCTOR:

constructor.py
class Addition:
first = 0
second = 0
answer = 0
# parameterized constructor
def __init__(self, f, s):
self.first = f
self.second = s
def display(self):
print("First number = " + str(self.first))
print("Second number = " + str(self.second))
print("Addition of two numbers = " + str(self.answer))
def calculate(self):
self.answer = self.first + self.second
# creating object of the class
# this will invoke parameterized constructor
obj = Addition(1000, 2000)
# perform Addition
obj.calculate()
# display result
obj.display()
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
ADDING POINTS USING OPERATOR OVERLOADING:

operatoroverloading.py
class point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def __add__(self,other):
x=self.x+other.x
y=self.y+other.y
return point(x,y)
def resu(self):
print(self.x,"\t",self.y)
p1=point(1,2)
p1.resu()
p2=point(2,3)
p2.resu()
p3=p1+p2
p3.resu()
OUTPUT:

Result:
The above program is executed successfully and the output is verified.
DISPLAY STUDENTS DETAILS USING CSV:

Studcsv.py
import csv
def write():
rows=[]
f=open("studcsv.csv","w",newline="")
csv_w=csv.writer(f)
L=["Num","Name"]
csv_w.writerow(L)
while True:
num=int(input("Enter number:"))
name1=input("Enter name:")
data=[num,name1]
rows.append(data)
ch=input("add more(y/n}")
if ch=='n':
break
csv_w.writerows(rows)
print("Data added")
f.close
print()

def display():
f=open("studcsv.csv","r",newline="")
print("All details")
read=csv.reader(f)
for i in read:
print(i)
f.close
print()
def search():
f=open("studcsv.csv","r",newline="")
read=csv.reader(f)
name1=input("Enter name to be searched:")
for i in read:
if i[1]==name1:
print(i)
break
else:
print("Name not found")
f.close
print()
write()
display()
search()
OUTPUT:

Excel File

Result:
The above program is executed successfully and the output is verified.

You might also like