Ilovepdf Merged Organized (1)[1]
Ilovepdf Merged Organized (1)[1]
…………………….......
Teacher Incharge
……………….……………… .............................
……………………………….. Principal’s Signature
Examiner’s Signature MRS. RITU ATRI
31-11-2024
2 WRITE A FUNCTION THAT ACCEPTS A NO. &
CHECK WHETHER A NO. IS HAPPY NO.OR NOT?
17
WRITE A PYTHON CODE TO CREATE THE TABLE 5-12-2024
SALESPERSON WITH GIVEN STRUCTURE AND
CONSTRAINT.
18
W. A. P. TO INPUT DETAILS OF EMPLOYEE 5-12-2024
(EMPNO, NAME, AGE, SALARY) FROM USER
AND STORE IT IN A DATABASE TABLE
EMPLOYEE. ALSO, DISPLAY STORED DATA ON
19 MONITOR BY EXTRACTING IT FROM TABLE.
CODE:
#Program to capitalize each word
def capitalize(s):
words = s.split()
capitalized_words = []
for w in words:
capitalized_words.append(w.capitalize())
return ' '.join(capitalized_words)
# Example usage:
sentence = input("Enter a sentence: ")#function invoking
print("Capitalised sentence is : ",capitalize(sentence))
OUTPUT:
Q 2 .WRITE A FUNCTION THAT ACCEPTS A NO. & CHECK
WHETHER A NO. IS HAPPY NO.OR NOT?
CODE:
#program to check whether a no. is happy no. or not
def is_happy_number(n):
def sum_of_squares(num):
return sum(int(digit) ** 2 for digit in str(num))
visited = set()
while n != 1 and n not in visited:
visited.add(n)
n = sum_of_squares(n)
return n == 1
CODE:
def euCount():
# Ask user for input text
user_input = input("Enter the text to be written to the file: ")
# Open the file in write mode to create or overwrite it with user
input
with open("IMP.TXT", "w") as file:
file.write(user_input + "\n") # Write user input to the file
# Initialize counters for 'e' and 'u'
count_e = 0
count_u = 0
CODE:
#function to count the no of digits in a text file
def CountDig():
# Ask the user to input text
user_input = input("Enter the text to be written to the file: ")
file = open("STORY.TXT", "w")
file.write(user_input)
file.close()
count_digits = 0
file.close()
# Display the count of digits
print(f"Number of digits in the file: {count_digits}")
def writeRecord():
# Open the file in append mode to add new records
file = open('EmpRecord.csv', mode='a', newline='')
writer = csv.DictWriter(file, fieldnames=['name', 'age', 'salary'])
file.close()
print("Data for 5 students has been written to 'Record.csv'.")
writeStudentRecords()#function invoking
OUTPUT:
CODE:
importcsv
def CreateFile():
file = open('Book.csv', mode='a', newline='')
writer = csv.DictWriter(file, fieldnames=['BookNo', 'Book_Name',
'Author', 'Price'])
if file.tell() == 0:
writer.writeheader()
book_no = input("Enter Book Number: ")
book_name = input("Enter Book Name: ")
author = input("Enter Author Name: ")
price = input("Enter Price: ")
writer.writerow({'BookNo': book_no, 'Book_Name': book_name,
'Author': author, 'Price': price})
file.close()
print("Record added to 'Book.csv'.")
def DisplayRec():
file = open('Book.csv', mode='r')
reader = csv.DictReader(file)
print(f"{row['BookNo']:<10}{row['Book_Name']:<20}{row['Author']:<20}
{row['Price']:<10}")
file.close()
print("Choose an option:")
print("1. Add a new book record")
print("2. Display all book records")
if choice == '1':
CreateFile()
elif choice == '2':
DisplayRec()
else:
print("Invalid choice.")
OUTPUT:
import pickle
def get_data():
data = {}
for i in range(5):
name = input(f"Enter name of employee {i + 1}: ")
salary = float(input(f"Enter salary of {name}: "))
data[name] = salary
return data
save_file("EmpRecord.dat", data)
OUTPUT:
def pop(self):
returnself.stack.pop() if self.stack else "Stack is empty."
def peek(self):
returnself.stack[-1] if self.stack else "Stack is empty."
def display(self):
for book in self.stack:
print(f"ISBN: {book['ISBN']}, Name: {book['name']}")
# Pop a book
print("\nPopped book:", book_stack.pop())
B.
C.
D.
E.
Q 18. WRITE A PYTHON CODE TO CREATE THE TABLE
SALESPERSON WITH GIVEN STRUCTURE AND CONSTRAINT.
CODE:
IMPORTMYSQL.CONNECTOR
CONN=MYSQL.CONNECTOR.CONNECT(HOST="localhost",USER="root",PAS
SWORD="root",DATABASE="computer")
conn =
mysql.connector.connect( host="local
host",
user="root",
password="Tiger@1234",
database="employee"
)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Employee
( EmpNo INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Age INT NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
)
''')
emp_no = int(input("Enter Employee Number (EmpNo): "))
name = input("Enter Employee Name: ")
age = int(input("Enter Employee Age: "))
salary = float(input("Enter Employee Salary: "))
cursor.execute('''
INSERT INTO Employee (EmpNo, Name, Age, Salary)
VALUES (%s, %s, %s, %s)
''', (emp_no, name, age, salary))
def create_table():
# Connect to the database
conn =
mysql.connector.connect( host="localho
st",
user="root",
password="root",
database="product"
)
cursor = conn.cursor()
def update_price():
# Connect to the database
conn =
mysql.connector.connect( host="localho
st",
user="root",
password="root",
database="product"
)
cursor = conn.cursor()
# Update prices by 5%
cursor.execute('''
UPDATE Product
SET Price = Price * 1.05
''')
conn.commit() # Commit the changes
print("Prices updated by 5%.")
conn.close() # Close the connection
def display_products():
# Connect to the database
conn =
mysql.connector.connect( host="localho
st",
user="root",
password="root",
database="product"
)
cursor = conn.cursor()
# Fetch all products
cursor.execute('SELECT * FROM Product')
rows = cursor.fetchall()
# Display product details
if rows:
print("Product Details (with updated prices):")
for row in rows:
print(f"ProductID: {row[0]}, ProductName: {row[1]}, Price:
{row[2]:.2f}")
else:
print("No products found.")
conn.close() # Close the connection
# Main function to run the program
def main():
create_table() # Create table if not already created
insert_sample_data() # Add sample products to the table
update_price() # Update prices by 5%
display_products() # Display the updated products
def del_record(name):
db =
mysql.connector.connect( host="local
host",
user="root",
password="root",
database="school"
)
cur = db.cursor()
if rec:
# Print details of the record to be deleted
print("Deleting the following record:")
print(f"ID: {rec[0]}, Name: {rec[1]}, Details: {rec[2:]}")