Ayush
Ayush
Kalyanpur
Session: 2024-2025
if num==sum:
print(num," is a perfect number")
else:
print(num," is not a perfect number")
Q10. Write a function in Python that counts the number of “Me” or
“My” (in smaller case also) words present in a text file “STORY.TXT”.
If the “STORY.TXT” contents are as follows:
My first book was Me and My Family. It gave me chance to be Known
to the world.
The output of the function should be: Count of Me/My in file: 4
Ans:
def countmemy():
f=open("story.txt","r")
d=f.read()
m=d.split()
c=0
for i in m:
if i=="me" or i=="my" or i=="Me" or i=="My":
c=c+1
print("Count of me or my in file",c)
countmemy()
Q11. Write a user defined function in Python that displays the number
of lines starting with ‘H’ in the file story.txt. Eg: if the file contains:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
def pop():
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
Output:-
Q 14: Write a function push(l) to perform push opration in stack. Where a list is
given:
L=[10,12,15,16,17,18,20,21,25]
Push only those element who is divisible by 5.
def push(l):
L2.append(l)
L2=[]
L1=[10,12,15,16,17,18,20,21,25]
for i in L1:
if i%5==0:
push(i)
print("Original List:\n",L1)
print("List after using push:\n",L2)
Output:-
Q15. Write a function Push() which takes "name" as argument and add in a
stack named "MyStack". After calling push() three times, a message should be
displayed "Stack is Full"
st=[ ]
StackSize=3
def push():
y="y"
while y=="y":
sn=input("Enter name of student:")
if len(st)<StackSize:
st.append(sn)
else:
print("Stack is full!")
print(st)
break
y=input("Do you want to enter more name(y/n):")
Output:-
Part-2
SQL Queries
Answer the following questions according to given tables:
T_id Name Age Department Doj Salary Gender Adhar_no
1 Jugal gupta 34 CS 2017-01-10 12000 M 987655
2 Sharmilla 31 History 2008-03- 20000 F 998877
24
3 Sandeep 32 Maths Null 30000 M 887766
4 Sangeeta 35 History 2015-07-01 40000 F 776655
5 Rakesh 42 Maths 2007-09- 25000 M Null
gupta 05
6 Shyam singh 50 History Null 30000 M 554433
7 Shiv om 44 CS 2017-02-25 21000 M 443322
8 Shalakha 33 Maths 2018-07-31 20000 F 332211
9 Shyam 35 Maths 2016-09- 40000 M 221100
Kumar 24
10 Moahn 30 Maths 2016-09- 25000 M 110022
kumar 24
Table: Teacher
7) To display name, age and salary of those teacher who’s name ends with ‘a’.
8) To display name, age and salary of those teacher who’s name contains letter ‘a’ at the second place.
Ans: select name,age,salary from teacher where name like "_ _ _ _ _ _ _ ";
10) To display the number of all teacher whose name ends with ‘Kumar’.
Ans: insert into teacher (t_id, name, department, salary) values (12, 'Arun kumar', 'CS',25000);
12) Write query to change department of all male teacher to ‘History’.
15) Query to delete all record at once free the memory of table.
22) Write query to display name and place of all teacher belongs to delhi.
Ans: select name,place from teacher natural join posting where place=’delhi’;
23). Write query to display number of teachers city-wise.
Ans: select place,count(place) from teacher natural join posting group by place;
24) Write query to display number of teachers city-wise. Order by city name.
Ans: select place,count(place) from teacher natural join posting group by place order by place;
25) Write query to display number of teachers city-wise if in each city having more than 3 teachers
order by city name.
Ans: select place,count(place) from teacher natural join posting group by place having count(place)>3;
Part C
Programs Based on
Python-SQL
Connectivity
Q1: Write a program to Creating database and Show database in
Mysql through Python.
Code:
import mysql.connector
mydb=mysql.connector.connect
(host="localhost",
user="root",
password="123456")
mycursor=mydb.cursor()
mycursor.execute("create database demo")
mycursor.execute("Show databases")
for i in mycursor:
print(i)
Q2: Write a program to Creating table inside any school database
through Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("create table std_details3\
(rno int primary key, name varchar(20), age int, city
varchar(20))")
std=mycursor.execute("desc std_details3")
for i in mycursor:
print(i)
Q3: Write a program to Display all male teachers data from ‘teacher’
table using fetchall() Through Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
mycursor.execute("select name,department from teacher where
gender='m'")
myrecords=mycursor.fetchall()
for i in myrecords:
print(i)
print("Total number of rows retrieved",mycursor.rowcount)
Q4: Write a Parameterized program to insert data in std_details3 table
Through Python-Mysql connectivity program.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="123456",database="school")
mycursor=mydb.cursor()
y="y"
while y=="y":
r=int(input("Enter rollno:"))
n=input("Enter name:")
a=int(input("Enter age:"))
c=input("Enter City:")
mycursor.execute("insert into std_details3\
values({},'{}',{},'{}')".format(r,n,a,c))
mydb.commit()
print(mycursor.rowcount,"Record Inserted")
y=input("Do you want to insert more data:")
Thanks
🙏🏻