Set 3
Set 3
Q-1 a) Write a program to count a total number of lines and count the total number of lines
starting with 'A', 'B', and 'C' from the file myfile.txt. (8)
Example: If File Contents as given below:
Python is super and trending language.
Allows to store the output in the files.
A text file stores textual data. Binary files can handle binary data.
Binary files use pickle module to store data.
CSV files can handle tabular data.
CSV files can be read easily using CSV reader object.
SOURCE CODE:
def count():
with open("myfile.txt","r") as f1:
data=f1.readlines()
cnt_lines=0
cnt_A=0
cnt_B=0
cnt_C=0
for lines in data:
cnt_lines+=1
if lines[0]=='A':
cnt_A+=1
if lines[0]=='B':
cnt_B+=1
if lines[0]=='C':
cnt_C+=1
print("Total Number of lines are:",cnt_lines)
print("Total Number of lines strating with A are:",cnt_A)
print("Total Number of lines strating with B are:",cnt_B)
print("Total Number of lines strating with C are:",cnt_C)
f1.close()
count()
OUTPUT:
1. b
mysql> CREATE DATABASE SCHOOL;
Query OK, 1 row affected (0.20 sec)
d)
IN PYTHON
import mysql.connector
db=mysql.connector.connect(host='localhost',user='root',passwd=
'Sriram@123 ',database='school')
cursor=db.cursor()
cursor.execute("SELECT * FROM STU WHERE DEPT,'HISTORY'")
cursor.fetchall()
db.close()
OUTPUT:
[(2, 'Ankit', 'M', 21, 'HISTORY', datetime.date(1998, 3, 24), 200, None), (6, 'Deepa', 'F', 19,
'HISTORY', datetime.date(1997, 6, 27), 300, None)]