Final Document of Assinment and Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Computer Programs

&
Assignment

Submitted By
YASH SRIVASTAVA
Roll No. :-
Class: XII B
Under Guidance of
Mr. Deen Dayal Ram
(Department of Computer Science)
Department of Computer Science
Jugal Devi Saraswati Vidya Mandir
Deendayal Nagar Kanpur 208003

1
My Project On-Line Project …….
S.N. All Program Description Menu Based Date Page Teacher Sig
Range
1 Revision tour of python (10 List Operation ) 10 Aug 22 01-09
program Menu Based
2 Function program of python (10 Tuple And 11 Sep 22 10-15
Dictionary Operation ) Menu Based
3 File handling program(05 Text,Binary,And 12 Oct 22 16-19
CSV file Operation ) Menu Based
4 Stack program ( 04 PUS,POP,Peek,Display 15 Nov 22 20-22
Operation ) Menu Based
5 SQL Program (03) With Table Creating 20 Dec 22 23-33
,Query And Output

2
My Project On-Line Project …….
Program And Assignment
print("Welcome to menu based program of this Assignment")
print("1 for Revision tour of python program")
print("2 for Function program of python")
print("3 for File handling program")
print("4 for Stack program")
print("5 for exit")
ch= int(input("Enter the program zone in which you want to enter......"))
if ch==1:
print("1 for Program to print elements of a list['q','w','e','r','t','y'] \
in seperate lines along with \element's both indexes \
(positive and negtive)......")
print("2 for Obtaining a 2D List")
print("3 for Traversing a 2D List")
print("4 for Program that displays \
options for inserting or deleting elements in a list")
print("5 for Program that inputs a list, replicates its twice\
and then print the sorted list in ascending .")
print("6 for for Program to input names of n students and \
store them in tuple. Also, input a name from the user\
and find if this student is present in the tuple or no ")
print("7 for Program to find frequencies of all elements of a list.\
Also,print the list of unique elements in the list and \
duplicate elements in the given list")
print("8 for Program to check if the maximum element of \

3
My Project On-Line Project …….
the list lies in the first half of the list or in second half")
print("9 for 28 for Program to create a dictionary namely \
Dct with 10 keys each having values as 200.Update the \
first and last values as 200. Update the first and last")
print("10 for Program to input a list and an element, and \
remove all occurences of the given elements from the list")
ch= int(input("Enter the program no. you want to run=> "))
if ch==1:
L=['q','w','e','r','t','y']
length= len(L)
for a in range(length):
print("At indexes",a,"and",(a-length),"element:",L[a])
elif ch==2:
lst=[]
r= int(input("How many rows do you want..?"))
c= int(input("How many columns do you want..?"))
for i in range (r):
row= []
for j in range (c):
elem= int(input("Element"+str(i)+","+str(j)+": "))
row.append(elem)
lst.append(row)
print("List created is:",lst)
elif ch==3:
lst=[]
r= int(input("How many rows do you want..?"))
4
My Project On-Line Project …….
c= int(input("How many columns do you want..?"))
for i in range (r):
row= []
for j in range (c):
elem= int(input("Element"+str(i)+","+str(j)+": "))
row.append(elem)
lst.append(row)
print("List created is:",lst)
print("[")
for i in range(r):
print(" [",lst[i][j],end=" ")
for j in range(c):
print(lst[i][j],end=" ")
print("]")
print("]")
elif ch==4:
val=[17,23,18,19]
print("The list is :",val)
while True:
print("Main Menu")
print("1.Insert")
print("2.Delete")
print("3.Exit")
ch= int(input("Enter your choice 1/2/3: "))
if ch==1:
item=int(input("Enter item :"))
5
My Project On-Line Project …….
pos= int(input("Insert at which position...?"))
index= pos-1
val.insert(index,item)
print("Successful! List now is : ",val)
elif ch==2:
print(" Deletion Menu ")
print("1. Delete using Value")
print("2. Delete using Index")
print("3. Delete using a sublist ")
dch= int(input("Enter choice(1 or 2 or 3) : "))
if dch==1:
item= int(input("Enter item to be deleted :"))
val.remove(item)
print("List now is : ",val)
elif dch==2:
index= int(input("Enter index of item to be selected :"))
val.pop(index)
print("List now is :",val)
elif dch==3:
l= int(input("Enter lower limit of the list slice to be deleted
:"))
h= int(input("Enter upper limit of the list slice to be deleted
:"))
del val[l:h]
print("List now is :",val)
else:

6
My Project On-Line Project …….
print("Valid choices are 1/2/3 only")
elif ch==3:
break;
else:
print("Valid choices are 1/2/3 only..")
elif ch==5:
val= eval(input("Enter a list => "))
print("Original list: ",val)
val= val*2
print("Replicated list : ",val)
val.sort()
print("Sorted in ascending order :",val)
elif ch==6:
lst= []
n= int(input("How many students..?"))
for i in range (1,n+1):
name= input("Enter name of student" + str(i)+ ":")
lst.append(name)
ntuple= tuple(lst)
nm= input("Enter name to be searched for :")
if nm in ntuple:
print(nm,"exists in the tuple")
else:
print(nm,"does not exists in the tuple")

elif ch==7:
7
My Project On-Line Project …….
lst= eval(input("Enter list : "))
length= len(lst)
uniq= []
dup1= []
count=i=0
while i<length:
element= lst[i]
count=1
if element not in uniq and element not in dup1:
i+=1
for j in range(i,length):
if element== lst[j]:
count+=1
else:
print("Element",element,"frequency:",count)
print("Original List is => ",lst)
print("Unique elements List is => ",uniq)
print("Duplicates elements List is => ",dup1)
elif ch==8:
lst= eval(input("Enter a list:"))
ln= len(lst)
mx= max(lst)
ind= lst.index(mx)
if ind<=(ln/2):
print("The maximum element ",mx,"lies in the 1st half.")
else:
8
My Project On-Line Project …….
print("The maximum element",mx,"lies in the 2nd half.")
elif ch==9:
Dct=dict.fromkeys(range(10),200)
Dct[0]+=200
Dct[9]+=200
print(Dct)
elif ch==10:
lst= eval(input("Enter a list...=> "))
item= int(input("Enter element to be removed: "))
c= lst.count(item)
if c==0:
print(item,"Not in the list")
else:
while c>0:
i= lst.index(item)
lst.pop(i)
c= c-1
print("List after removing",item,":",lst)
elif ch==2:
while True:
print("1 for Program to print a tuple's first three and three
elements together.")
print("2 for Program to print elements of a
tuple('Hello','Isn't','Python','fun','?')\
in seperate lines along with element's both indexes(=ve
and -ve)")

9
My Project On-Line Project …….
print("3 for Program to print the index of the minimum element in
a tuple")
print("4 for Program to input a tuplemt1 and sort its elements. \
At the end of the program,all the elements of the tuple\
t1 should be sorted in ascnding order")
print("5 for Program function second largest(T) which takes \
as input a tuple T and returns the second largest \
element in the tuple.")
print("6 for Program to check if there are multiple \
maximum element in a tuple of not.")
print("7 for Program to read roll nmbers and marks of four \
students and create a dictionary from it having roll no. as
keys.")
print("8 for Program to create a phone dictionary for all your \
friends and print each key value pair in seperate lines.")
print("9 for Program to create a dictonary namely Numerals from
following two lists.")
print("10 for Program to create a dictionary namely \
Dct with 10 keys each having values as 200.Update the \
first and last values as 200. Update the first and last")
print("11 for exit")
ch= int(input("Enter your choice...."))
if ch==1:
t1= eval(input("Enter input for tuple: "))
print(t1[0],t1[-2])
print(t1[1],t1[-2])

10
My Project On-Line Project …….
print(t1[2],t1[-3])
elif ch==2:
T=('Hello','Isnot','Python','fun')
length=len(T)
for a in range(length):
print('At indexes',a,'and',(a-length),'element:',T[a])
elif ch==3:
tup= eval(input("Enter a tuple: "))
mn= min(tup)
print("Minimum element",mn,"is at index",tup.index(mn))
elif ch==4:
t1= eval(input("Enter a tuple: "))
lst= sorted(t1)
t1= tuple(lst)
print("Tuple after sorting: ",t1)
elif ch==5:
T= (23,45,34,66,77,67,70)
maxvalue= max(T)
length= len(T)
secmax= 0
for a in range(length):
if secmax < T[a] <maxvalue:
secmax= T[a]
print("Tuple already given is : ",T)
print("Second Largest value is: ",secmax)
elif ch==6:
11
My Project On-Line Project …….
tup= eval(input("Enter a tuple: "))
mx= max(tup)
if tup.count(mx)>1:
print("Contains multiple maximum elements.")
else:
print("Contains only one maximum element.")
elif ch==7:
rno= []
mks= []
for a in range(4):
r= eval(input("Enter Roll no: "))
m= eval(input("Enter marks of above roll no: "))
rno.append(r)
mks.append(m)

d={rno[0]:mks[0],rno[1]:mks[1],rno[2]:mks[2],rno[3]:mks[3]}
print("Created Dictionary ")
print(d)
elif ch==8:
PhoneDict= {"Vishal":1234567,"Vaishnavi":7654321,\

"Steven":6734521,"Sampree":4673215,"Rabiya":4563217}
for name in PhoneDict:
print(name,":",PhoneDict[name])
elif ch==9:
keys= ['One','Two','Three']

12
My Project On-Line Project …….
values= [1,2,3]
Numerals= dict(zip(keys,values))
print("Given two lists are:",keys,values)
print("Dictionary created with these list is")
print(Numerals)
elif ch==10:
Dct=dict.fromkeys(range(10),200)
Dct[0]+=200
Dct[9]+=200
print(Dct)
elif ch==11:
exit()
elif ch==3:
while True:
print("1 for reading text file")
print("2 for writing in text file")
print("3 for writing in binary file")
print("4 for reading n bytes and the reading some \
more bytes from the last position read")
print("5 for reading in CSV file")
print("6 for exit from file")
ch= int(input("Enter your choice......"))
if ch==1:
def read_textfile():
file= open("reference_file1.txt","r")
str1= file.read()
13
My Project On-Line Project …….
print(str1)
read_textfile()
elif ch==2:
def copy_textfile():
file= input("Enter file name and address= ")
sfile= open(file,"r")
tfile= open("reference_file2.txt","w")
l= sfile.readline()
while(l!=" "):
tfile.write(l)
l= sfile.readline()
sfile.close()
tfile.close()
copy_textfile()
elif ch==3:
def write_binaryfile():
import pickle
bfile= open("reference_file1.dat","wb")
str= "This is a binary write file program"
pickle.dump(str,bfile)
bfile.close()
write_binaryfile()
elif ch==4:
def read_nbytes():
file= input("Enter file name and address = ")
myfile= open(file,"r")
14
My Project On-Line Project …….
str= myfile.read(30)
print(str)
str2= myfile.read(50)
print(str2)
myfile.close()
read_nbytes()
elif ch==5:
def read_CSVfile():
import csv
file1= input("Enter file name and location =")
f= open(file1,"r")
fr= csv.reader(f)
for i in fr:
print(i)
f.close()
read_CSVfile()
elif ch==6:
exit()
elif ch==4:
def stack_program():
print("Thanks for visiting stack program")
def isEmpty(stk):
if stk==[]:
return True
else:
return False
15
My Project On-Line Project …….
def Push(stk,item):
stk.append(item)
top= len(stk)-1
def Pop(stk):
if isEmpty(stk):
return "Underflow"
else:
item= stk.pop()
if len(stk)==0:
top= None
else:
top= len(stk)-1
return item
def Peek(stk):
if isEmpty(stk):
return "Underflow"
else:
top= len(stk)-1
return stk[top]
def Display(stk):
if isEmpty(stk):
print("Stack empty")
else:
top= len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
16
My Project On-Line Project …….
print(stk[a])
Stack= []
top= None
while True:
print("STACK OPERATIONS")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display stack")
print("5.Exit")
ch= int(input("Enter your choice"))
if ch==1:
item= int(input("Enter item: "))
Push(Stack,item)
elif ch==2:
item= Pop(Stack)
if item=="Underflow":
print("Underflow! Stack is empty")
else:
print("Popped item is",item)
elif ch==3:
item= Peek(Stack)
if item=="Underflow":
print("Underflow! Stack is empty!")
else:
print("Topmost item is",item)
17
My Project On-Line Project …….
elif ch==4:
Display(Stack)
elif ch==5:
break
else: print("Invalid choice")
elif ch==5: exit()

18
My Project On-Line Project …….
Programs Output:-

19
My Project On-Line Project …….
20
My Project On-Line Project …….
21
My Project On-Line Project …….
22
My Project On-Line Project …….
23
My Project On-Line Project …….
24
My Project On-Line Project …….
25
My Project On-Line Project …….
SQL Based Query And Their Output
DataBase Show Command

Table Create :-
Q18_p607) Write a output for Sql queries (i) To (iii) which are based on
the table : Student given below :
Basic Command For Question Solution. ………
mysql> Create Database Assinment;
Query OK, 1 row affected (0.06 sec)
mysql> use Assinment;
Database changed
mysql> create table student (RollNo int(5),Name Varchar(20),Class
Varchar(5),Dob date,Gender Varchar(2),City Varchar(20),Marks
int(5));
Query OK, 0 rows affected (0.14 sec)
mysql> Desc Student;

26
My Project On-Line Project …….
Value Insert Into Table Query…..
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(1,'Nanda','X','1995-06-06','M','Agra',551);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(2,'Saurabh','XII','1993-05-07','M','Mumbai',462);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(3,'Sanal','XI','1994-05-06','F','Delhi',400);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(4,'Trisla','XII','1995-08-08','F','Mumbai',450);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(5,'Stort','XII','1995-10-08','M','Delhi',369);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(6,'Marisla','XI','1995-08-08','F','Mumbai',450);
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(7,'Neha','X','1995-08-12','F','Moscow',377);

27
My Project On-Line Project …….
insert into student (RollNo ,Name ,Class ,Dob ,Gender ,City,Marks) Values
(8,'Nishant','X','1995-06-12','M','Moscow',489);

mysql> Select count(*) ,City From Student Group By City Having


Count(*)>1;

mysql> Select Max(Dob),Min(Dob) From Student;

28
My Project On-Line Project …….
mysql> Select Name, Gender From Student Where City ="Delhi";
Q18_p607)
1) To Display the record from table student in alphabetical order as per the
name of the student.
2) To display class, Dob, and City whose marks is between 450 and 551.
3) To display name, Class and Total Number of students who have secured
more than 450 marks, class wise.

4) To increase marks of all students by 20 whose class is ‘XII’.


Answer :-
Query1)
mysql> Select * From Student Order by Name;

Query2)
mysql> Select Class, Dob,City From Student Where marks between 450
AND 551; OR

29
My Project On-Line Project …….
mysql> Select Class, Dob,City From Student Where marks >=450 AND
marks <= 551;

Query4)
mysql> Update Student Set Marks=Marks+20 Where Class ='XII';
Query OK, 3 rows affected (0.09 sec)
Rows matched: 3 Changed: 3 Warnings: 0 Query4)

30
My Project On-Line Project …….
Query4)
mysql> Select Name,Class, Count(*), marks From Student group by class
having marks>450;

Q21_609)Given the following tables for a database Library :


Basic Command For Question Solution. ………
mysql> Create Table Books (Books_Id Varchar(20),Book_Name
Varchar(20),Author_Name Varchar(20),Publishers Varchar(20), Price
Int(5), Type Varchar(20),Qty int(3));
Query OK, 0 rows affected (0.13 sec)
sql> desc Books;

mysql> select * from books;


mysql> Create Table Issued(Books_Id Varchar(20),Quantity_Issued int(2));
Query OK, 0 rows affected (0.11 sec)

31
My Project On-Line Project …….
mysql> desc Issued;

Write SQL Queries For (a) to (f) :


a) To show book name, Author name and Price of books of First Publ.
Publishers.
b) To list the name from books of text type.
c) To display the name and price from books in the ascending order of
their price.
d) To Increase the price of all books of EPB Publishers by 50.
e) To display the Book_Id, Book_Name, and Quantity_Issued for all
books which have issued. (The query ill require contents from both the
tables.)
f) To Insert a new row in the table issued having the following data :
“F0003”,1.
g) Give the output of the following queries based on the above tables:-
a. Select Count(*) from Books;
b. Select Max(Price) From Books Where Quantity >=15;
c. Select Book_Name,Author_Name From Books Where Publisers
=”EPB”;
d. Select Count(Distinct Publishers) From Books Where
Price>=400;

32
My Project On-Line Project …….
Answer :-
Query1)
mysql> select Books_Name,Author_Name,Price from books where
Publishers='First Publ';

Query2)
mysql> select Books_Name from Books Where Type='Text';

Query3)
mysql> select Books_Name,price from books order by price;

Query4)
mysql> Update Books Set price=price+50 Where Publishers='EPB';

33
My Project On-Line Project …….
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> Select * From Books;

Query4)
mysql> Select books.Books_Id,Books_Name,Quantity_Issued From
Books,Issued where books.Books_Id=issued.Books_Id;

Query5)
mysql> Insert Into Issued Values ("F0003",1);
Query OK, 1 row affected (0.06 sec)
Query6)
a) 5
b) 750
c) Fast Cook Lata Kapoor
MyFirst C++ Brain & Brooke d)1

34
My Project On-Line Project …….

You might also like