Computer Report File
Computer Report File
(High School)
CODE:
def is_perfect_number(num):
if num < 1:
return False
divisors_sum = 0
for i in range(1, num):
if num % i == 0:
divisors_sum += i
return divisors_sum == num
number_to_check = int(input("Enter a number to check : "))
if is_perfect_number(number_to_check):
print(number_to_check," is a perfect number.")
else:
print(number_to_check," is not a perfect number.")
OUTPUT
2. Write a Python program that reads each row of a given CSV 1le and
skip the header of the 1le. Also print the number of rows and the 1eld
names. CSV FILE:
department_id,department_name,manager_id,location_id
10,Administration,200,1700
20,Marketing,201,1800
30,Purchasing,114,1700
40,Human Resources,203,2400
50,Shipping,121,1500
CODE:
import csv
1elds = []
rows = []
with open('departments.csv', newline='') as csv1le:
data = csv.reader(csv1le, delimiter=' ', quotechar=',')
# Following command skips the 1rst row of the CSV 1le.
1elds = next(data)
for row in data:
print(', '.join(row))
print("\nTotal no. of rows: %d"%(data.line_num))
print('Field names are:')
print(', '.join(1eld for 1eld in 1elds))
OUTPUT:
3. Write a Python program to access a function inside a function
CODE:
def outer_function():
print("This is the outer function.")
def inner_function():
print("This is the inner function.")
# Call the inner function from the outer function
inner_function()
# Call the outer function
outer_function()
4. Write a Python program to detect the number of local variables.
CODE:
def count_local_variables():
# De1ne some local variables
var1 = 10
var2 = "Hello"
var3 = [1, 2, 3]
# Use locals() to get the local symbol table
local_variables = locals()
CODE:
import string
def letters_1le_line(n):
with open("words1.txt", "w") as f:
alphabet = string.ascii_uppercase
letters = [alphabet[i:i + n] + "\n" for i in range(0, len(alphabet), n)]
f.writelines(letters)
letters_1le_line(3)
NAMES.DAT:
SUVRA SHEKHAR DEY
COMPUTER SCIENCE
CODE:
def RECCOUNT(1le_path):
record_size = 20 # Size of each record in bytes
try:
with open(1le_path, 'rb') as 1le:
1le_content = 1le.read()
num_records = len(1le_content) // record_size
print(f"Number of records in {1le_path}: {num_records}")
except FileNotFoundError:
print(f"File {1le_path} not found.")
except Exception as e:
print(f"An error occurred: {e}")
1le_path = 'NAMES.DAT'
RECCOUNT(1le_path)
OUTPUT:
7. Write a function SCOUNT () to read the content of binary 1le
“NAMES.DAT” and display number of records (each name occupies
20 bytes in 1le) where name begins from “S” in it.
NAMES.DAT:
SUVRA SHEKHAR DEY
COMPUTER SCIENCE
CODE:
def SCOUNT():
try:
with open("NAMES.DAT", "rb") as 1le:
name_size = 20
s_names_count = sum(1 for _ in iter(lambda:
1le.read(name_size), b''))
print(f"Total Names beginning from 'S' are {s_names_count}")
except FileNotFoundError:
print("File not found.")
except Exception as e:
print(f"An error occurred: {e}")
SCOUNT()
OUTPUT:
8. Consider the following CSV 1le (emp.csv):
1, Peter,3500; 2, Scott,4000; 3, Harry,5000; 4, Michael,2500; 5,
Sam,4200
Write Python function DISPEMP () to read the content of 1le emp.csv
and display only those records where salary is 4000 or above.
CODE:
import csv
def DISPEMP():
try:
with open("emp.csv", "r") as 1le:
reader = csv.reader(1le)
header = next(reader)
print(','.join(header))
for row in reader:
if int(row[2]) >= 4000:
print(','.join(row))
except FileNotFoundError:
print("File not found.")
except Exception as e:print(f"An error occurred: {e}") DISPEMP()
OUTPUT:
9. Consider the following CSV 1le (emp.csv):
1,Peter,3500; 2,Scott,4000; 3,Harry,5000; 4,Michael,2500; 5,Sam,4200
Write a Python function SNAMES() to read the content of 1le
emp1.csv and display the employee record whose name begins from “S”.
Also show no. of employee with 1rst letter “S” out of total record.
Output should be: 2,Scott,4000 5,Sam,4200
Number of “S” names are 2,5
CODE:
import csv
def SNAMES():
try:
with open("emp1.csv", "r") as 1le:
reader = csv.reader(1le)
s_names_count = 0
header = next(reader)
print(','.join(header))
for row in reader:
if row[1].startswith('S'):
print(','.join(row))
s_names_count += 1
print(f"Number of 'S' names are {s_names_count}")
except FileNotFoundError:
print("File not found.")
except Exception as e:
print(f"An error occurred: {e}")
SNAMES()
OUTPUT:
DATABSE (SQL)
1.
Ans.
ANO ANAME
101
103
102
Ans.
Ans.
ANO COUNT(*) MIN(AMOUNT)
101 2 2500
103 2 1000
2 5000
2.
SGRADE SALARY+HRA
S02 44000
SUM(SALARY) AVG(SALARY)
112000 37333.33
3.
(i) To display details of all Trains which starts from New Delhi
Ans. SELECT * FROM TRAINS WHERE START='NEW
DELHI'
(ii) To display PNR, PNAME, GENDER and AGE of all
passengers whose AGE is below 50
Ans. SELECT PNR,PNAME,GENDER,AGE FROM PASSENGER
WHERE AGE<50
(iii) To display total numbers of MALE and FEMALE passengers
Ans. SELECT GENDER,COUNT(*) FROM PASSENGERS
GROUP BY GENDER
(iv) To display records of all passengers travelling in trains whose
TNO is 12015
Ans. SELECT * FROM PASSENGERS WHERE TNO=12015
(v) SELECT MAX(TRAVELDATE),MIN(TRAVELDATE)
FROM PASSENGERS WHERE GENDER=‟FEMALE‟;
Ans.
MAX (TRAVELDATE) MIN (TRAVELDATE)
2018-11-10 2018-05-09
HABIBGANJ 2
AMRITSAR JN. 2
NEW DELHI 4
(vii) SELECT DISTINCT TRAVELDATE FROM PASSENGERS;
Ans.