Practical File
Practical File
def recur_factorial(n):
if n == 1 or n == 0: # Include 0! = 1
return 1
else:
return n * recur_factorial(n - 1)
Output:
Enter a number: 4
Factorial of 4 is 24
def recur_fibo(n):
if n <= 1:
return n
else:
return recur_fibo(n - 1) + recur_fibo(n - 2)
Output:
How many terms required? 3
Fibonacci sequence generated is:
0
1
1
Output:
Enter any string: deepak
Enter the character to count: e
e exists 2 times in the string
def list_avg(lst):
total = sum(lst)
return total / len(lst)
Output:
Input integers:
456782
Average is: 5.33
Q5: Write a function to find the maximum of three numbers using recursion.
Output:
10
Q6: Write a program that accepts a string and calculates the number of uppercase and
lowercase letters.
def string_test(s):
d = {"upper_case": 0, "lower_case": 0}
for c in s:
if c.isupper():
d["upper_case"] += 1
elif c.islower():
d["lower_case"] += 1
print('Original string:', s)
print('Number of uppercase characters:', d["upper_case"])
print('Number of lowercase characters:', d["lower_case"])
Output:
Original string: Play learn and grow
Number of uppercase characters: 1
Number of lowercase characters: 15
Q7: WAP to display details of employees earning between 20000 and 40000 from a binary
file "Employee.dat".
def Readfile():
with open("Employee.dat", "rb") as f:
line = f.readline()
while line:
data = line.split(':')
salary = float(data[2])
if 20000 <= salary <= 40000:
print(line)
line = f.readline()
Readfile()
Note: Ensure that 'Employee.dat' is structured correctly and opened in binary mode for proper
reading.
Q8: Write a function to count the occurrences of "my" in a text file "DATA.TXT".
def countmy():
with open("DATA.TXT", "r") as f: # open in text mode
content = f.read()
count = content.split().count("my")
print(f"'my' occurs {count} times.")
countmy()
Output:
'my' occurs 5 times.
Q9: Program using dictionary and text files to store Roman numerals and their
equivalents.
import pickle
n=0
while n != -1:
print("Enter 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000")
print("Enter -1 to exit")
n = int(input("Enter a numeral: "))
if n != -1 and n in roman_data:
print(f'Equivalent Roman numeral is: {roman_data[n]}')
else:
print("Thank you")
Q10: Program to search the record of a particular student by name in a CSV file.
import csv
with open("student.csv", "r") as f:
csv_reader = csv.reader(f)
name = input("Enter the name to search: ")
for row in csv_reader:
if row[0] == name:
print(row)
Output:
Enter the name to search: jatin
['jatin', 'XII', '70']
Q11: Program to count the exact number of records present in a CSV file (excluding the
header).
import csv
Output:
No. of records are: 10
Q12: Program to read byte by byte from a file using seek() and tell().
Q13: Program to update the name of a student by roll number in a binary file
'student.dat'.
import pickle
Output:
Enter the roll number to search: 2
Current name is: Radhika
New name: Sonia
Name updated!!!
import pickle
Output:
Contents of student file are:
2 Radhika 490
3 Shaurya 450
def count_vowels(s):
count = 0
vowels = "aeiouAEIOU"
for ch in s:
if ch in vowels:
count += 1
return count
Output:
Enter any string: function provides code reusability
Total number of vowels present in the string are: 13
Mysql
Create database student;
Use student;
dob DATE,
);
+---------+--------+------------+------+
+---------+--------+------------+------+
+---------+--------+------------+------+
+---------+--------------+------+-----+---------+-------+
+---------+--------------+------+-----+---------+-------+
+---------+--------------+------+-----+---------+-------+
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
cursor.execute(query, (student_name,))
result = cursor.fetchall()
if result:
print(row)
else:
cursor.close()
connection.close()
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
for i in result:
print(i)
cursor.close()
connection.close()
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
#Add a column
cursor.execute(query)
cursor.close()
connection.close()
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
#Add a column
cursor.close()
connection.close()
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
#Add a column
cursor.execute(query)
cursor.close()
connection.close()
import mysql.connector
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="student"
cursor = connection.cursor()
#Add a column
cursor.execute(query)
cursor.close()
connection.close()