Shameer Python
Shameer Python
LABORATORY RECORD
Submitted in partial fulfilment of the requirements for the award of
Masters of Computer Applications
At
SUBMITTED BY
SHAMEER SABEER(PJR24MCA-2017)
CERTIFICATE
Certified that this is a Bonafide record of practical work done in Programming Lab (20MCA131) by
SHAMEER SABEER Reg No.PJR24MCA-2017 of College of Engineering, Poonjar, during the
academic year 2024- 2026.
PROGRAM-1
ALGORITHM :
1. Read numbers n1,n2,n3
2. If n1>n2 and n1>n3, Largest is n1 Else if n2>n1 and n2>n3, Largest is n2
Else, Largest is n3
End of if
3. Print largest
SOURCE CODE :
n1=int(input('Enter first Number:'))
n2=int(input('Enter second Number:'))
n3=int(input('Enter third Number:'))
if(n1>=n2) and (n1>=n2):
largest=n1
elif(n2>=n1) and (n2>=n3):
largest=n2
else:
largest=n3
print('largest number is:',largest)
RESULT:
Program is executed and output is obtained
OUTPUT:
1
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-2
AIM: Accept a file name from user and print the extension of that file.
ALGORITHM:
1. Read a file name with extension
2. Using split(‘.’)function extract the extension from the file
3. Print extension
SOURCE CODE:
fname=input('enter the file name:')
exten=fname.split('.')
print('extension is:',exten[-1])
RESULT:
Program is executed an output is obtained
OUTPUT:
2
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-3
AIM: Create a string from given string where first and last characters exchanged
ALGORITHM:
1. Read string str.
2. Print rearranged string sliced using index.
str= l + str[1:-1] + f
3. Print str
SOURCE CODE:
str=input('Enter a string:')
f=str[0] l=str[-1]
str=l+str[1:-1]+f
print('New string is:',str)
RESULT:
Program is executed and output is obtained
OUTPUT:
3
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-4
AIM: Create a list of colors from comma-separated color names entered by user. Display
first and last colors
ALGORITHM:
1. read a list of colors in list.
2. Print first color, list[0]
3. Print last color list[-1]
SOURCE CODE:
list=[] n=int(input('enter no element:”))
for i in range(0,n):
element=input('enter the color:')
list.append(element)
print('full list:',list) print('first
element:',list[0]) print('last
element:',list[1])
RESULT:
Program is executed and output is obtained
OUTPUT:
4
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-5
ALGORITHM:
1. Read a list of numbers from user
2. For each in list Check list[i]>0
3. If yes display positive number
4. End of for
SOURCE CODE:
list=[]
n=int(input('Enter the no of elements:'))
print('Elements are:')
for i in range (0,n):
element=int(input())
list.append(element)
print('Positive Numbers are:')
for i in range(0,n):
if(list[i]>=0):
print(list[i])
RESULT:
Program is executed and output is obtained
OUTPUT:
5
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-6
ALGORITHM:
1.Read numbers and append to the list
2.Square=list[i]**2 for each number in the list
3. Display the squared number
SOURCE CODE:
list=[]
n=int(input('Enter the no of elements:'))
print('Elements are:')
for i in range (0,n):
element=int(input())
list.append(element)
print('sqaure of elements:')
for i in range(0,n):
print(list[i]**2)
RESULT:
The program is executed and output is obtained
OUTPUT:
6
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-7
ALGORITHM:
1. Read a string str
2. Enter the element to be searched
3. Initialize count=0
4. Check each elements of string with search if it presen increment count
5. Display count
SOURCE CODE:
str=input('Enter the String:')
search=input('Enter the element to be searched:') x=str.split(" ")
count=0
for i in str:
if(i==search):
count=count+1
print(count)
RESULT:
Program is executed and output is obtained
OUTPUT:
7
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-8
AIM: Prompt the user for a list of integers. For all values greater than 100, store ‘over’ instead.
ALGORITHM:
1. Read list of numbers in values.
2. For each i in values do
3. If a<=100 then Append i to x Else:
Append “over” to x
4. Print x
SOURCE CODE:
list=[]
n=int(input('Enter the no of elements:'))
print('Numbers are : \t')
for i in range(0,n):
number=int(input())
list.append(number) print('\n',
list)
for i in range(0, n):
if(list[i]>100):
list[i]='over'
print(list)
RESULT:
Program is executed and output is obtained
OUTPUT:
8
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-9
AIM: Store a list of first names. Count the occurrences of ‘a’ within the list
ALGORITHM:
1. .Read a list of names in a list
2. Initialize list = []
3. Initialize count=0
4. For name in list
Count=count+name.count(‘a’)
5. print count
SOURCE CODE:
list=[]
n=int(input('Enter the Limit:'))
for i in range(0,n):
fname=input('Enter the first name:')
list.append(fname)
print('Full List',list) count=0
for name in list:
count=count+name.count('a')
print('occurance of a:',count)
RESULT:
Program is executed and output is obtained
OUTPUT:
9
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-10
AIM: Get a string from an input string where all occurrences of first character replaced with ‘$’
ALGORITHM:
1. read a string
2. assign $ to first element
‘$’+str[1:]
3. print string
SOURCE CODE:
str=input('Enter a string:')
str='$'+str[1:] print('String is :',str)
RESULT:
Program is executed and output is obtained
OUTPUT:
10
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM – 11
ALGORITHM:
1. Read a list of integers in list.
5. Print oddlist
SOURCE CODE:
list=[]
n=int(input("Enter the no of elements:"))
print("numbers are:")
for i in range(0,n):
number=int(input())
list.append(number)
oddlist=[]
for i in range(o,n):
if((list[i]%2)!=0):
oddlist.append(list[i])
print(oddlist)
RESULT:
Program is executed and output is obtained
OUTPUT:
11
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-12
ALGORITHM:
1. Read two list of numbers list1 and list2.
2. //check if the lengths of the two lists are equal.
If length(list1)=length(list2) then
SOURCE CODE:
list_1=input("Enter the first list of integers(seperated by comma):")
list1=[int(x) for x in list_1.split(',')]
list_2=input("Enter the second list of integers(seperated by comma):")
list2=[int(x) for x in list_2.split(',')]
if len(list1)==len(list2):
print("The lists are same length",len(list1))
else:
12
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
RESULT:
Program is executed and output is obtained
OUTPUT:
13
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-13
AIM: Write a python program to read two lists color-list1 and color-list2. Print out all colors from
color-list1 not contained in color-list2.
ALGORITHM:
1. Read two list of colors in color_list1 and color_list2
2. Initialize list, list1=[]
3. //Append values from color_list1 to list1
3. Initialize list,result=[]
4. For each item in list1 do If item not in list2 then
Append item to result
5. Print result
SOURCE CODE:
list1=[]
list2=[]
n=int(input("enter the no.of colours in list 1:"))
print("colours are:")
for i in range(0,n):
color=input()
list1.append(color)
n=int(input("enter the no.of colours in list 2:"))
print("colours are:")
for i in range(0,n):
color=input()
list2.append(color)
14
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
set1=set(list1)
set2=set(list2)
print("Orginal set are:\n",list1,"\n",list2)
print("Colour difference:",set1-set2)
RESULT:
Program is executed and output is obtained
OUTPUT:
15
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-14
ALGORITHM:
1. Read two numbers
2. Initialize gcd=1 and n=minimum(num1,num2)+1
3. For I in range 1,n
If num1%2==0 and num2%2==0
Then gcd=i
4. Print gcd
SOURCE CODE:
num1=int(input('enter number1:'))
num2=int(input('enter number2:'))
gcd=1
n=min(num1,num2)+1
for i in range(1,n):
if((num1%i==0) and (num2%i==0))
gcd=i
print("GCD",num1,num2,'is',gcd)
RESULT:
Program is executed and output is obtained
OUTPUT:
16
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-15
ALGORITHM:
1. Read a Number n
2. If n%2==0 then print element
Iterate upto i<=n
SOURCE CODE:
n=int(input("enter a number:"))
print("factors of",n,"are:")
for i in range(1,n):
if(n%i==0):
print(i)
RESULT:
Program is executed and output is obtained
OUTPUT:
17
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM – 16
sum= sum(list)
6. Print sum
SOURCE CODE:
list=[]
n=int(input("enter the limit:"))
for i in range(0,n):
num=int(input("enter the number:"))
list.append(num)
sum=0
for i in range(0,n):
sum=sum+list[i]
print("sum is",sum)
RESULT:
Program is executed and output is obtained
OUTPUT:
18
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-17
ALGORITHM:
1. Read the limit of the series, n.
Print a
Store thevalue of ‘a’ in a temporary variable temp = a
Update ‘a’ with the value of ‘b’ a =b
SOURCE CODE:
n=int(input("enter the limit:"))
a=0
b=1
print("fibonacci series: \n",a,"\n",b)
for i in range(1,n-1):
c=a+b
a=b
b=c
print(c)
RESULT:
Program is executed and output is obtained
OUTPUT:
19
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-18
AIM: Add ‘ing’ at the end of a given string. If it already ends with ‘ing’, then add ‘ly’
ALGORITHM:
1. Read a string str
2. Check the last 3 letter are ing if yes
Str=str+”ly”
Else str=str+”ing”
3. Print str
SOURCE CODE:
str=input("enter a string:")
if(str[-3:]=='ing'):
str=str+'ly'
else:
str=str+'ing' print(str)
RESULT
Program is executed and output is obtained
OUTPUT
20
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM – 19
ALGORITHM:
1. Read a list of words in list.
2. Initialize longest_length:=0
5. Print longest_word
6. Print longest_length
SOURCE CODE:
list=[]
n=int(input("eenter the limit:"))
for i in range(0,n):
el=input("enter the value:")
list.append(el)
print(list)
long=len(list[0])
str=list[0]
for i in range(1,n):
if(len(list[i])>long):
str=list[i]
long=len(str)
print("longest word:",str)
print("length:",long)
RESULT:
Program is executed and output is obtained.
21
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
OUTPUT:
22
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-20
ALGORITHM:
1. Read the number of key-value pairs in the dictionary, n.
2. Initialize a dictionary, dict={}
3. For i=0 to n-1 do
Get the key from the user
Get the value from the user
Assign dict[key]= value
item[0]
6. Print dict_asc
7. //Sort the dictionary by keys in descending order
SOURCE CODE:
n=int(input("Enter the number of key value pairs in the dictionary:"))
dict={}
for i in range(n):
key=input("Enter the key:")
value=input("Enter the value:")
dict[key]=value
print("The dictonary is:",dict) dict_asc=sorted(dict.items(),key=lambda
item:item[0])
print("sorted dictinary in ascending order:",dict_asc)
dict_desc=sorted(dict.items(),key=lambda item:item[0],reverse=True)
print("sorted dictinary in descending order:",dict_desc)
RESULT:
Program is executed and output is obtained
23
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
OUTPUT:
24
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
PROGRAM-21
ALGORITHM:
1. Read the number of key-value pairs in first dictionary, n1.
2. Initialize a dictionary, dict1={}
3. For i=0 to n-1 do
Get the key from the user
Get the value from the user
Assign dict1[key]= value
6. Print dict1
7. Print dict2
8. //Create a copy of the first dictionary
merge_dict= dict1.copy()
9. //Update the copy with the key value pairs from the second dictionary
merge_dict.update(dict2)
SOURCE CODE:
n1=int(input("ENter the number of key value pairs in first dictionary:"))
dict1={}
for i in range(n1):
key=input("Enter key:")
value=input("Enter value:")
dict1[key]=value
n2=int(input("Enter the number of key value paies in second dictionary:"))
dict2={}
for i in range(n2):
25
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTERAPPLICATIONS
key=input("Enter key:")
value=input("Enter value:")
dict2[key]=value print(dict1) print(dict2)
merge_dict=dict1.copy()
merge_dict.update(dict2) print("Merged
dictionary:",merge_dict)
RESULT:
Program is executed and output is obtained
OUTPUT:
26
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM-22
ALGORITHM:
1. Read a number
2. Call Factorial function factorial(n)
3. Define factorial
If n==0 return 1
If n==1 return 1
Initialize fact=1
For i=1 to n+1 do fact=fact*i
4. Print fact
SOURCE CODE:
def factorial(n):
if(n==0):
return 1
if(n==1):
return 1
fact=1
for i in range(1,n+1):
fact=fact*i
return fact
num=(int(input("enter a number :")))
fact=factorial(num)
print("factorial is ",fact)
RESULT:
Program is executed and output is obtained
OUTPUT:
27
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM – 23
ALGORITHM:
1. Read a number to num
2. Call fuction factors(n)
Def factors(n)
For I =1 to n+1 check n%i==0 if true print i
SOURCE CODE:
def factors(n):
for i in range(1,n+1):
if(n%i==0):
print(i)
num=int(input("enter a number:"))
print("factors are:")
factors(num)
RESULT:
Program is executed and output is obtained
OUTPUT:
28
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM – 24
AIM: Write lambda functions to find the area of square, rectangle and triangle
ALGORITHM:
1. Read length of square, a
SOURCE CODE:
area1=lambda x:x*x
area2=lambda x,y:x*y
area3=lambda x,y:0.5*x*y
RESULT:
Program is executed an output is obtained
29
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
OUTPUT:
30
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM – 25
AIM: Create class person with attribute name and age display the name and age of a particular
person
ALGORITHM:
1. Define a class person
2. Define a constructor that takes name and age as parameters
3. Define display function to print name and age of a person
4. Create an object of class person
5. call display function using object.display()
SOURCE CODE:
class person:
def init (self,name,age):
self.name=name
self.age=age def
display(self):
print("name is:",self.name)
print("age is:",self.age)
p1=person("sanju",23)
p1.display()
RESULT:
Program is executed and output is obtained
OUTPUT:
31
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM-26
AIM: Create a class Publisher with attributes publisher id and publisher name. Derive class
Book from Publisher with attributes title and author. Derive class Python from Book with
attributes price and no_of_pages. Write a program that displays information about a Python
book. Use base class constructor invocation and method overridin
ALGORITHM:
1. Define a class publlisher
2. Define a constructor that takes publisher_id and publisher_name as parameters.
3. Define a class ,book that inherits from publisher.
4. Define a constructor that takes publisher_id,publisher_name,title and author as
Parameters.
5. Define a class python, that inherits from book.
6. Define a constructor that takes publisher_id,publisher_name,title and author, Price
and
no_of_pages a parameters.
7. Define a function display_info with parameter self.
8. Read publisher id,publisher name,book title,author name,book price and number of
pages.
9. Create a object of python class.
10. Call the function display_info and display book information
SOURCE CODE:
class Book(Publisher):
def
init (self,name,title,author):
super(). init (name)
self.title=title
self.author=author
class Python(Book):
def
init (self,name,title,author,price,no_of_pages):
32
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
self.no_of_pages=no_of_pages
def display(self):
print("publishername:",self.name,"\n
","book title:",self.title,"\n","book
author:",self.author,"\n","book
price",self.price,"\n","no_of_pages:"
,self.no_of_pages)
a=Python("Thomas","python
fundamentals","mark",100,200)
a.display()
RESULT:
Program is executed and output is obtained
OUTPUT:
33
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM-27
AIM: Define a class to represent a bank account. Include the following details like name of
the depositor, account number, type of account, balance amount in the account. Write
methods to assign initial values, to deposit an amount , withdraw an amount after checking
the balance, to display details such as name, account number, account type and balance
ALGORITHM:
1. Define a class bank.
34
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
Create(self)
1. Read account no as input.
2. Read account holder name.
3. While True do
Read the FD amount
If FD<500 then
deposit(self)
1. While True do
Read the deposit amount
If deposit amount is not a multiple of 100 then
Print “enter nultiples of 100” Else:
Print “Amount deposited”
withdraw(self)
1. While True do
Read withdrawal amount
If withdrawal amount > FD amount then
Print “entered amount exceeds balance” Else If withdrwal amount is not a multiple
of
100 then
35
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
SOURCE CODE:
ClassBank:
def create(self):
self.no = int(input("Enter account number: "))
self.name=input("Enter account holder name: ")
while True:
self.fd=int(input("Enter FD amount: "))
if self.fd<500:
print("Please deposit more than 500")
else:
self.fd=self.fd
print("Account created!")
break
def deposit(self):
while True:
namount = int(input("Enter amount to be deposited: ")
if namount%100==0:
self.fd = namount
print("Amount deposited!")
break
else:
print("Enter multiples of 100!")
def withdraw(self):
while True:
w_amount=int(input("Enter money to be withdrawn:")
if w_amount>self.fd:
print("Entered amount exceeds balance")
elif w_amount%100!=0:
print("Can only withdraw amount which is multiple of 100!")
elif self.fd-w_amount<500:
print("can only withdraw money with minimum balance 500")
else:
36
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
self.fd=self.fd
w_amountprint("Amount withdrawn!")
break
bank = []
while True:
print("1. A/C creation", "2. Money deposit", "3. Withdraw money", "4.Checkbalance")
choice =int(input("Enter choice: "))
if choice == 1:
account = Bank()
account.create()
bank.append(account)
elif choice == 2:
flag = 0
sacno = int(input("Enter account number to search:"))
for account in bank:
if account.no == sacno:
flag=1
break
if flag==1
print("Welcomeaccoun
t.name)
account.deposit()
else:
print("No such account!") elif
choice == 3:
flag = 0
sacno = int(input("Enter account number to search: ")) for
account in bank:
if account.no == sacno:
flag = 1
break
if flag == 1:
print("Welcome", account.name)
account.withdraw()
else:
print("No such account!")
37
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
elif choice == 4:
flag = 0
sacno = int(input("Enter account number to search: "))
for account in bank:
if account.no == sacno:
print("Welcome", account.name)
print("Balance: ", account.fd)
elif choice == 5:
break
RESULT:
Program is executed and output is obtained
OUTPUT:
38
20MCA131 PROGRAMMING LAB DEPT.OF.COMPUTER APPLICATIONS
PROGRAM-28
AIM: Write a python program to read each row from a given CSV file and print list of string
ALGORITHM:
1. import csv file
2. open csv file in write mode
3. write contents into csv file using writerow() function
4. close csv file
5. open csv file in read mode
6. print each row
SOURCE CODE:
import csv csvfile=open('example.csv','w',newline='')
writer=csv.writer(csvfile)
writer.writerow(['name','age','occupation'])
writer.writerow(['sanju','30','cricketer'])
writer.writerow(['ronaldo','37','footballer']) csvfile.close()
csvfile=open('example.csv','r)
reader=csv.reader(csvfile) for row
in reader:
print(row)
csvfile.close()
RESULT:
Program is executed and output is obtained
OUTPUT:
39