Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q1: Python Functions for Text File Operations

# a. Create a text file with some data

Data=input(“enter data”)

fob=open(‘record.txt’, 'w'):

fob.write(data)

# b. Read and display the number of words present in the text file

fob=open(‘record.txt’, 'r') as file:

content = fob.read()

words = content.split()

prinlen(words)

# c. Display all data of the text file after changing the case of each character

fob=open(‘record.txt’, 'r'):

content = fob.read()

print(content.upper())

# d. Display the number of words starting with a capital letter

fob= open(filename, 'r')

content =fob.read()

words = content.split()

count=0

for I in words:

if I[0].isupper():
count+=1

print(count)

Q2: Connectivity Question - SQL and Python

import mysql.connector as mc

# A. In SQL create a table EMPLOYEE (empno, empname, salary) and add two records.

conn =mc.connect(host=’localhost’,user=’root’,password=’school@123’)

cursor = conn.cursor()

cursor.execute(''CREATE TABLE EMPLOYEE (empno INT PRIMARY KEY,empname VARCHAR(30),salary


INT)'')

cursor.execute(''INSERT INTO EMPLOYEE VALUES({},’{}’,{})”.format(123,’abc’,5000)

cursor.execute(''INSERT INTO EMPLOYEE VALUES({},’{}’,{})”.format(133,’abc’,5000)

conn.commit()

# B. Implement a Python program using connectivity

# i. Add two more records in the table

cursor.execute(''INSERT INTO EMPLOYEE VALUES({},’{}’,{})”.format(223,’abc’,5000)


cursor.execute(''INSERT INTO EMPLOYEE VALUES({},’{}’,{})”.format(323,’abc’,5000)

conn.commit()

# ii. Display all the data from the table

cursor.execute('SELECT * FROM EMPLOYEE')

all_data = cursor.fetchall()

print("All Data from the EMPLOYEE table:")

for row in all_data:

print(row)

# iii. Display the record by taking empno from the user

emp_no = int(input("Enter empno to display record: "))

cursor.execute('SELECT * FROM EMPLOYEE WHERE empno=emp_no’)

record = cursor.fetchone()

if record:

print("Record found:", record)

else:

print("Record not found.")

You might also like