Practical File CS 2024 25 (Word)
Practical File CS 2024 25 (Word)
Result:
Result:
The above output has been verified in the terminal.
SAMPLE OUTPUT:
Choose the operation to perform:
1. list creation
2. swap value
3. Exit
Enter your choice1
Enter a list[3,25,13,6,35,8,14,45]
Choose the operation to perform:
1. list creation
2. swap value
3.Exit
Enter your choice2
25 3 13 35 6 8 45 14
Choose the operation to perform:
1. list creation
2. swap value
3.Exit
Enter your choice3
EX.NO.3
DATE:
String Operations
AIM:
To write a menu driven program in python using user defined functions to take string as
input and
(i) To check whether the given string is palindrome or not.
(ii) To count number of occurrences of a given character.
SOURCE CODE:
def palindrome(s):
rev=s[::-1]
if rev==s:
print("The String is palindrome")
else:
print("The string is not a palindrome")
def Count(st,choice):
c=st.count(ch)
return c
#Main program
while True:
print("Menu")
print("1.palidrome")
print("2.To count number of occurrences of a given character")
print("3. Exit")
opt=int(input("Enter your choice:"))
if opt==1:
s=input("enter the string:")
palindrome(s)
elif opt==2:
st=input("Enter the string:")
ch=input("Enter a character")
cnt=Count(st,ch)
print("The character",ch,"is present in",st,"is",cnt,"times")
elif opt==3:
break
else:
print("Invalid Choice")
Result:
The above output has been verified in the terminal.
SAMPLE OUTPUT:
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice:1
enter the string:race
The string is not a palindrome
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice:2
Enter the string:race
Enter a charactera
The character a is present in race is 1 times
Menu
1. palidrome
2. To count number of occurrences of a given character
3. Exit
Enter your choice: 3
EX.NO.4
DATE:
To Show Grades using Dictionary
AIM:
To write a python program that uses a function showGrades(s) which takes the dictionary S
SOURCE CODE:
def showGrades(s):
for k,v in s.items():
if sum(v)/3>=90:
grade='A'
elif sum(v)/3>=60:
grade='B'
else:
grade='C'
print(k,sum(v),sum(v)/3,"-",grade)
s={"Amit":[92,86,64],"Nagma":[65,42,43],"David":[92,90,88]}
showGrades(s)
Sample Output:
Amit 242 80.66666666666667 - B
Nagma 150 50.0 - C
David 270 90.0 – A
Result:
DATE:
Dictionary –Create and Update
AIM:
To write a python script to print a dictionary where the keys are numbers between 1 and
15(both included) and the values are square of keys. Also write a function search () that takes in
the dictionary d as parameter and searches for a particular key, if found returns the corresponding
value, if not found, an error message will displayed.
Sample dictionary
{1:1, 2:4, 3:9, 4:16,…15:225}
SOURCE CODE:
def search(d):
x=int(input("Enter a key-value to search"))
if x in d.keys():
print("key",x,"is found, the value is",d.get(x))
else:
print('key not found')
d={}
for i in range(1,16):
d[i]=i*i
print(d)
while True:
print("1.Search for a value")
print("2.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
search(d)
elif ch==2:
break
else:
print("invalid choice")
Sample Output:
Run1
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15:
225}
1. Search for a value
2. Exit
Enter your choice: 1
Enter a key-value to search5
key 5 is found, the value is 25
1. Search for a value
2. Exit
Enter your choice: 2
>>>
Run2:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15:
225}
1. Search for a value
2.Exit
Enter your choice:1
Enter a key-value to search16
key not found
1. Search for a value
2.Exit
Enter your choice:2
>>>
Result:
The above output has been verified in the terminal.
EX.NO:6
DATE:
AIM:
To create a python program to read a Text file and Display every sentence in a
Separate line.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUTPUT:
Story.txt:
EX.NO:7
DATE:
CREATING A PYTHON PROGRAM TO READ A TEXT FILE AND DISPLAY THE NUMBER
OFVOWELS/CONSONANTS/LOWER CASE/ UPPERCASE CHARACTERS.
AIM:
To write a Python Program to read a text file "Story.txt" and displays
the number of Vowels/ Consonants/ Lowercase / Uppercase/characters
in the file.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUTPUT:
Story.txt:
EX.NO:8
DATE:
AIM:
To write a python program to read lines from a text file "Sample.txt" and
copy those lines into another file which are starting with an alphabet 'a' or
'A'.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUTPUT:
Python Executed Program output:
Sample.txt:
New.txt:
EX.NO:09
Date:
CREATING A PYTHON PROGRAM TO CREATE AND SEARCH RECORDS IN BINARY
FILE
AIM:
To write a Python Program to Create a binary file with roll number and
name. Search for a given roll number and display the name, if not found
display appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUPUT:
DATE:
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
EX.NO: 11
DATE:
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT:
EX.NO: 12
DATE:
AIM:
AIM: To write a python program to maintain book details like book code, book title and price
using stacks data structures
book=[]
def push():
bcode=input("Enter bcode ")
btitle=input("Enter btitle ")
price=input("Enter price ")
bk=(bcode,btitle,price)
book.append(bk)
def pop():
if(book==[]):
print("Underflow / Book Stack in empty")
else:
bcode,btitle,price=book.pop()
print("poped element is ")
print("bcode ",bcode," btitle ",btitle," price ",price)
def peek():
if book==[]:
print("Book stack is empty")
else:
top=len(book)-1
print("the top of the book stack is :",book[top])
def traverse():
if book==[]:
print("Book stack is empty")
else:
top=len(book)-1
for i in range(top,-1,-1):
print(book[i])
while True:
print("1. Push")
print("2. Pop")
print("3.peek")
print("4. Traversal")
print("5. Exit")
ch=int(input("Enter your choice "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
peek()
elif(ch==4):
traverse()
elif(ch==5):
print("End")
break
else:
print("Invalid choice")
SAMPLE OUTPUT:
PYTHON PROGRAM EXECUTED OUTPUT
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 1
Enter bcode 101
Enter btitle computer
Enter price 200
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 1
Enter bcode 102
Enter btitle physics
Enter price 100
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 2
poped element is
bcode 102 btitle physics price 100
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 3
the top of the book stack is : ('101', 'computer', '200')
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 4
('101', 'computer', '200')
1. Push
2. Pop
3.peek
4. Traversal
5. Exit
Enter your choice 5
End
Ex.no.14
Write a python program using function PUSH (Arr), where Arr is a list of numbers. From this list
push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message.
AIM:
To write a python program using function PUSH (Arr), where Arr is a list of numbers. From this list
push all numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at
least one element, otherwise display appropriate error message.
SOURCE CODE:
def push(Arr,item):
if item%5==0:
Arr.append(item)
def pop(Arr):
if Arr==[]:
print("No item found")
else:
print("The deleted element:")
return Arr.pop()
def show():
if Arr==[]:
print('No item found')
else:
top=len(Arr)-1
print("The stack elements are:")
for i in range(top,-1,-1):
print(Arr[i])
#Main program
Arr=[]
top=None
while True:
print('****** STACK IMPLEMENTATION USING LIST ******')
print('1: PUSH')
print('2: pop')
print('3: Show')
print('0: Exit')
ch=int(input('Enter choice:'))
if ch==1:
val=int(input('Enter no to push:'))
push(Arr,val)
elif ch==2:
res=pop(Arr)
print(res)
elif ch==3:
show()
elif ch==0:
print('Bye')
break
Output is:
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:1
Enter no to push:55
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:1
Enter no to push:33
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:2
The deleted element:
55
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit
Enter choice:3
No item found
****** STACK IMPLEMENTATION USING LIST ******
1: PUSH
2: pop
3: Show
0: Exit Enter choice:0 Bye
Ex.no.15
Write a python program to create a dictionary containing names and marks as key-value pairs of 5
students.
With separate user-defined functions to perform the following operations. Push the keys (name of
the student) of the dictionary into a stack, where the corresponding value (marks) is greater than
70.
Display the content of the stack.
SOURCE CODE:
def push(stk,d):
for i in d:
if d[i]>70:
stk.append(i)
def disp():
if stk==[]:
print("stack is empty")
else:
top=len(stk)-1
print(“The stack elements are;”)
for i in range(top,-1,-1):
print(stk[i])
stk=[]
d={}
print("performing stack operation using Dictionary")
while True:
print()
print("1.push")
print("2.disp")
print("0.exit")
opt=int(input("Enter your choice:"))
if opt==1:
d["Ramesh"]=int(input("Enter the mark of ramesh:"))
d["Umesh"]=int(input("Enter the mark of Umesh:"))
d["Vishal"]=int(input("Enter the mark of Vishal:"))
d["Khushi"]=int(input("Enter the mark of Khushi:"))
d["Ishika"]=int(input("Enter the mark of Ishika:"))
push(stk,d)
elif opt==2:
disp()
elif opt==0:
print('Bye)
break
OUTPUT:
SAMPLE OUTPUT
1. push
2. disp
0. exit
Enter your choice:1
Enter the mark of ramesh: 55
Enter the mark of Umesh: 66
Enter the mark of Vishal: 77
Enter the mark of Khushi: 44
Enter the mark of Ishika: 88
1. push
2.disp
0.exit
Enter your choice:2
Ishika
Vishal
1. push
2.disp
0.exit
Enter your choice:0
Bye
>>>
EX.NO:16
DATE:
CREATING A PYTHON PROGRAM TO INTEGRATE MYSQL WITH PYTHON
(display Avgmarks greater than 75)
AIM:
To create a python program to integrate mysql with python to display the avgmarks
greater than 75
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SQL Table
Output:
EX.NO: 17
DATE
CREATINGAPYTHONPROGRAM TOINTEGRATEMYSQLWITHPYTHON
(INSERTING RECORDS AND DISPLAYING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python by inserting records to
Emp table and display the records.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUTPUT:
Python Executed Program Output:
SQL OUTPUT:
EX.NO: 18
AIM:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Python Executed Program Output:
RUN -1:
Run – 2:
SQL OUTPUT:
OUTPUT -1:
OUTPUT -2:
EX.NO: 19
AIM:
Result:
Thus, the above Python program is executed successfully and the output is verified.
SAMPLE OUTPUT:
Python Executed Program Output:
RUN -1:
Run -2:
SQL OUTPUT:
EX.NO: 20
DATE:
CREATINGAPYTHONPROGRAM TOINTEGRATEMYSQLWITHPYTHON
(DELETING RECORDS)
AIM:
To write a Python Program to integrate MYSQL with Python to search an Employee
using EMPID and delete the record of an employee if present in already existing table
EMP, if not display the appropriate message.
SOURCE CODE:
Result:
Thus, the above Python program is executed successfully and the output is
verified.
SAMPLE OUTPUT:
Python Executed Program
Output :Run-1
Output RUN – 2:
SQL OUTPUT:
Ex.No.21 Sql exercise -1
TABLE STUDENT
TABLE MATERIAL
MCODE TYPE
M001 TERELENE
M002 COTTON
M003 POLYSTER
M004 SILK
ANSWERS.
A. SELECT DCODE, DESCRIPTION FROM DRESS ORDER BY DCODE;
B. SELECT * FROM DRESS WHERE LAUNCH DATE BETWEEN ’05-DEC-07’ AND ’20-
JUN-08’
C. SELECT AVG(PRICE) FROM DRESS WHERE DCODE =”M003”;
D. SELECT MCODE, MAX(PRICE),MIN(PRICE)FROM DRESS GROUP BY
MCODE;
Ex.No.23 SQL Exercise-3
a.) To display the details of those consumer whose address is delhi
b. To display the details of stationary whose price is in the range of 8 to 15.
c. To display the consumer name, address from table consumer and company and
price from table stationary with their corresponding matching S_ID.
d.) To increase the price of all the stationary by 2.
TABLE: STATIONARY
S_ID STATIONARY COMPANY PRICE
NAME
DP01 DET PEN ABC 10
PL02 PENCIL XYZ 6
ER05 ERASER XYZ 7
PL01 PENCIL CAN 5
GP02 GELPEN ABC 15
TABLE: CONSUMER
C_ID CONSUMER NAME ADDRESS S_ID
01 GOOD EARNER DELHI PL01
06 WHITE WELL MUMBAI GP02
12 TOPPER DELHI DP01
15 WRITE 4 DRAW DELHI PL02
16 MOTIVATION BANGALORE PL01
ANSWERS.
a.) SELECT * FROM CONSUMER WHERE ADDRESS=”DELHI”;
b.) SELECT CONSUMER NAME, ADDRESS ,COMPANY, PRICE FROM CONSUMER A,
STATIONARYB WHERE A.S_ID=B.S_ID;
c.) UPDATE STATIONARY SET PRICE=PRICE+2;
d.) INSERT INTO CONSUMER VALUES(17,”FAST WRITER”,”MUMBAI’,”GP01”);