You are on page 1of 6

Nikhil Rai

22BCE11442

Practice Set 4
Nikhil Rai
22BCE11442

Q1) Write a Python program to create a file where all letters of English alphabet
are listed by specified number of letters on each line.

OUT
PUT

Q2) A text file “PYTHON.TXT” contains alphanumeric text. Write a program


that reads this text file and writes to another file “PYTHON1.TXT” entire file
except the numbers or digits in the file.
Code
Nikhil Rai
22BCE11442

OUTPUT

Q3) Write a Python program to combine each line from first file with the
corresponding line in second file.
Code

OUTPUT
Nikhil Rai
22BCE11442

Q4) Write a Python program to create a table and insert some records in that
table. Finally selects all rows from the table and display the records.
Code
import sqlite3
def create_table(cursor):
cursor.execute('''
CREATE TABLE IF NOT EXISTS example_table (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
''')
def insert_record(cursor, name, age):
cursor.execute("INSERT INTO example_table (name, age) VALUES (?, ?)",
(name, age))
def display_records(cursor):
cursor.execute("SELECT * FROM example_table")
rows = cursor.fetchall()
print("ID | Name | Age")
print("-----------------")
for row in rows:
print(f"{row[0]} | {row[1]} | {row[2]}")
def main():
conn = sqlite3.connect('example.db')# Connect to a SQLite database (or
create one if it doesn't exist)
cursor = conn.cursor()# Create a cursor object to interact with the
database
Nikhil Rai
22BCE11442

cursor.execute("DROP TABLE IF EXISTS example_table")# Drop the table if it


exists
create_table(cursor)# Create a table
num_records = int(input("Enter the number of records to add: "))# Insert
records based on user input
for _ in range(num_records):
name = input("Enter the name: ")
age = int(input("Enter the age: "))
insert_record(cursor, name, age)
# Commit the changes
conn.commit()
# Display all rows from the table
display_records(cursor)
# Close the connection
conn.close()
if __name__ == "__main__":
main()

OUTPUT

Q5) Write a Python program to display the details of jobs in descending sequence
on job title.
------------+---------------------------------+------------+------------+
| JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY |
+------------+---------------------------------+------------+------------+
| AD_PRES | President | 20080 | 40000 |
| AD_VP | Administration Vice President | 15000 | 30000 |
| AD_ASST | Administration Assistant | 3000 | 6000 |
Nikhil Rai
22BCE11442
| FI_MGR | Finance Manager | 8200 | 16000 |
| FI_ACCOUNT | Accountant | 4200 | 9000 |
| AC_MGR | Accounting Manager | 8200 | 16000 |
| AC_ACCOUNT | Public Accountant | 4200 | 9000 |
| SA_MAN | Sales Manager | 10000 | 20080 |
| SA_REP | Sales Representative | 6000 | 12008 |
| PU_MAN | Purchasing Manager | 8000 | 15000 |
| PU_CLERK | Purchasing Clerk | 2500 | 5500 |
| ST_MAN | Stock Manager | 5500 | 8500 |
| ST_CLERK | Stock Clerk | 2008 | 5000 |
| SH_CLERK | Shipping Clerk | 2500 | 5500 |
| IT_PROG | Programmer | 4000 | 10000 |
| MK_MAN | Marketing Manager | 9000 | 15000 |
| MK_REP | Marketing Representative | 4000 | 9000 |
| HR_REP | Human Resources Representative | 4000 | 9000 |
| PR_REP | Public Relations Representative | 4500 | 10500 |
+------------+---------------------------------+------------+------------+
Code
from prettytable import PrettyTable
# Data for the jobs
jobs_data = [
["AD_PRES", "President", 20080, 40000],
["AD_VP", "Administration Vice President", 15000, 30000],
["AD_ASST", "Administration Assistant", 3000, 6000],
["FI_MGR", "Finance Manager", 8200, 16000],
["FI_ACCOUNT", "Accountant", 4200, 9000],
["AC_MGR", "Accounting Manager", 8200, 16000],
["AC_ACCOUNT", "Public Accountant", 4200, 9000],
["SA_MAN", "Sales Manager", 10000, 20080],
["SA_REP", "Sales Representative", 6000, 12008],
["PU_MAN", "Purchasing Manager", 8000, 15000],
["PU_CLERK", "Purchasing Clerk", 2500, 5500],
["ST_MAN", "Stock Manager", 5500, 8500],
["ST_CLERK", "Stock Clerk", 2008, 5000],
["SH_CLERK", "Shipping Clerk", 2500, 5500],
["IT_PROG", "Programmer", 4000, 10000],
["MK_MAN", "Marketing Manager", 9000, 15000],
["MK_REP", "Marketing Representative", 4000, 9000],
["HR_REP", "Human Resources Representative", 4000, 9000],
Nikhil Rai
22BCE11442

["PR_REP", "Public Relations Representative", 4500, 10500]


]
# Create a PrettyTable instance
table = PrettyTable()
table.field_names = ["JOB_ID", "JOB_TITLE", "MIN_SALARY", "MAX_SALARY"]
# Add data to the table
for job in sorted(jobs_data, key=lambda x: x[1], reverse=True):
table.add_row(job)
# Display the table
print(table)
OUTPUT

You might also like