Jitesh File
Jitesh File
Program 1: Write a program to check whether a given program is prime or not prime.
Code:
def prime_no():
num=int(input("enter a number:"))
f=0
for i in range(2,num):
if num%i==0:
f=1
break
if f==1:
print("the number is not prime")
else:
print("the number is prime")
prime_no()
Program 6: Write a program to read a file and display the content line by line , each word separated by “#”.
Code:
def sep():
fr=open("C:\\Users\\jites\\OneDrive\\Desktop\\data.txt","r")
data=fr.readlines()
for i in data:
w=i.strip().split()
for j in w:
print(j,end="#")
fr.close()
sep()
Output: Features#of#Python....#Easy#to#read#and#understand.#Interpreted#language.#
2
Program 7: Write a program to read a text file and count the vowels.
Code:
def count_vowel():
v=0
fr=open("C:\\Users\\jites\\OneDrive\\Desktop\\data.txt","r")
data=fr.read()
vowel=["a","i","o","e","u","A","E","I","O","U"]
for i in data:
if i in vowel:
v=v+1
print("the no. of vowels are",v)
count_vowel()
Program 8: Write a program to check if the given email address is valid or not.
Code:
def V():
st=input("enter the email address=")
a="email.com,gmail.com,yahoo.com"
n,c=st.split("@")
if c in a:
print("valid")
else:
print("not valid")
V()
Program 9: Write a program to read content from a text file and print only the lines that starts with “A”.
Code:
def startwithA():
fr=open("C:\\Users\\jites\\OneDrive\\Desktop\\data.txt","r")
data=fr.readlines()
for i in data:
if i[0]=="A":
print(i)
else:
pass
fr.close()
startwithA()
3
Program 11: Write a program read a text file and search a admission no.
Code:
def write_data():
fw=open("C:\\Users\\jites\\OneDrive\\Desktop\\data.txt","w")
st=""
n=int(input("enter a limit:"))
for i in range(n):
adm_no=input("enter a adm_no:")
name=input("enter a name:")
cls=input("enter a class:")
st=adm_no+","+name+","+cls+"\n"
fw.write(st)
fw.close()
#main
write_data()
def read_data():
fr=open("C:\\Users\\jites\\OneDrive\\Desktop\\data.txt","r")
data=fr.readlines()
search=int(input("enter a searched element:"))
for i in data:
a,n,c=i.split(",")
if int(a)==search:
print(a)
print(n)
print(c)
else:
print("record not found")
fr.close()
#main
read_data()
4
Program 12: Write a program to create a CSV file having details of students.
Code:
import csv
def csv_creation():
fw=open("C:\\Users\\jites\\OneDrive\\dat.csv","w")
c=csv.writer(fw)
c.writerow(["adm_no","name","cls"])
num=int(input("enter a limit:"))
for i in range(num):
adm_no=int(input("enter adm_no:"))
name=input("enter name of the student:")
cls=int(input("enter class:"))
l=[adm_no,name,cls]
c.writerow(l)
print("record added")
fw.close()
#main
csv_creation()
record added
Program 13: Write a program to copy the only lines having “school” and write them in another file.
Code:
def search():
fr=open("C:\\Users\\jites\\OneDrive\\Desktop\\prachi.txt","r")
fw=open("C:\\Users\\jites\\OneDrive\\Desktop\\ram.txt","w")
data=fr.readlines()
for i in data:
if "school" in i.lower():
fw.write(i)
print("record is added")
fr.close()
fw.close()
#Main
search()
5
Program 14: Write a program to create a binary file to store adm_no , name and cls and search adm_no and
display the detail.
Code:
import pickle
def add_student(filename):
fw= open("C:\\Users\\jites\\OneDrive\\filename", "ab")
n=int(input(“enter the limit of record=>”))
for i in range(n):
student = {
'admission_no': input("Enter Admission No.: "),
'name': input("Enter Name: "),
'class': input("Enter Class: "),
'age': int(input("Enter Age: "))
}
pickle.dump(student, fw)
print("Student details added successfully.\n")
#main
filename = "Diya.dat"
ch="y"
while ch=="y":
print("\nMenu:")
print("1. Add Student")
print("2. Search Student")
print("3. Exit")
6
Output: Menu:
1. Add Student
2. Search Student
3. Exit
Menu:
1. Add Student
2. Search Student
3. Exit
Menu:
1. Add Student
2. Search Student
3. Exit
Enter your choice: 3
7
Program 15: Write a program to implement a stack using list.
Code:
def stack():
s=[]
c="y"
while c=="y":
print("1.Push")
print("2.Pop")
print("3.display")
ch=int(input("enter your choice:"))
if ch==1:
n=int(input("enter an limit:"))
for i in range(n):
e=int(input("enter an element:"))
s.append(e)
print(s)
elif ch==2:
if s==[]:
print("stack empty")
else:
print("deleted element is ",s.pop())
elif ch==3:
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print("wrong output")
c=input("do you want to coutinue or not?")
stack()
Output: 1.Push
2.Pop
3.display
enter your choice:1
enter an limit:4
enter an element:11
enter an element:12
enter an element:13
enter an element:14
[11, 12, 13, 14]
do you want to coutinue or not?y
1.Push
2.Pop
3.display
enter your choice:2
deleted element is 14
do you want to coutinue or not?y
1.Push
2.Pop
3.display
enter your choice:3
13
12
11
8
SQL QUErY
Program 16 Write SQL query based on the following table:
(1): Write SQL query to display product name and price for all products whose price is in the range 50
to 250.
Query:
(2): Write SQL query to display details of products whose manufacturer is either XYZ or ABC.
Query:
(3): Write SQL query to display product name, manufacturer and price for all products that are not
given any discount.
Query:
(4): Write SQL query to display product name and price for all product name ends with “h”.
Query:
9
(5): Write SQL query to display client name, city, P_ID and product name for all clients whose cities is
Delhi.
Query:
Query:
(2): Write sql query to increase the size of s name to hold 30 characters.
Query:
10
(3): Write SQL query to remove the column hobby.
Query:
(4): Write a SQL query to insert a row in the table with any values of your choice that can be
accommodated there.
Query:
(1): List the name of those students who have obtained rank 1 sorted by NAME.
Query:
11
(2): Display a list of all those names whose AVERAGE is greater than 65.
Query:
(3): Display the names of those students who have opted COMPUTER as a SUBJECT with an
AVERAGE of more than 60.
Query:
(4): List the names of all the students in the alphabetical order.
Query:
Query:
12
(6): SELECT DISTINCT RANK FROM GRADUATE;
Query:
mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")
cur=mycon.cursor()
cur.execute(query)
for x in cur:
print(x)
show_all_data()
13
Program 20: Write code to connect a MYSQL database and then insert a record using python shell.
mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")
cur=mycon.cursor()
for i in range(n):
print(“record added”)
mycon.commit()
insert_record()
record added
14
Program 21: Write code to connect a MYSQL database and then update a record using python shell.
mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")
cur=mycon.cursor()
mycon.commit()
update_record()
record is update .
Program 22: Write code to connect a MYSQL database and then delete a record using python shell.
mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")
cur=mycon.cursor()
delete()
record is deleted .
THaNk YoU…
15