INDEX S. PROGRAM PAGE Remarks NO NO 11. Write the program to find the given string ispalindrome or 3 not. 22. Write a program to find the area of the shapes using 4 module. 33. Read a text file line by line and display each word separated 6 by a #. 44. Read a text file and display the number of vowels 7 /consonants / uppercase /lowercase characters. 55. Remove all the lines that contain the character 'a' in a file 9 and write it to another file. 66. Create the binary file which should contains the student 10 details and to search the student based on rollno and display the details. 77. Create the binary file which should contains the student 12 details and to update the student based on rollno. 88. Write a random number generator that generates random 14 numbers between 1 and 6 (simulates a dice). 99. Create the csv file which should contains the employee 15 details and to search the employee based on emp no and display the details. 1010.Create the csv file which contains user_id and Password. 17 Write a user defined functions to enter, display and search the password for given user_id. 1111.Write a program to implement all stack operations using list 19 as stack. 1212.MYSQL TABLE: STUDENT 21-24 1313.MYSQL TABLES: DOCTOR,PATIENT 25-28 1414.Write a python program to create the table and insert the 29 records into the table by getting the data from the user and to store in the MySQL database. 1515.Write a python program to display all the records from the 31 table student. 16 32 Write a python program to update the student mark on table based on the examno given by the user. If record not 1 found display the appropriate message. Database : myschool Table name : student 1716.Write a python program to show tables from a selected database. 18 Write a python program to delete the particular record from the table based on the examno given by the user . If record not found display the appropriate message.DATABASE : MYSCHOOL TABLE NAME : STUDENT 17. 2 33 34 1. Write the program to find the given string is palindrome or not. AIM: To write a program to find the given string is palindrome or not. PROGRAM: print("Palindrome checking program") s=input("Enter the string :") s1=s[::-1] if s==s1: print("The given string ",s,"is a palindrome") else: print("The given string ",s,"is not a palindrome") OUTPUT: RESULT: The above program has been executed successfully and the output is verified. 3 2. Write a program to find the area of the shapes using module. AIM: To write a program to find the area of the shapes using module PROGRAM: MODULE PROGRAM: areaa def square(a): return a*a def rectangle(l,b): return l*b def triangle(b,h): return b*h def circle(r): return 3.14*r*r def cube(a): return 6*a**2 def cylinder(r,h): return 2*3.14*r*(r+h) def cone(r,h): return 3.14*r*r+h def sphere(r): return 4*3.14*r**2 MAIN PROGRAM: import areaa print("Area of the shapes calculating program") while True: print("1-square\t2-rectangle\t3-triangle\t4-circle\n5-cube\t6- cylinder\t7-cone\t8sphere\t9-quit") ch=int(input("Enter your choice:")) if ch==1: a=float(input("Enter the area value:")) ar=areaa.square(a) print("The area of the square is :",ar) elif ch==2: l=float(input("Enter the length value:")) b=float(input("Enter the breadth value:")) ar=areaa.rectangle(l,b) print("The area of the rectangle is :",ar) elif ch==3: b=float(input("Enter the base value:")) h=float(input("Enter the height value:")) ar=areaa.triangle(b,h) 4 print("The area of the rectangle is :",ar) elif ch==4: a=float(input("Enter the radius value:")) ar=areaa.circle(a) print("The area of the circle is :",ar) elif ch==5: a=float(input("Enter the area value:")) ar=areaa.cube(a) print("The area of the cube is :",ar) elif ch==6: r=float(input("Enter the radius value:")) h=float(input("Enter the height value:")) ar=areaa.cylinder(r,h) print("The area of the cylinder is :",ar) elif ch==7: r=float(input("Enter the radius value:")) h=float(input("Enter the height value:")) ar=areaa.cone(r,h) print("The area of the cone is :",ar) elif ch==8: a=float(input("Enter the radius value:")) ar=areaa.sphere(a) print("The area of the sphere is :",ar) elif ch==9: break else: print("Invalid choice") continue OUTPUT: RESULT: The above program has been executed successfully and the output is verified. 5 3. Read a text file line by line and display each word separated by a #. AIM: To read a text file line by line and display each word separated by a #. PROGRAM: filein = open("Mydoc.txt",'r') line =" " while line: line = filein.readline() for w in line: if w == ' ': print('#',end = '') else: print(w,end = '') filein.close() OUTPUT: RESULT: The above program has been executed successfully and the output is verified. 6 4. Read a text file and display the number of vowels/consonants/uppercase/lowercase characters. in the file. AIM: To read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file. PROGRAM: filein = open("Mydoc.txt",'r') line = filein.read() count_vow = 0 count_con = 0 count_low = 0 count_up = 0 count_digit = 0 count_other = 0 print(line) for ch in line: if ch.isupper(): count_up +=1 if ch.islower(): count_low += 1 if ch in 'aeiouAEIOU': count_vow += 1 if ch.isalpha(): count_con += 1 if ch.isdigit(): count_digit += 1 if not ch.isalnum() and ch !=' ' and ch !='\n': count_other += 1 print("Digits",count_digit) print("Vowels: ",count_vow) 7 print("Consonants: ",count_con-count_vow) print("Upper Case: ",count_up) print("Lower Case: ",count_low) print("other than letters and digit: ",count_other) filein.close() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 8 5. Remove all the lines that contain the character 'a' in a file and write it to another file. AIM: To remove all the lines that contain the character 'a' in a file and write it to another file. PROGRAM: a=open('sample1.txt','r') b=open('sample2.txt','w') c=a.readlines() print("File Content Sample1.txt") print(c) for i in c: if 'a' not in i: b.write(i) print() a.close() b.close() print("File Content Sample2.txt ,lines not contains the letter 'a'") b=open('sample2.txt','r') print(b.readlines()) b.close() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 9 6. Create the binary file which should contains the student details and to search the student based on rollno and display the details. AIM: To create the binary file which should contains the student details and to search the student based on rollno and display the details. PROGRAM: import pickle def write(): f=open('student','wb') while True: roll = int(input("Enter Roll Number :")) name= input("Enter Name :") mark=int(input("Enter Mark :")) record=[roll,name,mark] pickle.dump(record,f) ch=input("Do you want to continue Y/N") if ch in 'Nn': break f.close() def read(): f=open('student','rb') try : while True: rec=pickle.load(f) print(rec) except EOFError: f.close() def search(): f=open('student','rb+') r=int(input("Enter Roll Number :")) try : while True: pos=f.tell() rec=pickle.load(f) if rec[0]==r: print("Rollno:",rec[0]) print("Name:",rec[1]) print("Mark:",rec[2]) 10 except EOFError: f.close() write() read() search() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 11 7. Create the binary file which should contains the student details and to update the student based on rollno. AIM: To create the binary file which should contains the student details and to update the student mark based on rollno. PROGRAM: import pickle f=open('student','wb') def write(): while True: roll = int(input("Enter Roll Number :")) name= input("Enter Name :") mark=int(input("Enter Mark :")) record=[roll,name,mark] pickle.dump(record,f) ch=input("Do you want to continue Y/N") if ch in 'Nn': break f.close() def read(): f=open('student','rb') try : while True: rec=pickle.load(f) print(rec) except EOFError: f.close() def Update(): f=open('student','rb+') r=int(input("Enter Roll Number :")) try : while True: pos=f.tell() rec=pickle.load(f) if rec[0]==r: um=int(input("Enter the updated mark:")) rec[2]=um f.seek(pos) pickle.dump(rec,f) 12 print("Record Updated") except EOFError: f.close() write() print("Student Details before Updating:") read() Update() print("Student Details after Updating:") read() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 13 8. Write a random number generator that generates random numbers between 1 and 6 (simulates a dice). AIM: PROGRAM: import random print("Dice game ") print("Game starts...") ans='y' while ans=='y': print("Dice rolling ") s=random.randint(1,6) print("You got:",s) ans=input("Do you want to roll again the dice (y/n):") print("See you again.. Bye ") OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 14 9. Create the csv file which should contains the employee details and to search the employee based on emp no and display the details. AIM: To create a csv file which contains the employee details and to search the employee based on emp no and display the details. PROGRAM: import csv def write(): f=open("emp.csv","w",newline="") wo=csv.writer(f) wo.writerow(["EMPNO","EName","ESalary"]) while True: empno=int(input("Enter Employee No:")) name=input("Enter Employee Name:") sal=int(input("Enter Employee Salary:")) data=[empno,name,sal] wo.writerow(data) ch=input("Do you want to continue y/n:") if ch in 'Nn': break f.close() def display() : f=open("emp.csv","r") ro=csv.reader(f) for i in ro: print(i) def search() : Found=0 u=input("Enter the EmpNo to search:") f=open("emp.csv","r") ro=csv.reader(f) next(ro) for i in ro: if i[0]==u: print("EMP NO:",i[0]) print("EMP Name:",i[1]) print("EMP Salary:",i[2]) Found=1 f.close() if Found==0: 15 print("Record not Found") write() display() search() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 16 10. Create the csv file which contains user_id and Password. Write a user defined functions to enter, display and search the password for given user_id. AIM: To create a csv file with user id , password and to search the password for given user id. PROGRAM: import csv def write(): f=open("details.csv","w",newline="") wo=csv.writer(f) wo.writerow(["USER ID","PASSWORD"]) while True: u_id=input("Enter User ID:") pwd=input("Enter Password:") data=[u_id,pwd] wo.writerow(data) ch=input("Do you want to continue y/n:") if ch in 'Nn': break f.close() def display() : f=open("details.csv","r") ro=csv.reader(f) for i in ro: print(i) def search() : Found=0 u=input("Enter the user Id to search:") f=open("details.csv","r") ro=csv.reader(f) next(ro) for i in ro: if i[0]==u: print("User Id:",i[0]) print("Password:",i[1]) Found=1 f.close() if Found==0: print("Record not Found") write() display() 17 search() OUTPUT: RESULT: Thus, the above program has been executed successfully and the output is verified. 18 11. Write a program to implement all stack operations using list as stack. AIM: To write a program to implement all stack operations using list as stack. PROGRAM: def push(stk,elt): stk.append(elt) print("Element inserted..") print(stk) def pop(stk): if len(stk)==0: print('STACK UNDERFLOW') else: c=stk.pop() print('The popped element is:',c) def display(stk): a=stk[::-1] print(a) stack=[] while True: print("...Stack Operations....") print("1.PUSH") print("2.POP") print("3.DISPLAY") print("4.EXIT") ch=int(input("Enter your choice")) if ch==1: element=int(input("Enter the element to push")) push(stack,element) elif ch==2: pop(stack) elif ch==3: display(stack) elif ch==4: break 19 OUTPUT : RESULT : Thus, the above program has been executed successfully and the output is verified. 20 MYSQL QUERIES Table:Students EXAMNO 1201 1202 1203 1204 1205 1206 NAME PAVINDHAN ESHWAR BHARKAVI HARIPRIYA JAI PRADHAP KAVIPRIYA CLASS XII XII XII XII XII XII SEC A1 A1 A2 A2 NULL A2 MARK 489 465 498 400 398 NULL SET-1 1. Write the query to create the above table and set the constraints for the attributes. EXAMNOPRIMARY KEY,NAME- NOT NULL ,MARK CHECK-MARK SHOULD NOT EXCEED 500. 2. Write the query to insert the mentioned records into the table. 3. Write a query to display all the student records. 21 4. Write the query to display all the student records who is studying in A1 section. 5. Write the query to display the records whose mark is notassigned. 6. Write the query to display whose marks in the range 400 to 450(both values are inclusive). 7. Write the query to display the student’s name, marks who secured more than 400 and less than 487. 8. Write the query to display the details whose name is starts with ‘P’ or ‘B’. 22 9. Write the query to display the student name and section whose name contain ‘priya’. 10. Write the query to display all the details sort by name in descending order. 11. Write the query to add 10 marks with the existing marks as ‘Updated marks’ and display the details. 12. Write the query to add a new column named as CITY with the data type VARCHAR(30) and apply the default constraint ‘NOT MENTIONED’ in the students table. 23 13. Write the query to redefine the NAME field size into VARCHAR(40) in the Students table. 14. Write the query to update the section is A3 whose section is null. 15. Write the query to update the city of all records with the following cities [chennai,bengaluru] 24 SET -B TABLE NAME : DOCTOR DOCID D01 D02 D03 D04 D05 D06 D07 D08 DNAME ASHWATHAN RAMYA SRIRAM SANJAY SRINITHI SANTHOSH KARTHICK YASHIK DEPT ONCOLOGY GYNAECOLOGY DENTAL CARDIOLOGY PEDIATRICS PEDIATRICS CARDIOLOGY GYNAECOLOGY CITY CHENNAI COIMBATORE BHOPAL HYDERABAD DELHI BENGALURU JAIPUR AHMEDABAD SALARY 150000 140000 180000 160000 120000 140000 180000 195000 TABLE NAME : PATIENT PATID P01 P02 P03 P04 P05 P06 P07 P08 PNAME SATHISH SUPRIYA ARUNA RAMESH KUMAR PRIYA ROOPA CHARLES APMDATE 2022/08/19 2022/09/21 2022/10/20 2022/08/25 2022/09/23 2022/09/14 2022/08/13 2022/10/12 GENDER MALE FEMALE FEMALE MALE MALE FEMALE FEMALE MALE AGE 45 23 43 84 12 10 66 24 DOCID D01 D02 D08 D04 D02 D06 D03 D07 1 Write the query to create the Doctor table and keep docid to be the primary key. create table doctor( docid varchar(4) primary key, dname varchar(40), dept varchar(20), city varchar(20), salary integer); 2 Write the query to create the patient table and keep DOCID to be the foreign key. 25 3 Write the query to display the count of male and female patients. 4 Write the query to display the count of male and female patients in the age between 20 and 40. 5 Write the query to display the number of doctors in each dept. 6 Write the query to display the sum of the salary of the doctor’s department wise. 26 7 Write the query to display Docid, doctor name from doctor table and their corresponding patient name. 8 Write the query to display patient name, doctor name, patient age and their appointment date from the tables. 9 Write the query to display Docid, doctor name from doctor table and their corresponding patient name where doctor name starting with s. 10 Write a query to display the sum of salary of doctors of each department whose number of departments is greater than or equal to 2. 27 11 Write a query to display the details of doctor and patient in oncology department using natural join. 12 Write a query to display the details of doctor and patient in oncology department using inner join. 13 Write a query to display the Cartesian product of table doctor and table patient. 28 PYTHON - MYSQL CONNECTIVITY PROGRAMS 1. Write a python program to create the table and insert the records into the table by getting the data from the user and to store in the MySQL database. Database :myschool Table name : student Attributes : examno,name,class,section,mark AIM : To write a python program to create the table and insert the records into the table by getting the data from the user and to store in the MySQL database. PROGRAM: import mysql.connector as my a=my.connect(host='localhost',user='root',password='anitha123', database='MYSCHOOL') if a.is_connected(): print('Database connected') else: print('Database not connected') b=a.cursor() q0='drop table if exists student' b.execute(q0) q="create table student(EXAMNO INT,NAME VARCHAR(20),CLASS INT,SECTION CHAR(2),MARK INT)" b.execute(q) ans='y' while ans.lower()=='y': ex=int(input("ENTER EXAMNO:")) na=input("ENTER NAME:") cl=int(input("ENTER CLASS:")) s=input("ENTER SECTION:") m=int(input("ENTER MARK:")) q1="insert into student values({},'{}',{},'{}',{})".format(ex,na,cl,s,m) b.execute(q1) print("Record successfully stored in the student table") ans=input("Do you want to add another record(y/n):") a.commit() a.close() 29 OUTPUT: RESULT: Thus the above program has been executed successfully and theoutput is verified. : 30 2. Write a python program to display all the records from the table student. AIM: PROGRAM: import mysql.connector as my a=my.connect(host='localhost',user='root',password='anitha123',database='MYSCH OOL') mycursor=a.cursor() print("\n Display all the records ") sql="SELECT * FROM STUDENT" mycursor.execute(sql) result=mycursor.fetchall() for x in result: print(x) a.close() OUTPUT: RESULT: Thus the above program has been executed successfully and theoutput is verified. 31 3. Write a python program to update the student mark on table based on the examno given by the user. If record not found display the appropriate message. Database : myschool Table name : student AIM : To write a python program to update the student mark on table based on the examno given by the user and if the record not founddisplay the appropriate message. PROGRAM : import mysql.connector as my a=my.connect(host='localhost',user='root',password='anitha123',database='MYSCH OOL') ans='y' while ans.lower()=='y': b=a.cursor() r=int(input("ENTER THE EXAMNO YOU WANT TO UPDATE:")) q1="select * from student where examno={}".format(r) b.execute(q1) e=b.fetchall() d=b.rowcount if d!=0: nm=int(input('ENTER THE NEW MARK:')) q2='UPDATE STUDENT SET MARK={} WHERE EXAMNO = {} ' . format (nm,r) b.execute(q2) a.commit() print('RECORD UPDATED SUCCESSFULLY WITH NEW MARK') else: print('RECORD NOT FOUND') ans=input('DO YOU WANT TO UPDATE ANOTHER RECORD (Y/N):') a.close() OUTPUT : RESULT : Thus the above program has been executed successfully and theoutput is verified. 32 4. Write a python program to show tables from a selected database. AIM: To write a python program to show tables from a selected database. PROGRAM: import mysql.connector as my a=my.connect(host='localhost',user='root',password='anitha123',database='PACE') mycursor=a.cursor() print("\n Show Tables from databases ") sql="show tables" mycursor.execute(sql) for x in mycursor: print(x) OUTPUT: RESULT: Thus the above program has been executed successfully and theoutput is verified. 33 5. Write a python program to delete the particular record from the table based on the examno given by the user . If record not found display the appropriate message. DATABASE : MYSCHOOL TABLE NAME : STUDENT AIM: To Write a python program to delete the particular record from the table based on the examno given by the user . If record not found display the appropriate message. DATABASE : MYSCHOOL TABLE NAME : STUDENT PROGRAM: import mysql.connector as my a=my.connect(host='localhost',user='root',password='anitha123',database='MYSCH OOL') ans='y' while ans.lower()=='y': b=a.cursor() r=int(input("ENTER THE EXAMNO YOU WANT TO DELETE:")) q1="select * from student where examno={}".format(r) b.execute(q1) e=b.fetchall() print(e) d=b.rowcount if d!=0: q2='DELETE FROM STUDENT WHERE EXAMNO={}'.format(r) b.execute(q2) a.commit() print('RECORD DELETED SUCCESSFULLY ') else: print('RECORD NOT FOUND') ans=input('DO YOU WANT TO DELETE ANOTHER RECORD (Y/N):') a.close() OUTPUT: 34 RESULT: Thus, the above program has been executed successfully and theoutput is verified. 35