Anurag 12
Anurag 12
8 Create a CSV file by entering user-id and password, read and search the
password for given userid.
9 Create a student table and insert data. Implement the following SQL
commands on the student table: ALTER table to add new attributes /
modify data type / drop attribute
10 UPDATE table to modify data
13 GROUP BY and find the min, max, sum, count and average
19 Delete record from table for roll no. using python interface
for i in f:
words=i.split()
new_line="A#".join(words)
print(new_line)
OUTPUT:
PROGRAM 2
AIM: ● Read a text file and display the number of
vowels/consonants/uppercase/lowercase characters in the file.
SOURCE CODE:
def count_character(text):
vowels = “aeiouAEIOU”
vowels_count = sum(1 for char in text if char in vowels)
consonants_count = sum(1 for char in text if char.isalpha() and char not in vowels)
print(f”vowels: {vowels_count},Constants:{consonants_count}”)
count_characters(“hello world”)
print(“Anurag”)
OUTPUT:
PROGRAM 3
AIM: Remove all the lines that contain the character 'a' in a file and
write it to another file
SOURCE CODE:
With open (‘Original.txt’) as f,open(‘Duplicate.txt’,’w’) as file:
File.writelines(line for line in f if ‘a’ not in line)
OUTPUT:
PROGRAM 4
AIM: Create a binary file with name and roll number. Search for a
given roll number and display the name, if not found display
appropriate message
SOURCE CODE:
Import pickle
With open(‘students.dat’,’wb’) as f:
Pickle.dump({18:’anurag’,19:’anshik’},f)
Def search(roll):
Try:
With open(‘students.dat’,’rb’) as f:
Except FileNotFounderror:
Print(‘File Not Found.’)
Search(18)
OUTPUT:
PROGRAM 5
AIM: Create a binary file with roll number, name and marks. Input a
roll number and update the marks.
SOURCE CODE:
import pickle
record =[]
while True:
data=[roll_no,name,marks]
record.append(data)
if choice.upper()=="N":
break
f=open("student",'wb')
pickle.dump(record,f)
print("Record is added")
f.close()
OUTPUT:
PROGRAM 6
AIM: Write a random number generator that generates random
numbers between 1 and 6 (simulates a dice).
SOURCE CODE:
Import random
def roll_dice():
return random.radiant(1,6)
result = roll_dice()
print(“you rolled a:”, result)
OUTPUT:
PROGRAM 7
AIM: Write a Python program to implement a stack using list.
SOURCE CODE:
Stack = []
def pop():
return stack.pop() if stack else “empty”
push(1)
push(2)
push(1234)
print(pop())
OUTPUT:
PROGRAM 8
AIM: Create a CSV file by entering user-id and password, read and
search the password for given userid.
SOURCE CODE:
import csv
header=["User id","password"]
f=open("Employee.csv","w",newline='')
write=csv.writer(f,delimiter=',')
write.writerow(header)
write.writerows(row)
f.close()
f=open("Employee.csv","r")
csv_reader=csv.reader(f)
if row[0]==n:
print("Password is : ",row[1])
f.close()
OUTPUT:
PROGRAM 9
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: ALTER table to add new
attributes / modify data type / drop attribute
SOURCE CODE:
OUTPUT:
PROGRAM 10
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: UPDATE table to modify data
SOURCE CODE:
OUTPUT:
PROGRAM 11
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table:ORDER By to display data in
ascending / descending order
SOURCE CODE:
OUTPUT:
PROGRAM 12
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: DELETE to remove tuple(s)
SOURCE CODE:
OUTPUT:
PROGRAM 13
AIM: Create a student table and insert data. Implement the following
SQL commands on the student table: GROUP BY and find the min,
max, sum, count and average
SOURCE CODE:
OUTPUT:
PROGRAM 14
AIM: Create table student inside database “School” using python
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )
cur=db.cursor()
q1="use school;"
q2='''create table students( name varchar(50), std_id int PRIMARY KEY, class char(5), age int ); '''
cur.execute(q)
cur.execute(q1)
cur.execute(q2)
db.commit()
OUTPUT:
PROGRAM 15
AIM: Insert multiple records in a table using python interface
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )
cur=db.cursor()
='use school;'
cur.execute(a)
cur.execute(q)
cur.execute(q1)
cur.execute(q2)
cur.execute(q3)
db.commit()
OUTPUT:
PROGRAM 16
AIM: To display all records of table using python interface
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )
cur=db.cursor()
cur.execute("use school;")
row = cur.fetchall()
for i in row:
print(i)
OUTPUT:
PROGRAM 17
AIM: Search a record using string template with %s formatting
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )
cur=db.cursor()
cur.execute("use school;")
a='kanika'
row = cur.fetchall()
for i in row:
print(i)
OUTPUT:
PROGRAM 18
AIM: Search a record using string template with {} and format
function
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123" )
a='naina’
row = cur.fetchall()
for i in row:
print(i)
OUTPUT:
PROGRAM 19
AIM: Delete record from table for roll no. using python interface
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anuragr@123" )
cur=db.cursor()
cur.execute("use school;")
db.commit()
OUTPUT:
PROGRAM 20
AIM: Write a code to connect to a Mysql database namely School and
then fetch all those records from table Student where grade is ‘A’.
SOURCE CODE:
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",password="anurag@123")
cur=db.cursor()
cur.execute("use school;")
cur.execute(q)
row =cur.fetchall()
for i in row :
print(i)
OUTPUT: