Question: 16
Write a python program to read a file named “story.txt”, count and print total lines starting with vowels in the file?
Sol:
filepath = 'story.txt'
vowels="AEIOUaeiou"
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
if(line[0] in vowels):
#print(line)
print("Line {}: {}".format(cnt, line.strip()))
cnt=cnt+1
line = fp.readline()
Output:
Line 1: amit
Line 2: owl
Line 3: Eat apple a day and stay healthy
Line 4: Anmol
>>>
Question: 17
Python program to plot a sine wave using a line chart
Sol:
#program to plot a sine wave using a line chart import matplotlib.pyplot as plt import numpy as np xvals=np.arange(-2,1,0.01) yvals=np.sin(xvals) #evaluate function on xvals create line plot with xvals and yvals plt.plot(xvals,yvals) show the grid plt.grid(True) plt.show()
Output:

Question:18
Python program to plot bar chart
Sol:
#program to plot bar chart
import matplotlib.pyplot as plt
import numpy as np
objects=("python","c++","Java","Perl","C","Lisp")
y_pos=np.arange(len(objects))
performance=[10,8,5,4,2,1]
plt.xlabel="Usage"
plt.ylabel="Languages"
plt.title("Programming language usage")
plt.bar(y_pos,performance,align="center",width=.5, color='r')
plt.barh(y_pos,performance,align="center", color='r')
plt.show()
Output:

Question:19
Python interface with MySQL
Write a function to insert a record in table using python and MySQL interface.
Sol:
def insert_data():
#take input for the details and then save the record in the databse
#to insert data into the existing table in an existing database
import pymysql
# Open database connection
#conn=pymysql.connect(host='localhost',user='root',password='',db='test')
db = pymysql.connect("localhost","root","","test4")
# prepare a cursor object using cursor() method
c = db.cursor()
r=int(input("Enter roll no "))
n=input("Enter name ")
p=int(input("Enter per "))
try:
# execute SQL query using execute() method.
c.execute("insert into student (roll,name,per) values (%s,%s,%s)",(r,n,p))
#to save the data
db.commit()
print("Record saved")
except:
db.rollback()
# disconnect from server
db.close()
# function calling
insert_data()
Output:
Enter roll no 101
Enter name amit
Enter per 97
Record saved
>>>
Program 20:
Python interface with MySQL
Write a function to display all the records stored in a table using python and MySQL interface.
Sol:
def display_all():
#display the records from a table
#field by field
import pymysql
# Open database connection
#conn=pymysql.connect(host='localhost',user='root',password='',db='test')
db = pymysql.connect("localhost","root","","test4")
# prepare a cursor object using cursor() method
try:
c = db.cursor()
sql='select * from student;'
c.execute(sql)
countrow=c.execute(sql)
print("number of rows : ",countrow)
#data=a.fetchone()
data=c.fetchall()
#print(data)
print("=========================")
print("Roll No Name Per ")
print("=========================")
for eachrow in data:
r=eachrow[0]
n=eachrow[1]
p=eachrow[2]
# Now print fetched result
print(r,' ',n,' ',p)
print("=========================")
except:
db.rollback()
# disconnect from server
db.close()
# function calling
display_all()
Output:
number of rows : 2
=========================
Roll No Name Per
=========================
102 aaa 99
101 amit 97
=========================
>>>
Program :21
Python interface with MySQL
Write a function to search a record stored in a table using python and MySQL interface.
Sol:
def search_roll():
#searching a record by roll no
#display the records from a table
#field by field
import pymysql
# Open database connection
#conn=pymysql.connect(host='localhost',user='root',password='',db='test')
db = pymysql.connect("localhost","root","","test4")
# prepare a cursor object using cursor() method
try:
z=0
roll=int(input("Enter roll no to search "))
c = db.cursor()
sql='select * from student;'
c.execute(sql)
countrow=c.execute(sql)
print("number of rows : ",countrow)
#data=a.fetchone()
data=c.fetchall()
#print(data)
for eachrow in data:
r=eachrow[0]
n=eachrow[1]
p=eachrow[2]
# Now print fetched result
if(r==roll):
z=1
print(r,n,p)
if(z==0):
print("Record is not present")
except:
db.rollback()
# disconnect from server
db.close()
# function calling
search_roll()
Output:
Enter roll no to search 101
number of rows : 2
101 amit 97
>>>
CBSE Class 12 @ Python
Tutorials | Technical Questions | Interview Questions |
|---|---|---|
|
C Programming C++ Programming Class 11 (Python) Class 12 (Python) |
C Language C++ Programming Python |
C Interview Questions C++ Interview Questions C Programs C++ Programs |




