0% found this document useful (0 votes)
3 views

Comp.science

The document outlines a programming file for a Computer Science course focusing on Python and MySQL for the academic year 2024-25. It includes various programming exercises, such as creating lists, tuples, dictionaries, and functions, as well as database operations using MySQL. Additionally, it contains SQL practical exercises involving table creation, data insertion, and query writing.

Uploaded by

Aryan Kamboj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Comp.science

The document outlines a programming file for a Computer Science course focusing on Python and MySQL for the academic year 2024-25. It includes various programming exercises, such as creating lists, tuples, dictionaries, and functions, as well as database operations using MySQL. Additionally, it contains SQL practical exercises involving table creation, data insertion, and query writing.

Uploaded by

Aryan Kamboj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

PROGRAMMING FILE

Computer Science
(Python& MySQL)
(2024-25)

Created by
Name: Aryan Kamboj
Class XII B
Roll No: __________

Program 1: WAP to display frequencies of all the element of a list.


#Program 2: Write a program to accept values from a user and create a
tuple.
#Program 3: WAP to input name of a student and class and roll no, it in a
dictionary and display in tabular form also search and display for a
particular name.
#Program4 WAP to print Fibonacci Series up to n terms, also find sum of
series
#Program 5: Write a program to find factorial of entered number using
library function fact ().
#Program 6: WAP to display frequencies of the Monday as 1st day of
month in 2021.
#Program 7:Write a function Div() which takes a 10 elements numeric
tuple and return the sum of elements which are divisible by 3 & 5
#Program 8: WAP to read data from a text file DATA.TXT, and display
word which have maximum/minimum characters.
#Program 9: WAP to read a file And Print The Contents of file along
with numbers of words and frequency of word ‘the’ in it
#Program 10: Write a program to write a string in the binary file
“Pbx.dat” and count the number of times a character appears in the
given string using a dictionary.
#Program 11: Write a function to input a list and arrange in ascending
order using BUBBLE SORT.
#Program 12: Write a program that rotates the elements of a list so that
the element at the first index moves to the second index, the element in
the second index moves to the third
Index, etc., and the element in the last index moves to the first index.

#PROGRAM 13: A school wants to make an online application form on


website for registration of students who wants to apply for the various
school leaders post. The form requires first name, last name and post.

#Write a Menu driven program that provides functions for :


#a) Selecting only those names entered entries where the first
letter of the first name and last name are capitalized
#b) Selecting only the incorrectly entered names
#c) Returning a list with corrected names.

#PROGRAM 14: Write a program to show push and pop operation using
stack.
#PROGRAM 15 Write a python code to add student using MySQL
connectivity and show result to the examiner. Create database and table
as below:
Name of database =school
Name of table = student (roll, name, age, class, city) give primary key
and not null constraints
#Program 1: WAP to display frequencies of all the element of a list.

L=[5,15,21,5,21,8,15,6]
L1=[]
L2=[]
fori in L:
ifi not in L2:
x=L.count(i)
L1.append(x)
L2.append(i)
print('element','\t\t\t',"frequency")
fori in range (len(L1)):
print(L2[i],'\t\t\t',L1[i])
#Program 2: Write a program to accept values from a user and create a
tuple.

t=tuple()

n=int(input("enter limit:"))

fori in range(n):

a=input("enter number:")

t=t+(a,)

print("output is")

print(t)
#Program 3: WAP to input name of a student and class and roll no, it in a
dictionary and display in tabular form also search and display for a
particular name.

d={}
i=1
n=int(input("enter number of entries"))
whilei<=n:
name=input("enter name of student")
cls=int(input("enter class"))
rollno=int(input("enter roll no"))
d[name]= [cls , rollno]
i=i+1
l=d.keys()
print("\n name\t\t","class\t\t","rollno")
fori in l:
k=d[i]
print(i,'\t\t',end=" ")
for j in k:
print(j,'\t\t',end='\t\t')
x=input("\nenter name to be searched:\ ")
fori in l:
ifi==x:
print("\n name\t\t","class\t\t","rollno")
k=d[i]
print(i,'\t\t',end=" ")
for j in k:
print(j,'\t\t',end="\t")
break
#Program4 WAP to print Fibonacci Series up to n terms, also find sum of
series

n= int(input("enter the number of terms in Fibonacci series "))

a,b=0,1

c=a+b

print(a,b,end=" ")

fori in range (n-2):

print(a+b,end=" ")

a,b = b,a+b

c=c+b

print()

print("sum of ",n,"terms if series =",c)


#Program 5: Write a program to find factorial of entered number using
library function fact().

import math

def fact(n):

if n<2:

return 1

else :

return n*fact(n-1)

x=int(input("Enter value for factorial : "))

ans=math.factorial(x)

print ("the factorial of the no.is ",ans)


#Program 6: WAP to display frequencies of the Monday as 1st day of month
in 2021.

importdatetime

fromdatetime import datetime

M1 = 0

months = range(1,13)
for year in range(2021, 2022):
for month in months:
ifdatetime(year, month,1).weekday() == 0:
M1 += 1
print('Monday','/t/t/t','frequency ', M1)
#Program 7: Write a function Div() which takes a 10 elements numeric
tuple and return the sum of elements which are divisible by 3 & 5

def div(n):
s=0
fori in n:
if i%3==0 and i%5==0:
s=s+i
return s

l=[]
fori in range(10):
print("enter the ", i + 1,"th number of the tuple ",end = '',sep
="")
t=int(input("enter"))
l.append(t)
n=tuple(l)
print("tuple=",n)
print("sum of number in tuple divisible by 3 &5",div(n))
#Program 8: WAP to read data from a text file DATA.TXT, and display
word which have maximum/minimum characters.

f1=open("Pbx.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
maxC=len(words[0])
minC=len(words[0])
minfinal=""
maxfinal=""
for word in words[1:]:
length=len(word)
ifmaxC<length:
maxC=length
maxfinal=word
ifminC>length:
minC=length
minfinal=word
print("Max word : ",maxfinal,", maxC: ",maxC)
print("Min word : ",minfinal,", minC: ",minC)
#Program 9: WAP to read a file And Print the Contents of file along with
numbers of vowels and frequency of word ‘the’ in it

f = open( "str.txt","r")

b=f.read()

word1=0

ctr=0

v=['a','e','i','o','u']

print("Contents of file:")

print(b)

for i in b:

if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':

ctr = ctr +1

f.close()

print()

print(" number of vowels",ctr)

f1= open( "str.txt","r")

str = f1.read()

word=str.split()

print( 'frequency of "the" = ', word.count('the'))


#Program 10: Write a program to write a string in the binary file
“Pbx.dat” and count the number of times a character appears in the given
string using a dictionary.

import pickle
str="this is business i'mdoin business "
a=open('Pbx.dat','wb')
pickle.dump(str,a)

a.close()

a=open('comp.dat','rb')
str=pickle.load(a)
print("\n\nthe string in the binary file is : \n",str)
dl={}
for x in str:
if x not in dl:
dl[x]=1
else:
dl[x]=dl[x]+1
print("\nThe count of each letter of string is :\n", dl)
a.close()
#Program 11: Write a function to input a list and arrange in ascending
order using BUBBLE SORT.

l = eval(input( "Enter the list "))

fori in range (len(l)-1 ):

for j in range(len(l)-1) :

if l[j]>l[j+1]:

l[j],l[j+1]=l[j+1],l[j]

print("arranged list:",l)
#Program 12: Write a program that rotates the elements of a list so that the
element at the first index moves to the second index, the element in the
second index moves to the third
index, etc., and the element in the last index moves to the first index.

n=int(input("Enter Number of items in List: "))


DATA=[]
fori in range(n):
item=int(input("Item :%d: "%(i+1)))
DATA.append(item)
print("Now List Items are :",DATA)
lg=len(DATA)
print("Now Number of items before update are :",lg)
b=["undef"]*lg
for x in range(lg-1)):
if (x)>lg:
break
b[x+1]=DATA[x]
b[0]=DATA[-1]
print("RESULT OF NEW LIST IS " , b)
#PROGRAM 13: A school wants to make an online application form on
website for registration of students who wants to apply for the various
school leaders post. The form requires first name, last name and post.
#Write a Menu driven program that provides functions for:
#a) Selecting only those names entered entries where the first letter
of the first name and last name are capitalized
#b) selecting only the incorrectly entered names
#c) Returning a list with corrected names.

defselect_errors(STL):
newlist=[]
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if name[0].islower() or surname[0].islower():
newlist.append(record)
returnnewlist

defselect_correct(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname = name_surname[1]
if not name[0].islower() and not surname[0].islower():
newlist.append(record)
returnnewlist

defcorrect_entries(STL):
newlist = []
for record in STL:
name_surname = record.split(' ')
name = name_surname[0]
surname =name_surname[1]
newlist.append(name.capitalize()+''+
surname.capitalize())
returnnewlist

#__main__
STL = []
ch = 0
while (ch != 4):
print("\t----")
print("\tMENU")
print("\t----")
print("1.Apply for the School Post")
print("2.List of all applicants")
print("3.Correct the Incorrect Enteries")
print("4.Exit")
ch = int(input("Enter your choice (1-4):"))
ifch == 1:
name = input("Enter your name :")
STL.append(name)
elifch == 2:
print("\tStudents applied so far:")
print(STL)
elifch == 3:
ok_enteries = select_correct(STL)
error_enteries = select_errors(STL)
corrected_enterie = correct_enteries(STL)
print("Correctly entered names:", ok_enteries)
print("Incorrectly entered names;",error_enteries)
print("corrected names:", corrected_enteries)
elifch !=4:
print("valid choices are1..4:")
else:
print("THANK YOU")
#PROGRAM 14:Write a program to show push and pop operation using
stack.

#stack.py
def push(stack,x): #function to add element at the end of
list
stack.append(x)
def pop(stack): #function to remove last element from list
n = len(stack)
if(n<=0):
print("Stack empty....Pop not possible")
else:
stack.pop()
def display(stack): #function to display stack entry
iflen(stack)<=0:
print("Stack empty...........Nothing to display")
fori in stack:
print(i,end=" ")
#main program starts from here
x=[]
choice=0
while (choice!=4):
print("********Stack Menu***********")
print("1. push(INSERT)")
print("2. pop(DELETE)")
print("3. Display ")
print("4. Exit")
choice = int(input("Enter your choice :"))
if(choice==1):
value = int(input("Enter value "))
push(x,value)
if(choice==2):
pop(x)
if(choice==3):
display(x)
if(choice==4):
print("You selected to close this program")
#PROGRAM 15 Write a python code to add student using MySQL
connectivity and show result to the examiner. Create database and table as
below:
Name of database =school
Name of table = student (roll, name, age, class, city) give primary key
and not null constraints

import mysql.connector
from mysql.connector import Error

def insert_rec():
try:
connection =
mysql.connector.connect(host='127.0.0.1',database='school',u
ser='root',passwd='123456')

print('success')
cursor=connection.cursor()
cursor.execute('use school')
r=int(input('Enter Rno'))
n=input('Enter Name to be added in table')
a =int(input('Enter Age'))
c=int(input('Enter Class'))
ci = input('Enter City')

cursor.execute("insert into student values (" + str(r) +", ' "


+ n+" ',"+str(a)+","+str(c)+" , ' " +ci +" ' )" )

connection.commit()
except Error as e:
print('error in connection ',e)

# call it
insert_rec()
SQL PRACTICAL FILE
Q1. Consider the following table named ACCESSORIES and answer the
following questions based on it.
Field name Type Width Constraint

No Varchar 3 Primary Key

Name Varchar 30 NOT NULL

Price Integer 10

ID Varchar 3 NOT NULL

Type Varchar 10

Company Varchar 20 NOT NULL

Qty Integer 5

Buy_Date Date NOT NULL

1. Create the above given Table.

2. Insert 5 records in the table


3. Write the SQL queries:
o To display Name and Price of all the Accessories in ascending order
of their Price

o To display maximum Price of all accessories grouped by their


ID.

o Introduce 5% discount on all accessories having ID as S02 or


S03.
Q2. Consider the following tables and writ SQL Queries to:-
TABLE: SENDER

SenderID SenderName SenderAddress SenderCity


ND01 R Jain 2, ABC Appts New Delhi
MU02 H Sinha 12,Newton Mumbai
MU15 S Jha 27/A, Park Street Mumbai
ND50 T Prasad 122-K,SDA New Delhi
TABLE: RECIPIENT
RecID SenderID RecName RecAddress RecCity
KO05 ND50 R Bajpayee 5,Central Avenue Kolkata
ND08 MU02 S Mahajan 116 ,A Vihar New Delhi
MU19 ND01 H Singh 2A, Andheri Mumbai
MU32 MU15 P N Swamy B5, CS Terminus Mumbai
ND48 ND50 S Tripathi 13, B1 D Vihar New Delhi

(a) To display the names of all Senders from Mumbai.


(b) To display the RecID, SenderName, SenderAddress,
RecName, RecAddressfor every Recipient.

(c) To display Recipient details in ascending order of


RecName.

(d) T
o

display number of Recipients from each City.

(e) To display names of city where senders are available (no


value should be repeated).

(f) Displ
a
y
SenderName for every Recipient who resides in Mumbai.

(g) Count number of recipients in each city.

You might also like