Professional Documents
Culture Documents
PROBLEM STATEMENT
Write a program in python to get 2 numbers N1 and N2 from the user and perform all basic
mathematical operations i.e. +,-,*,/,//,% on these numbers.
Example Output-
Enter N1: 4
Enter N2: 3
4 + 3 = 12
4–3=1
4 * 3 = 12
4 / 3 = 1.3333
4//3 = 1
4%3 = 1
CODE
OUTPUT
<><><><>><><>
ACTIVITY 2
PROBLEM STATEMENT
Write a program in python to print mathematical table of number N. Display T terms of table where
N and T is entered by user.
Example output—
Enter N : 4
Enter T : 5
5 Terms of Table of 4—
4X1=4
4X2=8
4 X 3 = 12
4 X 4 = 16
CODE
OUTPUT
<><><><>><><>
ACTIVITY 3
PROBLEM STATEMENT
Write a program in python to get a string from user and print the statistic as below-
Total No. of characters in String –
Number of letters –
Number of Uppercase letters –
Number of lowercase letters –
Number of Digits—
Number of Spaces--
Number of Special Characters—
CODE
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
ACTIVITY 4
PROBLEM STATEMENT
Write a program in python to get a list of numbers L from user and do the following on the list-
1. Display all elements of list on screen separated by #.
2. Display 2nd last element of list on screen
3. Add 10 to first element of list
4. Delete 2nd element of list
5. Sort the list in decreasing order and display it.
CODE
OUTPUT
<><><><>><><>
ACTIVITY 5
PROBLEM STATEMENT
Write a function FACT(N) to find factorial of a number N. Now Use this function to calculate factorial
of any number entered by user.
CODE
def FACT(N):
f = 1
for i in range(N,1,-1):
f = f*i
print("Factorial of ",N," is :",f)
OUTPUT
<><><><>><><>
ACTIVITY 6
PROBLEM STATEMENT
Princ = 100
Time = 2
Rate = 12
print("Using Positional Function Arguments")
SimpleInterest(Princ,Time,Rate)
print("Using Default Function Arguments")
SimpleInterest(Princ,Time)
print("Using Keyword Function Arguments")
SimpleInterest(R = Rate, P = 1000,T = 3)
OUTPUT
<><><><>><><>
ACTIVITY 7
PROBLEM STATEMENT
Write a program to open a file student.txt in different file modes. Check whether
file is opened or not in each case.
Hint: use <FileHandle>.closed to check if file is open or closed.
CODE
<><><><>><><>
ACTIVITY 8
PROBLEM STATEMENT
Write a Program to read any text file and display its content as each word
separated by “ # “ symbol.
CODE
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
ACTIVITY 9
PROBLEM STATEMENT
Write a Program to read the content of file and display the following-
Total number of characters in file:
Total number of digits in file:
Total number of vowels in file:
Total number of consonants in file:
Total number of uppercase letters:
Total number of lower case letters:
CODE
f = open("file1.txt")
v=0
c=0
u=0
l=0
d=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch.isdigit():
d=d+1
elif ch!=' ' and ch!='\n':
o+=1
print("Total number of characters in file: ",len(data))
print("Total number of digits in file: ",d)
print("Total Vowels in file :",v)
print("Total Consonants in file :",c)
print("Total number of uppercase letters:",u)
print("Total number of lower case letters:",l)
f.close()
OUTPUT
<><><><>><><>
PROGRAM 10
PROBLEM STATEMENT
Write Program to read the content of file line by line and write its content to
another file except for the lines that contains “*‟ character in it.
CODE
F1 = open("source.txt")
F2 = open("target.txt","w")
#Reading from the source file line by line
for Line in F1:
if '*' not in Line:
#Writing into target file
F2.write(Line)
F1.close()
F2.close()
OUTPUT
<><><><>><><>
PROGRAM 11
PROBLEM STATEMENT
Write a program to Read a text file and count the number of times words ‘He’
and ‘She’ found in the file. Ignore the capital casing of letters (i.e. also count he,
she, HE, SHE etc.).
CODE
OUTPUT
<><><><>><><>
PROGRAM 12
PROBLEM STATEMENT
Write a program to create a CSV file MARKS.csv after getting RollNo. , Name and
Marks of 10 students.
Now read this CSV file and print RollNo. and Name of those student who score
more than 90 marks.
CODE
<><><><>><><>
PROGRAM 13
PROBLEM STATEMENT
Write a Program to create a binary file to store Rollno and Name of students.
Then Search any Rollno and display name if Rollno is found, otherwise display -
“Rollno not found”
CODE
F=open('student.dat','rb')
r = int(input("Enter Roll number to search :"))
while True:
try:
data = pickle.load(F)
if data[0]==r:
print("Record Found, Name is :",data[1])
break
except EOFError:
print("NOT FOUND")
break
F.close()
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 14
PROBLEM STATEMENT
Program to create binary file to store Rollno,Name and Marks and update marks of
entered Rollno.
CODE
#Program to create a binary file to store
#Rollno and name and then update it
import pickle
F=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
data = [roll,name,marks]
pickle.dump(data,F)
ans=input("Add More ?(Y)")
F.close()
F=open('student.dat','rb+')
r = int(input("Enter Roll number to update :"))
n = int(input("Enter new Marks :"))
while True:
try:
POS = F.tell()
data = pickle.load(F)
if data[0]==r:
data[2] = n
F.seek(POS,0)
pickle.dump(data,F)
print("Record Updated")
break
except EOFError:
print("NOT FOUND")
break
F.close()
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 15
PROBLEM STATEMENT
CODE
def Pop(stk):
if stk==[]:
print("Stack Underflow")
else:
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
print("Popped item is "+str(item))
def Display(stk):
if stk==[]:
print("Stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print (stk[i])
# Main
STACK=[]
while True:
print("STACK OPERATIONS")
print("1 : PUSH")
print("2 : POP")
print("3 : DISPLAY")
print("0 : EXIT")
op = input("Enter Your Choice: ")
if op == '0':
break
elif op == '1':
ITEM = input("Enter Item: ")
Push(STACK,ITEM)
elif op == '2':
Pop(STACK)
elif op == '3':
Display(STACK)
else:
print("Invalid Choice, Try Again! ")
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 16
PROBLEM STATEMENT
(iv) Change data type of STUDENT.NAME column to Text of size 200 characters
(v) Add a new columns in table STUDENT as per below detail
Column Name Data Type
SECTION 1 Character
Fee Decimal
(vi) Display the structure of table on screen.
(vii) Delete column SECTION from STUDENT table
CODE
(i) CREATE DATABASE KVK_2022;
USE KVK_2022;
(ii) CREATE TABLE DEPARTMENT (
DEPT_ID VARCHAR(10) PRIMARY KEY,
DEPT_NAME VARCHAR(100) NOT NULL);
(iii) CREATE TABLE STUDENT (
UID INT PRIMARY KEY,
NAME VARCHAR(100) NOT NULL,
CLASS INT CHECK(CLASS >=1 OR CLASS <=12),
GENDER CHAR(1),
DOB DATE,
ADHARID INT UNIQUE,
DEPT_ID VARCHAR(10) REFERENCES DEPARTMENT(DEPT_ID) );
(iv) ALTER TABLE STUDENT MODIFY NAME VARCHAR(200);
(v) ALTER TABLE STUDENT ADD ( SECTION CHAR(1), FEE DECIMAL);
(vi) DESC STUDENT;
(vii) ALTER TABLE STUDENT DROP SECTION;
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 17
PROBLEM STATEMENT
CODE
(i) INSERT INTO DEPARTMENT VALUES ('HUM','Humanities');
INSERT INTO DEPARTMENT VALUES ('SCI','Science');
INSERT INTO DEPARTMENT VALUES ('COM','Commerce');
INSERT INTO STUDENT VALUES(101 ,'Joya' ,12 ,'F' ,'20050112'
,1201 ,'HUM' ,1000);
INSERT INTO STUDENT VALUES(102 ,'Suresh' ,12 ,'M' ,'20060809'
,1202 ,'SCI' ,3000);
INSERT INTO STUDENT VALUES(103 ,'Mahesh' ,11 ,'M' ,'20070909'
,1203 ,'COM' ,2000);
INSERT INTO STUDENT VALUES(104 ,'Aman' ,12 ,'M' ,'20061019'
, NULL ,'SCI' ,3000);
INSERT INTO STUDENT VALUES(105 ,'Priyanka' ,12 ,'F'
,'20050115' , NULL ,'COM' ,2000);
INSERT INTO STUDENT VALUES(106 ,'Sheena' ,11 ,'F' ,'20060529'
,1206 ,'HUM' ,1000);
INSERT INTO STUDENT VALUES(107 ,'Pankaj' ,11 ,'M' ,'20070924'
,1207 ,'SCI' ,3000);
INSERT INTO STUDENT VALUES(108 ,'Tarun' ,11 ,'M' ,'20070417'
, NULL ,'COM' ,2000);
(ii) UPDATE STUDENT SET ADHARID = 1204 WHERE UID = 104;
(iii) UPDATE STUDENT SET FEE = FEE + 500 WHERE DEPT_ID = ‘HUM’
(iv) DELETE FROM STUDENT WHERE UID = 108;
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 18
PROBLEM STATEMENT
CODE
(i) SELECT * FROM STUDENT;
(ii) SELECT UID, NAME FROM STUDENT;
(iii) SELECT NAME, CLASS FROM STUDENT WHERE GENDER = 'F';
(iv) SELECT FEE + FEE*0.20 AS 'NEW FEE' FROM STUDENT WHERE DEPT_ID =
'SCI';
(v) SELECT * FROM STUDENT WHERE GENDER = 'M' AND CLASS = '12';
(vi) SELECT UID,NAME, ADHARID FROM STUDENT WHERE UID BETWEEN 102 AND
106;
OR
SELECT UID,NAME, ADHARID FROM STUDENT WHERE UID >= 102 AND UID <=
106;
(vii) SELECT UID,NAME, ADHARID FROM STUDENT WHERE UID BETWEEN 102 AND
106;
(viii) SELECT * FROM STUDENT WHERE CLASS = 11 AND NAME LIKE '_a%';
(ix) SELECT * FROM STUDENT WHERE ADHARID IS NULL;
(x) SELECT * FROM STUDENT WHERE ADHARID IS NOT NULL;
(xi) SELECT * FROM STUDENT ORDER BY UID DESC;
(xii) SELECT DISTINCT DEPT_ID FROM STUDENT;
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 19
PROBLEM STATEMENT
CODE
(i) SELECT SUM(FEE) FROM STUDENT;
(ii) SELECT AVG(FEE) FROM STUDENT WHERE GENDER='F';
(iii) SELECT COUNT(*) FROM STUDENT WHERE GENDER='M';
(iv) SELECT COUNT(*) FROM STUDENT WHERE GENDER='F' AND ADHARID IS
NULL;
(v) SELECT MAX(DOB), MIN(DOB) FROM STUDENT;
(vi) SELECT MAX(FEE), MIN(FEE) FROM STUDENT WHERE GENDER='F';
(vii) SELECT DEPT_ID, COUNT(*) FROM STUDENT GROUP BY DEPT_ID;
(viii) SELECT GENDER, SUM(FEE) FROM STUDENT GROUP BY GENDER;
(ix) SELECT GENDER, MAX(FEE),MIN(FEE) FROM STUDENT GROUP BY GENDER;
(x) SELECT DEPT_ID, COUNT(*) FROM STUDENT GROUP BY DEPT_ID HAVING
COUNT(*) >2;
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 20
PROBLEM STATEMENT
CODE
(i) SELECT S.NAME, D.DEPT_NAME FROM STUDENT S, DEPARTMENT D WHERE
S.DEPT_ID = D.DEPT_ID;
(ii) SELECT S.UID, S.NAME, S.FEE, D.DEPT_NAME FROM STUDENT S,
DEPARTMENT D WHERE S.DEPT_ID = D.DEPT_ID AND S.GENDER='F';
(iii) SELECT S.UID, S.NAME, D.DEPT_NAME FROM STUDENT S, DEPARTMENT D
WHERE S.DEPT_ID = D.DEPT_ID AND D.DEPT_NAME LIKE '%e';
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 21
PROBLEM STATEMENT
CODE
import mysql.connector as sql
try:
CON = sql.connect(host="localhost", user="root",passwd="mysql",
database="KVK_2022")
if CON.is_connected():
print("Connected")
else:
print("Could not Connect")
except:
print("Could not Connect")
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 22
PROBLEM STATEMENT
Write a Python program to get details of 5 students and insert it into Student table created in Activity 16.
Structure of table is –
CODE
import mysql.connector as sql
CON = sql.connect(host="localhost", user="root",passwd="mysql",
database="KVK_2022")
CUR=CON.cursor()
for i in range(5):
print("Getting Details of Student ",i+1)
UID = int(input("Enter UID: "))
NAME = input("Enter Name: ")
CLASS = input("Enter Class: ")
GENDER = input("Enter Gender: ")
DOB = input("Enter DOB(YYYYMMDD): ")
ADHAR = int(input("Enter Adhar ID: "))
DEPT= input("Enter Dept ID: ")
FEE = float(input("Enter Fee: "))
QUERY = f'''INSERT INTO STUDENT VALUES (
{UID}, '{NAME}', '{CLASS}','{GENDER}','{DOB}',{ADHAR},
'{DEPT}', {FEE})'''
CUR.execute(QUERY)
CUR.execute("COMMIT")
print("Record Inserted")
CON.close()
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 23
PROBLEM STATEMENT
PWrite a Python program to update the fee of any student based upon the UID entered
by user.
CODE
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>
PROGRAM 24
PROBLEM STATEMENT
Write a Python program to display the details of student whose name is entered by
user.
CODE
import mysql.connector as sql
CON = sql.connect(host="localhost", user="root",passwd="mysql",
database="KVK_2022")
CUR=CON.cursor()
N = input("Enter Name :")
QUERY = f"SELECT * FROM STUDENT WHERE NAME LIKE '%{N}%'"
CUR.execute(QUERY)
DATA = CUR.fetchall()
for I in DATA:
print(I)
CON.close()
OUTPUT
<To be written or printed and pasted after execution by the student>
<><><><>><><>