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

PROGRAM FILE-10 (1)

Uploaded by

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

PROGRAM FILE-10 (1)

Uploaded by

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

ACADEMIC YEAR 2024-2025

NAME : UMANG PATEL


STD : XII-
ROLL NO :
SUBJECT : COMPUTER SCIENCE
TOPIC : PROGRAMMING IN PYTHON /
MySQL QUERIES
RYAN INTERNATIONAL SCHOOL
CBSE SURAT
BONAFIDE CERTIFICATE

This is to certify that the program file submitted by


UMANG PATEL of class XII- , is considered as the part
of AISSCE conducted by CBSE is a bonafide record of
the work carried out under our guidance and supervision
at
RYAN INTERNATIONAL SCHOOL, CBSE, SURAT.

INTERNAL EXAMINER EXTERNAL


EXAMINER

HEAD OF INSTITUTION
INDEX OF CONTENTS
SR No Title Signatur Signatur
e e
(Teache (Examine
r) r)

PYTHON PROGRAMS

1 PROGRAM TO READ A LINE AND PRINT NO. OF


UPPERCASE, LOWERCASE, DIGITS AND
ALPHABETS.
2 PROGRAM TO CALCULATE NO. OF WORDS,
CHARACTERS AND PERCENTAGE OF
CHARACTERS THAT ARE ALPHA NUMERIC.
3 PROGRAM TO SORT A LIST USING BUBBLE SORT.
4 PROGRAM TO SORT A LIST USING INSERTION
SORT.
5 PROGRAM TO READ A STRING AND CHECK
WHETHER IT IS PALLINDROME OR NOT.
6 PROGRAM TO ROTATE THE ELEMENTS OF A LIST.
7 PROGRAM TO PRINT THE LONGEST WORD IN LIST
OF WORDS.
8 PROGRAM TO GENERATE A RANDOM NUMBER IN
A GIVEN RANGE THRICE.
9 PROGRAM TO WRITE A PYTHON FUNCTION
SIN(X,N) TO CALCULATE THE VALUE OF SIN(X)
USING TAYLORS SERIES EXPANSION UPTO ‘N’
TERMS.
10 PROGARM TO FIND MINIMUM ONE’S DIGIT IN TWO
NUMBERS.
11 PROGRAM TO READ A FILE LINE BY LINE AND
PRINT IT.
12 PROGRAM TO COUNT WORDS ‘TO’ AND ‘THE’ IN A
TEXT FILE.
13 PROGRAM TO CONVERT BINARY NUMBER TO
DECIMAL
14 PROGRAM TO CONVERT OCTAL NUMBER TO
DECIMAL
15 PROGRAM TO WRITE DATA TO CSV FILE.
16 PROGRAM TO PERFORM BINARY SEARCH IN AN
ARRAY.
17 PROGRAM TO READ THE CONTENTS OF BINARY
FILE.
18 PROGRAM TO IMPLEMENT STACK OPERATIONS.
19 PROGRAM TO MODIFY THE CONTENTS OF BINARY
FILE.
20 PROGRAM TO FIND THE SUM OF GEOMETRIC
PROGRESSION SERIES
21 PROGRAM TO INTEGRATE MYSQL WITH PYTHON
BY INSERTING RECORDS TO EMP TABLE AND
DISPLAY THE RECORDS.
22 PROGRAM TO INTEGRATE MYSQL WITH PYTHON
TO SEARCH AN EMPLOYEE USING EMPID AND
DISPLAY THE RECORD IF PRESENT IN ALREADY
EXISTING TABLE EMP, IF NOT DISPLAY THE
APPROPRIATE MESSAGE.
23 PROGRAM TO INTEGRATE MYSQL WITH PYTHON
TO SEARCH AN EMPLOYEE USING EMPID AND
UPDATE THE SALARY OF AN EMPLOYEE IF
PRESENT IN ALREADY EXISTING TABLE EMP, IF
NOT DISPLAY THE APPROPRIATE MESSAGE.
24 INTEGRATE SQL WITH PYTHON BY IMPORTING
THE MYSQL MODULE.

MYSQL COMMANDS

Create a Database and Table(s) in MySQL and


perform the following:
1 Write a MySQL Query using 'AND'
2 Write a MySQL Query using 'OR'
3 Write a MySQL Query using 'NOT'
4 Write a MySQL Query using 'BETWEEN'
5 Write a MySQL Query using 'IN'
6 Write a MySQL Query using 'LIKE'
7 Write a MySQL Query using 'DISTINCT'
8 Write a MySQL Query using 'COUNT'
9 Write a MySQL Query using 'HAVING'
10 Write a MySQL Query using 'ORDER BY'
11 Write a MySQL Query using 'GROUP BY'
12 Write a MySQL Query using 'MAX', 'MIN', 'AVG'
# PROGRAM 1:- TO READ THE LINE AND COUNT AND PRINT THE NUMBER
OFUPPERCASE, LOWERCASE, DIGITS AND ALPHABETS.

line=input("Enter a line:")
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
else:
alphacount+=1
print("Number of uppercase letters:",uppercount)
print("Number of lowercase letters:",lowercount)
print("Number of alphabets:",alphacount)
print("Number of digits:",digitcount)

# OUTPUT:-

Enter a line: Hello 123, ZIPPY zippy zap


Number of uppercase letters: 6
Number of lowercase letters: 12
Number of alphabets: 18
Number of digits: 3
# PROGRAM 2:- TO CALCULATE NUMBER OF WORDS, CHARACTERS AND
PERCENTAGE OF CHARACTERS THAT ARE ALPHA NUMERIC.

s=input("Enter a string and press enter:-")


t=0
print("Number of words=",len(s.split()))
l=len(s)
print("Number of characters=",l)
fori in s:
if i.isalnum():
t+=1
else:
break
p=(t/l)*100
print("Percentage of alphanumeric characters=",p,"%")

# OUTPUT: -

Enter a string and press enter:- hi!! how are you? @user
Number of words =5
Number of characters= 23
Percentage of alphanumeric characters= 8.695652173913043 %
# PROGRAM 3:-TO SORT A LIST USING BUBBLE SORT.

alist=[23,5,72,45,12,3,9]
print("Original list is:",alist)
n=len(alist)
for i in range(n):
for j in range(0,n-i-1):
if alist[j]>alist[j+1]:
alist[j],alist[j+1]=alist[j+1],alist[j]
print("List after sorting:",alist)

# OUTPUT:-

Original list is: [23, 5, 72, 45, 12, 3, 9]


List after sorting: [3, 5, 9, 12, 23, 45, 72]
# PROGRAM 4:- TO SORT A SEQUENCE USING INSERTION SORT.

alist= [15,6,13,22,3,52,2]
print("Original list is:",alist)
for i in range(1,len(alist)):
key=alist[i]
j=i-1
while j>=0 and key<alist[j]:
alist[j+1]=alist[j]
j=j-1
else:
alist[j+1]=key
print("List after sorting:",alist)

# OUTPUT:-

Original list is: [15, 6, 13, 22, 3, 52, 2]


List after sorting: [2, 3, 6, 13, 15, 22, 52]
# PROGRAM 5:-TO READ A STRING AND CHECK WHETHER IT IS
PALINDROME OR NOT.

string=input("Enter a string:")
n=len(string)
rev=''
for i in range(n-1,-1,-1):
rev=rev+string[i]
if rev==string:
print("IT IS A PALINDROME")
else:
print("IT IS NOT A PALINDROME")

# OUTPUT:-

Enter a string: MALAYALAM


IT IS A PALINDROME
Enter a string: COMPUTER
IT IS NOT A PALINDROME
# PROGRAM 6:- TO ROTATE THE ELEMENTS OF A LIST.

l=[]
lr = [ ]
n=int(input("Enter the number of elements in list:-"))
for i in range(0,n):
s=int(input("Enter the number:-"))
l.append(s)
print("Original list is",l)

for j in range(-1,-(n+1),-1):
p=l[j]
lr.append(p)
print("Reversed list is",lr)

# OUTPUT:-

Enter the number of elements in list:-8


Enter the number:-9
Enter the number:-74
Enter the number:-6
Enter the number:-32
Enter the number:-88
Enter the number:-14
Enter the number:-20
Enter the number100
Original list is [9, 74, 6, 32, 88, 14, 20, 100]
Reversed list is [100, 20, 14, 88, 32, 6, 74, 9]

# PROGRAM 7:- TO PRINT THE LONGEST WORD IN THE LIST OF WORDS.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=input("Enter element" + str(x+1) + ":")
a.append(element)
max1=len(a[0])
temp=a[0]
for i in a:
if (len(i)>max1):
max1=len(i)
temp=i
print("The word with the longest length is:")
print(temp)

# OUTPUT:-

Enter the number of elements in list:5


Enter element1:PUNE
Enter element2:BOMBAY
Enter element3:CHENNAI
Enter element4:DELHI
Enter element5:SURAT
The word with the longest length is:
CHENNAI
# PROGRAM 8:- GENERATE A RANDOM NUMBER IN A GIVEN RANGE THRICE.

import random

def rand(n,m):
s=random.randint(n,m)
return s

a=int(input("Enter the starting value of range:-"))


b=int(input("Enter the ending value of range:-"))
for i in range (0,3):
print("Random number generated is:-",rand(a,b))

# OUTPUT:-

Enter the starting value of range:-10


Enter the ending value of range:-1000
Random number generated is:- 264
Random number generated is:- 415
Random number generated is:- 19
# PROGRAM 9:- TO WRITE A PYTHON FUNCTION SIN(X,N) TO CALCULATE
THE VALUEOF SIN(X) USING TAYLORS SERIES EXPANSION
UPTO ‘N’ TERMS.

from math import pi


def fac(x):
f=1
for i in range(1,x+1):
f=f*i
return f
def sin(x,n):
s=0
ctr=0
for i in range(1,n+1,2):
ctr+=1
s+=((-1)**(ctr+1))*(x**i)/fac(i)
return s
print(sin(pi/2,5))

# OUTPUT:-

1.0045248555348174
# PROGRAM 10:- MINIMUM ONE’S DIGIT IN TWO NUMBERS.

def comp(a,b):
a1=a%10
b1=b%10
if a1<b1:
return a
elif b1<a1:
return b
else:
return 0
n1=int(input("Enter the first number:-"))
n2=int(input("Enter the second number:-"))
ans=comp(n1,n2)
if ans==0:
print("Both have same one's digit")
else:
print(ans,"has minimum value of one's digit")

# OUTPUT:-

Enter the first number:-491


Enter the second number:-278
491 has minimum value of one's digit

Enter the first number:-12


Enter the second number:-42
Both have same one's digit
# PROGRAM 11:-TO READ A FILE LINE BY LINE AND PRINT IT.

FILE TO BE READ:-

# CODE :-
f=open("poem.txt","r")
l=f.readlines()
for i in l:
print(i)
# OUTPUT:-

Where the mind is without fear and the head is held high

Where knowledge is free

Where the world has not been broken up into fragments

By narrow domestic walls

Where words come out from the depth of truth

Where tireless striving stretches its arms towards perfection

Where the clear stream of reason has not lost its way

Into the dreary desert sand of dead habit

Where the mind is led forward by thee

Into ever-widening thought and action

Into that heaven of freedom, my Father, let my country awake.


# PROGRAM 12:- TO COUNT WORDS ‘TO’ AND ‘THE’ IN A TEXT FILE.
FILE TO BE READ:-

#CODE:-
text = open("poem.txt",'r')
word1="to"
word2="the"
c1=0
c2=0
for line in text:
words=line.split()
l=len(words)
for i in range(0,l):
s=words[i]
if s==word1:
c1=c1+1
if s==word2:
c2=c2+1
print("The word 'to' occurs",c1,"times")
print("The word 'the' occurs",c2,"times")

# OUTPUT:-
The word 'to' occurs 0 times
The word 'the' occurs 7 times

# PROGRAM 13:- TO CONVERT BINARY NUMBER TO DECIMAL NUMBER.

b_num = list (input ("Input a binary number: "))

value = 0

for i in range(len(b_num)):

digit = b_num.pop()

if digit == '1':

value = value + pow(2, i)

print("The decimal value of the number is", value)

# OUTPUT:-

Input a binary number: 11001110


The decimal value of the number is 206
# PROGRAM 14: PROGRAM TO CONVERT OCTAL NUMBER TO DECIMAL.

def octalToDecimal(n):
num = n;
dec_value = 0;

base = 1;

temp = num;
while (temp)
last_digit = temp % 10;
temp = int(temp / 10);
dec_value += last_digit * base;
base = base * 8;
return dec_value;

num = 45621;
print(octalToDecimal(num));

# OUTPUT:-

19345
# PROGRAM 15: TO WRITE DATA TO CSV FILE.

import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]

filename = "university_records.csv"
with open(filename, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)

# OUTPUT:-
# PROGRAM 16:-BINARY SEARCH IN AN ARRAY.

def bsearch (ar,item):


beg=0
last=len(ar)-1
while beg <= last:
mid=(beg+last)//2
if item==ar[mid]:
return mid
elif item > ar[mid]:
beg=mid+1
else:
last=mid-1
else:
return False
n=int(input("Enter size of linear list"))
print("Enter elements in ascending order")
ar=[0]*n
for i in range(n):
ar[i]=int(input("Element"+str(i)+":"))
item=int(input("Enter element to be searched"))
ind=bsearch(ar,item)
if ind:
print("Element found at index",ind)
else:
print("Element not found!")
# OUTPUT:-

Enter size of linear list:-5


Enter elements in ascending order
Element0:-1
Element1:-2
Element2:-3
Element3:-4
Element4:-5
Enter element to be searched:-4
Element found at index- 3

Enter size of linear list:-5


Enter elements in ascending order
Element0:-1
Element1:-2
Element2:-3
Element3:-4
Element4:-5
Enter element to be searched-9
Element not found!
# PROGRAM 17:- PROGARM TO READ THE CONTENTS OF BINARY FILE.

import pickle
emp1={'Empno':1201,'Name':'anushree','Age':25,'Salary':47000}
emp2={'Empno':1211,'Name':'zoya','Age':30,'Salary':48000}
emp3={'Empno':1251,'Name':'alex','Age':31,'Salary':50000}

empfile=open('emp.txt','wb')

pickle.dump(emp1,empfile)
pickle.dump(emp2,empfile)
pickle.dump(emp3,empfile)
empfile.close()

# OUTPUT:-
# PROGRAM 18:- IMPLEMENTATION OF STACK OPERATIONS.

def isempty(stk):
if stk==[]:
return True
else:
return False

def push(stk,item):
stk.append(item)
top=len(stk)-1

def pop(stk):
if isempty(stk):
print("Underflow!")
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item

def peek(stk):
if isempty(stk):
print ("Underflow!")
else:
return stk[len(stk)-1]

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):
print(stk[a])

stack=[]
top=None
while True:
print("Stack Operations")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Dislay 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)
print("popped item is",item)
elif ch==3:
item=peek(stack)
print("Topmost item is",item)
elif ch==4:
display(stack)
elif ch==5:
break
else:
print("Invalid choice!")
# OUTPUT:-

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-6

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-8

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-2

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-1
Enter item:-4

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-4
4 <-top
2
8
6

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-3
Topmost item is:-4

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-2
popped item is :-4

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-4
2 <-top
8
6

STACK OPERATIONS
1. Push
2. Pop
3. Peek
4. Display Stack
5. Exit
Enter your choice:-5
# PROGRAM 19:- TO MODIFY THE CONTENTS OF BINARY FILE.

def update_binary(word, new):

string = b""

Flag = 0

with open('file.txt', 'r+b') as file:

pos = 0
data = string = file.read(1)

while data:
data = file.read(1)

if data == b" ":

if string == word:

file.seek(pos)

Flag = 1
break
else:

pos = file.tell()
data = string = file.read(1)
else:

string += data
continue

if Flag:
print("Record successfully updated")
else:
print("Record not found")

word = input("Enter the word to be replaced: ").encode()


new = input("\nEnter the new word: ").encode()

update_binary(word, new)
# OUTPUT:-

Contents of binary file

TEXT FILE
# PROGRAM 20:- PROGRAM TO FIND THE SUM OF GEOMETRIC
PROGRESSION SERIES.

import math
a = int(input("Please Enter First Number of an G.P Series: : "))
n = int(input("Please Enter the Total Numbers in this G.P Series: : "))
r = int(input("Please Enter the Common Ratio : "))

total = (a * (1 - math.pow(r, n ))) / (1- r)


tn = a * (math.pow(r, n - 1))

print("\nThe Sum of Geometric Progression Series = " , total)


print("The tn Term of Geometric Progression Series = " , tn)

# OUTPUT:-

Please Enter First Number of an G.P Series: 1


Please Enter the Total Numbers in this G.P Series: 5
Please Enter the Common Ratio : 2

The Sum of Geometric Progression Series = 31.0


The tn Term of Geometric Progression Series = 16.0
# PROGRAM 21:- TO INTEGRATE MYSQL WITH PYTHON BY INSERTING
RECORDS TO Emp TABLE AND DISPLAY THE RECORDS.

# OUTPUT:-
# SQL OUTPUT :
# PROGRAM 22:- TO INTEGRATE MYSQL WITH PYTHON TO SEARCH AN
EMPLOYEE USING EMPID AND DISPLAY THE RECORD IF
PRESENT IN ALREADY EXISTING TABLE EMP, IF NOT
DISPLAY THE APPROPRIATE MESSAGE.

#OUTPUT:-
Python Executed Program Output:
# SQL OUTPUT :
# PROGRAM 23: TO INTEGRATE MYSQL WITH PYTHON TO SEARCH AN
EMPLOYEE USING EMPID AND UPDATE THE SALARY OF AN
EMPLOYEE IF PRESENT IN ALREADY EXISTING TABLE EMP,
IF NOT DISPLAY THE APPROPRIATE MESSAGE.

# OUTPUT :
Python Executed Program Output:
#SQL OUTPUT :
# PROGRAM 24: INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL
MODULE.

import mysql.connector

con=mysql.connector.connect(host='localhost', user='root',
password='A123456z',db='jinal')
a=con.cursor()

sql=" select * from school"


a.execute(sql)
myresult=a.fetchall()
for result in myresult:
print(result)

# OUTPUT:-

(2, 'ruhani', datetime.date(2002, 12, 23), 'adajan')


(1, 'priya', datetime.date(2002, 2, 2), 'citylight')
(3, 'parineeti', datetime.date(2002, 5, 15), 'vasu')

Create a Database and Table(s) in MySQL and perform the


following:
1. MYSQL QUERY USING ‘AND’

2. MYSQL QUERY USING ‘OR’


3. MYSQL QUERY USING ‘NOT’

4. MYSQL QUERY USING ‘BETWEEN’

5. MYSQL QUERY USING ‘IN’


6. MYSQL QUERY USING ‘LIKE’

7. MYSQL QUERY USING ‘DISTINCT’

8. MYSQL QUERY USING ‘HAVING’


9. MYSQL QUERY USING ‘ORDER BY’

10. MYSQL QUERY USING ‘GROUP BY’

11. MYSQL QUERY USING ‘COUNT’


12. MYSQL QUERY USING ‘MAX’, ‘MIN’, ‘AVG’

You might also like