[go: up one dir, main page]

0% found this document useful (0 votes)
4 views15 pages

Jitesh File

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)
4 views15 pages

Jitesh File

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/ 15

program

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()

Output: enter a number:6


the number is not prime.
Program 2: Write a program to get the reverse of a number.
Code:
def reverse_no():
num=int(input("enter a number:"))
r=0
rev=0
while num>0:
r=num%10
rev=(rev*10)+r
num=num//10
print("the reverse of the number is:",rev)
reverse_no()

Output: enter a number:567


the reverse of the number is: 765
Program 3: Write a program to get the sum of a list.
Code:
def sum_list():
lst=[]
num=int(input("enter a limit:"))
for i in range(num):
element=int(input("enter a element:"))
lst.append(element)
print(lst)
s=0
for i in lst:
s+=i
print("the sum of the list is:",s)
sum_list()

Output: enter a limit:3


enter a element:45
enter a element:56
enter a element:67
[45, 56, 67]
the sum of the list is: 168
Program 4: Write a program to search a letter in a string.
Code:
def search():
st=input("enter a string:")
search=input("enter a letter to be searched:")
if search in st:
print("found")
else:
print("not found")
search()

output: enter a string:Radhey Radhey


enter a letter to be searched:R
found

Program 5: Write a program to print the fibonaccies sequence.


Code:
def fibonacci():
a=0
b=1
i=0
num=int(input("enter steps:"))
while i<num:
print(a)
c=a+b
a=b
b=c
i=i+1
fibonacci()

Output: enter steps:4


0
1
1
2

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()

Output: the no. of vowels are 42

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()

Output: enter the email address=shreejee123@email.com


Valid

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()

Output: Artificial intelligence.


Application development.

Program 10: Write a program to generate a dice’s no. randomly.


Code:
import random
print("random dice no is :",random.randint(0,6))

Output: random dice no is : 4

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()

Output: enter a limit:5


enter a adm_no:1
enter a name:Anu
enter a class:9
enter a adm_no:2
enter a name:Bhumi
enter a class:9
enter a adm_no:3
enter a name:Radha
enter a class:9
enter a adm_no:4
enter a name:Ram
enter a class:9
enter a adm_no:5
enter a name:shyam
enter a class:9

enter a searched element:1


1
Anu
9

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()

Output: enter a limit:5


enter adm_no:1
enter name of the student:Anu
enter class:9
enter adm_no:2
enter name of the student:Bhumi
enter class:9
enter a adm_no:3
enter a name:Radha
enter a class:9
enter a adm_no:4
enter a name:Ram
enter a class:9enter a adm_no:5
enter a name:shyam
enter a class:9

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()

Output: record is added

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")

def search_student(filename, admission_no):


fr= open("C:\\Users\\jites\\OneDrive\\filename", "rb")
student = pickle.load(fr)
for i in student:
if student['admission_no'] == admission_no:
print("Student Details:")
print(f"Admission No.: {student['admission_no']}")
print(f"Name: {student['name']}")
print(f"Class: {student['class']}")
print(f"Age: {student['age']}")
return
else:
print("record not found")

#main
filename = "Diya.dat"
ch="y"
while ch=="y":
print("\nMenu:")
print("1. Add Student")
print("2. Search Student")
print("3. Exit")

choice = input("Enter your choice: ")


if choice == '1':
add_student(filename)
elif choice == '2':
admission_no = input("Enter Admission No. to search: ")
search_student(filename, admission_no)
elif choice == '3':
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
ch=input("do you want to run this again....")

6
Output: Menu:
1. Add Student
2. Search Student
3. Exit

Enter your choice: 1


enter the limit of record=>5
Enter Admission No.: 101
Enter Name: Ram
Enter Class: 6
Enter Age: 11
Enter Admission No.: 102
Enter Name: Shyam
Enter Class: 7
Enter Age: 12
Enter Admission No.: 103
Enter Name: Radha
Enter Class: 8
Enter Age: 13
Enter Admission No.: 104
Enter Name: Sita
Enter Class: 9
Enter Age: 14
Enter Admission No.: 105
Enter Name: Krishna
Enter Class: 10
Enter Age: 15

Student details added successfully.

do you want to run this again....y

Menu:
1. Add Student
2. Search Student
3. Exit

Enter your choice: 2


Enter Admission No. to search: 101
Student Details:
Admission No.: 101
Name: Ram
Age: 12

do you want to run this again....y

Menu:
1. Add Student
2. Search Student
3. Exit
Enter your choice: 3

Exiting the program.

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:

Program 17 Write SQL query based on the following table:

(1): Write SQL query to create the table.

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:

Program 18 Write SQL query based on the following table:

(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:

(5): SELECT * FROM GRADUATE WHERE NAME LIKE “% I %”;

Query:

12
(6): SELECT DISTINCT RANK FROM GRADUATE;

Query:

pYTHoN WITH SQL


Program 19: Write code to connect a MYSQL database and then display all the records of any table in database
using python shell.

Code: def show_all_data():

import mysql.connector as msql

mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")

cur=mycon.cursor()

a=input("enter the table name=")

query="select * from {}".format(a)

cur.execute(query)

for x in cur:

print(x)

show_all_data()

Output: enter the table name=client

(3, 'LIVE LIFE', 'DELHI', 'BS01')

(2, 'TOTAL HEALTH', 'MUMBAI', 'FW05')

(5, 'DREAMS', 'DELHI', 'FW12')

(4, 'PRETTY WOMAN', 'DELHI', 'SH06')

(1, 'COSMETIC SHOP', 'DELHI', 'TP01')

13
Program 20: Write code to connect a MYSQL database and then insert a record using python shell.

Code: def insert_record():

import mysql.connector as msql

mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")

cur=mycon.cursor()

n=int(input("enter the limit of insert record="))

for i in range(n):

roll_no=int(input("enter the roll no. of student="))

s_name=input("enter the student name=")

gender=input("enter the gender of student=")

dob=input("enter the DOB of student=")

fees=int(input("enter the fees of student="))

hobby=input("enter the hobby=")

cur.execute("insert into student


values({0},'{1}','{2}','{3}',{4},'{5}')".format(roll_no,s_name,gender,dob,fees,hobby))

print(“record added”)

mycon.commit()

insert_record()

Output: enter the limit of insert record=1

enter the roll no. of student=107

enter the student name=Radha

enter the gender of student=F

enter the DOB of student=2007-02-06

enter the fees of student=2300

enter the hobby=Crafting

record added

14
Program 21: Write code to connect a MYSQL database and then update a record using python shell.

Code: def update_record():

import mysql.connector as msql

mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")

cur=mycon.cursor()

hobby=input("enter the new hobby=")

roll_no=int(input("enter the roll no where you change="))

cur.execute("update student set hobby='{0}' where roll_no={1}".format(hobby,roll_no))

print("record is update .")

mycon.commit()

update_record()

Output: enter the new hobby=Painting

enter the roll no where you change=107

record is update .

Program 22: Write code to connect a MYSQL database and then delete a record using python shell.

Code: def delete():

import mysql.connector as msql

mycon=msql.connect(host="localhost",user="root",passwd="123",database="graduate")

cur=mycon.cursor()

roll_no=int(input("enter the roll no that you want to delete="))

cur.execute("delete from student where roll_no={0}".format(roll_no))

print("record is deleted .")

delete()

Output: enter the roll no that you want to delete=107

record is deleted .

THaNk YoU…
15

You might also like