INDEX
S.NO PROGRMAS SIGN
1. Write A Program That Reads A Line And Print Statistics
2. List: The Extended Metho
3. Tuples: Slicling
4. Dictionary Functions
5. Write A Program To Calculate Simple Interest Using A
Function
6. Write A Program Implement Stack Operation
7. Function That Takes A Positive Integer And Returns The
Ones Position Of It.
8. Function That Recieves Two Numbera and Perform All
Arithemetic Operations
9. Write A Program To Read First three lines Of File
Poem.Txt
10. Write A Program To display The Size Of A File In Bytes
11. Write A Program To print Cubes Of A File In Bytes.
12. Write A Program That Finds An Elements Index/Position
In A Tuples.
13. Write A Program to create a dictionary containing names
of competition winners as key and numbers of wins as
values.
14. Write a function to count number of lines in a text file
STORY.TXT which is starting with letter A.
15. Write a program to add two more students details to the
file MARK DAT.
16. Write a program to get student data from user and write
onto a binary file
17. Write SQL statement based on some parameters.
18. INSERT QUERY
19. UPDATE QUERY
20. Write a program for a file stud.DAT that read contents
where event 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 :-
Fig : Output Question 3
Question No. 4 :-
# Write to use dictionary functions.
Employee={"Name": "JAY", "Age": 21, "salary":30000,"Company":"INFOSYS"}
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 the 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)
print("Item Added Succesfully ")
elif ch==6:
remove(stack)
elif ch==7:
print()
print("Thank You")
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("The Once Digit Is : ",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(“E:\poem.txt”,”r”)
for I in range(0,4):
str=myfile.readline()
print(str,end=” “)
myfile.close()
OUTPUT :-
Fig : Original Text File
Fig : 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 :-
Fig : Output
Fig : Original File
Question No.11 :-
# Write A Program To print Cubes Of A File In Bytes.
for i in range(12,18):
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 = ('g','e','t','o','f','f','l','i','f','e')
searchval=input("Enter single letter without quotes: ")
try:
index = val.index(searchval) # Finds index
if(index>0):
print("Index of “,searchval, “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 :-
Fig : Output
Question No.15 :-
# Write a program to add two more students details to the file MARK
DAT.
fileout=open("E:\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 :-
Fig : 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 jay;
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, “Aman’’, 550, "Computers”, 67,1);
Query OK, 1 row affected (0.12 sec)
mysql> insert into graduate values (3, "Umran", 500, "Maths", 65,1);
Query OK, 1 row affected (0.06 sec)
mysql> insert into graduate values (4, "Naman", 520, "Chemistry", 69,1);
Query OK. 1 row affected (0.09 sec)
OUTPUT :-
Question No.19 :-
# Update Query.
mysql> Update graduate set Name="Sarthak" where Name = "Aman";
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, 1 rows affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> Update graduate set Average= Null where Average = 69;
Query OK, 1 row affected (0.36 sec)
Rows matched: 1 Changed: 1 Warnings:
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')
ath(f1,f2)
f1.close()
f2.close()
OUTPUT :-