S.
No Title Signature
1. WAP TO WRITE A LINE AND PRINT ITS STATISTICS
2. LIST: THE EXTENDED METHOD
3. TUPLE: SLICING THE TUPLE
4. WAP TO USE DICTIONARY FUNCTIONS
5. WAP TO CALCULATE SIMPLE INTEREST USING A FUNCTION
6. WAP TO IMPLEMENT STACK OPERATIONS
7. FUNCTION THAT TAKES A POSITIVE INTEGER AND RETURNS THE
ONE’S POSITION DIGIT OF THE INTEGER
8. WAP THAT RECEIVES TWO NUMBERS IN A FUNCTION AND RETURNS
ALL ARITHMETIC OPERATIONS
9.
WAP TO READ THE FIRST THREE LINES OF THE FILE POEM.TXT
WAP TO DISPLAY THE SIZE OF A FILE IN BYTES.
10.
WAP TO PRINT THE CUBES OF NUMBERS IN THE RANGE 15 TO 20.
11.
12. WAP THAT FINDS AN ELEMENT’S INDEX/POSITION IN A TUPLE
WITHOUT USING INDEX().
WAP TO CREATE A DICTIONARY CONTAINING NAMES OF
13. COMPETITION WINNER STUDENTS AS KEY AND NUMBER OF THEIR
WINS AS VALUE.
14. WAP TO COUNT NO. OF LINES IN A TEXT FILE STORY.TXT WHICH IS
STARTING WITH LETTER A
WAP TO ADD TWO MORE STUDENTS DETAILS TO THE FILE MARK.DAT
15.
WAP TO GET STUDENT DATA FROM USER AND WRITE ONTO A
16. BINARY FILE.
WRITE SQL STATEMENTS BASED ON SOME PARAMETER.1234
17.
INSERT QUERY
18.
UPDATE QUERY
19.
WAP FOR A FILE SPORT.DAT THAT READ CONTENTS WHERE EVENT
20. NAME IS ATHLETICS.
QUESTION NO.1 :-
WRITE A PROGRAM THAT READS A LINE AND
PRINT STATISTICS.
Line = input(“Enter a string/line/sentence: “)
lowercharcount = uppercharcount = 0
alphacount = digitcount = 0
for a in line:
if a.islower():
lowercharcount += 1
elif a.isupper():
uppercharcount += 1
elif a.isdigit():
digitcount += 1
if a.isalpha():
alphacount += 1
print(“Number of Lowercase letters :”,lowercharcount)
print(“Number of Uppercase letters :”,uppercharcount)
print(“Number of Digits :”, digitcount)
print(“Number of Alphabets :”, alphacount)
OUTPUT:-
Question No.2 :-
List: the extended method
language1=['French']
language2=['Spanish','Protuguese']
language3=["Chinese","Japanese"]
language1.extend(language2)
language1.extend(language3)
print()
print("NEW LANGUAGE LIST:- ",language1)
OUTPUT:-
Question No.3 :-
Tuple: Slicing the tuple.
tuplex=(2,4,3,5,4,6,7,8,6,1)
slicex=tuplex[3:5]
print(slicex)
slicex=tuplex[:6]
print(slicex)
slicex=tuplex[5:]
print(slicex)
slicex=tuplex[:]
print(slicex)
slicex=tuplex[-8:-4]
print(slicex)
tuplex=tuple("COMPUTER SCIENCE WITH PYTHON")
print(tuplex)
slicex=tuplex[2:9:2]
print(slicex)
slicex=tuplex[::4]
print(slicex)
slicex=tuplex[9:2:-4]
print(slicex)
OUTPUT:-
Question no. 4
Write to use dictionary functions;
Employee={"Name": "Manya", "Age": 20, “salary”:20000,
“Company” :”MICROSOFT”}
print()
print(type(Employee))
print("printing Employee data .......")
print("Name : %s" %Employee["Name"])
print("Age : %d" %Employee["Age"])
print("Salary : %d" %Employee["salary"])
print("Company : %s" %Employee["Company"])
OUTPUT:-
Question no. 5 :-
Write to calculate simple interest using a function.
def interest(principal,time,rate):
return principal* rate *time
prin=float(input("Enter principal amount: "))
roi=float(input("Enter rate of interest(ROI): "))
time=int(input("Enter time in years: "))
print()
print("Simple Interest:- ")
si=interest((prin),time,roi/100)
print("Rs.",si)
OUTPUT:-
Question No.6 :-
Python program to implement a stack
def isEmpty(stk):
if stk == []:
return True
else:
return False
def push(stk,item):
stk.append(item)
top=len(stk)-1
def pop(stk):
if isEmpty(stk):
return"Underflow"
else:
item=stk.pop()
if len(stk)==0:
top= None
else:
top=len(stk)-1
return item
def peek(stk):
if isEmpty(stk):
return"Underflow"
else:
top=len(stk)-1
return stk[top]
def display(stk):
if isEmpty(stk):
print("Stack empty ")
else :
top = len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
def add(stk,item):
stk.append(item)
top = len(stk)-1
def remove(stk):
if(stk==[]):
print("Stack empty;Underflow")
else:
print("Deleted student is :",stk.pop())
stack=[]
top = None
while True:
print("STACK OPERATION:")
print("1.PUSH")
print("2.POP")
print("3.PEEK")
print("4.DISPLAY STACK")
print("5.ADD")
print("6.REMOVE")
print("7.EXIT")
ch = int(input("Enter your choice(1-7):"))
if ch==1:
item=int(input("Enter item: "))
push(stack,item)
elif ch==2:
item=pop(stack)
if item=="Underflow":
print("Underflow! stack is empty! ")
else:
print("Popped item is",item)
elif ch==3:
item=peek(stack)
if item=="Underflow":
print("Underflow! stack is empty! ")
else:
print("Topmost item is ",item)
elif ch==4:
display(stack)
elif ch==5:
rno = int(input("Enter Roll no to be inserted :"))
sname = input("Enter Student name to be inserted :")
item = [rno,sname]
add(stack,item)
elif ch==6:
remove(stack)
elif ch==7:
break
else:
print("Invalid choice ")
OUTPUT:-
Question no. 7 :-
Function That Takes A Positive Integer And Returns
The Ones Position Of It.
def Ones(num):
Ones_dight=num%10
print(Ones_dight)
return Ones_dight
A=int(input("Enter The No. "))
Ones(A)
OUTPUT:-
Question No.8 :-
Write a Function That Recieves Two Number and
Perform All Arthematic Operations.
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
min = float(num1) - float(num2)
mul = float(num1) * float(num2)
div = float(num1) / float(num2)
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
print('The subtraction of {0} and {1} is {2}'.format(num1, num2,
min)) print('The multiplication of {0} and {1} is
{2}'.format(num1, num2, mul))
print('The division of {0} and {1} is {2}'.format(num1, num2,
div))
OUTPUT :
Question No.9 :-
Write A Program To Read First three lines Of File
Poem.Txt.
myfile=open("C:\Programs\poem.txt","r")
for i in range(0,4):
str=myfile.readline()
print(str,end=" ")
myfile.close()
OUTPUT:-
Question No.10 :-
Write A Program To display The Size Of A File In
Bytes.
myfile=open("C:\Programs\poem.txt","r")
str=myfile.read()
size=len(str)
print("Size of the given file is ")
print(size,"bytes")
OUTPUT:-
Question No.11 :-
Write A Program To print Cubes Of A File In Bytes.
for i in range(15,21):
print("Cube of number",i,end=" ")
print("is",i**3)
OUTPUT:-
Question No.12 :-
Write A Program That Finds An Elements
Index/Position In A tuples.
val = ('a','b','r','a','k','a','d','h','a')
searchval=input("Enter single letter without quotes:")
try:
index = val.index(searchval) # Finds index
if(index>0):
print("Index of k is: ",index)
except:
print(searchval,"is not present")
print("the tuple is",val)
OUTPUT:-
Question No.13:-
Write A Program to create a dictionary containing
names of competition winners as key and number
of their wins as value.
n=int(input("How many students? "))
compwinners={}
for a in range(n):
key=input("name of the student: ")
value= int(input("Number of competitions won: "))
compwinners[key]=value
print("The dictionary now is: ")
print(compwinners)
OUTPUT:-
QUESTION NO.14 :-
WRITE A FUNCTION TO COUNT NO OF LINES IN A
TEXT FILE POEM.TXT WHICH IS STARTING WITH
LETTER C.
def countlines():
file=open("C:\Programs\poem.txt","r")
lines=file.readlines()
count=0
for w in lines:
if w[0]=="A":
count=count+1
print("Total lines starting with A or a",count)
file.close()
countlines()
OUTPUT:-
Question No.15 :-
Question no.15
Write a program to add two more students details
to the file MARK DAT.
fileout=open("C:\Programs\Marks.dat","a")
for i in range(2):
print("Enter details for student",(i+1),"below")
rollno=int(input("roll no: "))
name=input("Name:")
marks=float(input("Marks: "))
rec=str(rollno)+","+name+","+str(marks)+"\n"
fileout.write(rec)
fileout.close()
OUTPUT:-
Question no.16 :-
WAP TO GET STUDENT DATA FROM USER AND
WRITE ONTO A BINARY FILE.
import pickle
stu={}
stufile=open('C:\Programs\stu.dat','wb')
ans='y'
while ans=='y':
rno=int(input("enter roll number : "))
name=input("enter name :")
marks=float(input("enter marks :"))
stu['Rollno']=rno
stu['Name']=name
stu['Marks']=marks
pickle.dump(stu,stufile)
ans=input("Want to enter more records?? (y/n)....")
stufile.close()
OUTPUT:-
Question no.17
WRITE SQL STATEMENTS BASED ON SOME
PARAMETERS
mysql> use mysql;
Database changed
mysql>create table graduate(Sno int, Name char(20), Stipend
int, Subject char(15), Average int, Division int);
Query OK,0 rows affected(2.40sec)
OUTPUT:-
Question No.18 :-
Insert Query
mysql> insert into graduate values (1, "Mrunal",
400,"Physics",68,1)
Query OK, I row affected (0.17 sec)
mysql> insert into graduate values (2, “Bhuvnash’’, 450,
"Computer SC”, 68,1)
Query OK, 1 row affected (0.12 sec)
mysql>insert into graduate values (3, "Tuhina", 300, "Chemistry",
62,2):
Query OK, 1 row affected (0.06 sec)
mysql>insert into graduate values (4, "Manasvi", 350, "Physics",
63,1);
Query OK. 1 row affected (0.09 sec)
mysql> insert into graduate values (5, "Sahiba", 500.
"Mathematics",70,1):
Query OK, 1 row affected (0.17 sec)
mysql>insert into graduate values (6, "John", 400, "Chemistry",
55,2);
Query OK, 1 row affected (0.08 sec)
mysql>insert into graduate values (7, "Nimash", 250,"Physics", 64,
1);
Query OK, 1 row affected (0.09 sec)
mysql>insert into graduate values (8, "Rubina",
450,"Mathematics", 68, 1);
Query OK, 1 row affected (0.12 sec)
OUTPUT:-
Question No.19 :-
Update Query.
mysql> Update graduate set Sno = 11 where Sno = 9;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> Update graduate set Name="Kavya" where Name =
"John";
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>Update graduate set Subject = "English" where
Subject=”Chemistry”;
Query OK, 2 rows affected (0.09 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> Update graduate set Average= Null where Average = 70;
Query OK, 1 row affected (0.36 sec)
Rows matched: 1 Changed: 1 Warnings: 0
OUTPUT:-
Question No.20 :-
WRITE A PROGRAM FOR A FILE SPORT DAT THAT
READ CONTENTS WHERE EVENT NAME IS
ATHELETICS
def ath():
f1=open("C:\Programs\stud.dat","r")
f2=open("C:\Programs\athletics.dat","w")
l=f1.readlines()
for line in l:
if line.startswith("Athletics"):
f2.write(line)
f2.write('\n')
f1.close()
f2.close()
ath()
OUTPUT:-