Program File Finalsr
Program File Finalsr
VARANASI
Program on
Python and SQL
Session : 2020-2021
1|Page
index
S.NO CONTENT Pg.NO
1) The day number in the year in the range 2 to 365 and ask the first
day of the year Sunday or Monday or Tuesday etc.
5) Dictionary add three name and their phone in the dictionary after
getting input from user
11) Write a Python Program to Multiply Two Matrices using nested list
12) Write a program to count the words "to" and "the" present in a
text file "Poem.txt"
2|Page
15) Write a program to read and display content of file from end to
beginning.
19) Write a program to search and row count content of a CSV file.
20) Write a program to search and row count content of a CSV file.
22) Write a python program to read a file line by line store it into an
array.
1: Write a program that ask the user the day number in the year in the
range 2 to 365 and ask the first day of the year Sunday or Monday or
3|Page
Tuesday etc. Then the program should display the day on the day
number that has been input.
d = {"Sunday":1,"Monday":2,"Tuesday":3,"Wednesday":4,"Thursday":5,
"Friday":6,"Saturday":7}
days = { 1 :"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",
6:"Friday",7:"Saturday"}
dic= { }
user = int(input("Enter a number between 2 to 365 = "))
first = d[day]
print(first)
for j in range(1,366) :
dic[ j ] = first
if first == 7 :
first = 0
first += 1
a = dic[ user ]
print(days [ a ])
4|Page
mon = ["january", "february","march" , "april", "may" , "june" , "july" ,
"august ", "september ", "october ", "november" , 'december ']
for a in range(mid):
if
string[a]==string[rev
]: a +=1 rev -= 1
else:
print(string, “is not palindrome”)
break
else:
print(string, “is palindrome”)
5|Page
4:Write a Python Program to Solve a simple Quadratic
Equation.
import cmath
a =int(input(“enter A:”))
b = int(input(“enter B:”))
c = int(input(“enter C:”))
d = (b**2) - (4*a*c)
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
dic = {}
6|Page
name = input ("Enter the name :- ") phone =
input ("Enter the Phone number :- ")
dic[ name ] = phone
while True :
a = input("Enter the name from the dictionary
(For exit enter 'q'):- “)
if a == "q"or a== "Q":
break
else :
print (dic[a])
6: Write a Python function that accepts a string and calculate
the number of uppercase letters and lowercase letters.
def string_test(s):
d = {"UPPER_CASE" : 0, "LOWER_CASE" : 0}
for c in s:
if c.isupper():
d["UPPER_CASE"] += 1
elif c.islower ():
d["LOWER_CASE"] += 1
else:
pass
print("Series with initial value", ini, "& step value", st, "goes as:")
8|Page
doll = float(input("Enter dollar : "))
rup(doll) #void
9|Page
n1, n2 = 0, 1
count = 0
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]] # result
10|Page
is 3x4 result =
[[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for r in result:
print(r)
12: Write a program to count the words "to" and "the" present
in a text file "Poem.txt".
to_no = 0
the_no = 0
for i in lst :
11|Page
word = i.split()
for j in word :
if j == "to" :
to_no += 1
elif j == "the" or j == "The" :
the_no += 1
file.close()
13: Write a program that takes a number and checks if it is a
happy number.
12|Page
else :
num = str( sum_sq_digits (num) )
Ishappy( num )
def DISPLAYWORDS() :
file = open("story.txt",
"r") lst = file.readlines()
for i in lst :
word = i.split()
for j in word :
if len( j ) < 4 :
print( j )
file.close()
DISPLAYWORDS()
15: Write a program to read and display content of file from
end to beginning.
f1 = open("pathwala.txt",'r')
13|Page
data = f1.readlines()
content = "" for i in
data : content +=
i+"\n"
f1.close()
14|Page
16. Write a program to read and display content of a CSV file-
(‘tras.csv’, ‘w’).
import csv
fh=open('tras.csv','w')
sw=csv.writer(fh)
sw.writerow(["name","class","b.no.","area"])
for I in range(5):
print('enter your info')
nm=input('name:-')
cl=int(input('class:-'))
b_no=int(input('bus number:-'))
area=input("enter your area")
stu=[nm,cl,b_no,area]
sw.writerow(stu) fh.close()
import csv
file.close()
18 : Write a program to read and display content of a
CSVfile-(‘tras.csv’, ‘w’).
def push(dict):
l=list() for i
in dict :
if dict[i]>75:
l.append(i)
return l
def pop(L1):
l2=[] if
len(L1)!=0:
for i in range(0,len(L1)):
x=L1.pop()
l2.append(x)
return l2
16|Page
d1={'OM':76,'JAI':45,'BOB':89,'ALI':65,'ANU':90,'TOM':82}
l=push(d1) print(pop(l))
import csv
ob=open('data.csv','r+'
) fb=csv.reader(ob) def
ssearch(AdmNo): for i
in fb: if i[0]==AdmNo:
print("records present :","admno name class marks ",'\n',i)
def rcount():
count=0
for i in fb:
count+=1
print('No of records=',count)
x=int(input('Enter adm no'))
ssearch(x)
17|Page
if com == "push" or com == "Push" :
num = int(input("Enter a number : "))
stack.insert(0,num)
elif len(stack) == 0 :
print("Underflow")
import csv
ob=open('data.csv','w+')
f=csv.writer(ob,delimiter='|')
f.writerow(['admno','name','class','marks'])
f.writerow(['10486','Ishaan','12','50'])
f.writerow(['10445','devansh','12','50'])
f.writerow(['10487','satvik','12','50'])
18|Page
22: Write a python program to read a file line by line store it
into an array.
def file_read(fname):
content_array = []
with open(fname) as
f:
#Content_list is the list that contains the read lines.
for line in f:
content_array.append(line)
print(content_array)
file_read('test.txt')
19|Page
23: Create a table named ‘GYM’ to store information about
fitness items being sold in it.
#Query to Add a new row for new item in GYM with the details :
"G107", "Vibro exerciser", 21000, "GTCFitness" insert
into GYM values ("G107", "Vibro exerciser", 21000,
"GTCFitness") ;
25: Program for connectivity as Interface python with
MySQL.
if mycon.is_connected():
print('connected')
21|Page
cur=con.cursor()
cur.execute("select * from
stu") data=cur.fetchall()
print(data)
22|Page
26. Showing Queries for Cartesian Product.
23|Page
28. Showing Queries for Equi-join and Natural- join.
24|Page
29. Queries on the database School .
25|Page
26|Page
27|Page