Index
Index
Index
3. Tuples: Slicling
4. Dictionary Functions
language1=['French']
language2=['Spanish','Protuguese']
language3=["Chinese","Japanese"]
language1.extend(language2)
language1.extend(language3)
print()
print("NEW LANGUAGE LIST:- ",language1)
OUTPUT :-
Question No.3 :-
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)
slicex=tuplex[::4]
print(slicex)
slicex=tuplex[9:2:-4]
print(slicex)
OUTPUT :-
OUTPUT :-
Question no. 5 :-
def interest(principal,time,rate):
return principal* rate *time
OUTPUT :-
Question No.6 :-
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")
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 :-
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 :-
OUTPUT :-
Question No.9 :-
myfile=open(“E:\poem.txt”,”r”)
for I in range(0,4):
str=myfile.readline()
print(str,end=” “)
myfile.close()
OUTPUT :-
Fig : Output
Question No.10 :-
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
for i in range(12,18):
print("Cube of number",i,end=" ")
print("is",i**3)
OUTPUT :-
Question No.12 :-
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:-
for a in range(n):
key=input("name of the student: ")
value= int(input("Number of competitions won: "))
compwinners[key]=value
OUTPUT :-
QUESTION NO.14 :-
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 :-
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 :-
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 (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.
OUTPUT :-
Question No.20 :-
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 :-