PROGRAM : 1
WRITE A FUNCTION PERFECT() WHICH WILL TAKE A NUMBER AS ITS PARAMETER AND CHECK
WHETHER NUMBER IS PERFECT OR NOT.
'''A number is a perfect number if is equal to sum of its proper divisors, sum of its
positive divisors excluding the number itself.'''
SOURCE CODE
OUTPUT
6 is perfect
15 not perfect
35
PROGRAM: 2
DESIGN A FUNCTION COMPOUNDINTEREST() WHICH RECEIVES AMOUNT,TIME AND RATE AS
PARAMETERS AND CALCULATES COMPOUND INTEREST.SET DEFAULT VALUE
0.10 FOR RATE AND 2 FOR TIME.
SOURCECODE
#compound interest (amt*(1+rate/100)^time)-amt
35
OUTPUT
35
PROGRAM : 3
WRITE A FUNCTION STATISTICS(SEN) WHICH RECEIVES A SENTENCE AS A PARAMETER
AND PRINTS THE STATISTICS LIKE NO OF UPPER,NO OF LOWER, NO OF DIGITS PRESENT IN
SENTENCE.
SOURCE CODE
OUTPUT
35
PROGRAM :4
CREATE A TEXT FILE AND WRITE FEW LINES.NOW READ THE FILE CONTENTS LINE
BY LINE AND DISPLAY EACH WORD SEPARATED BY #.
SOURCE CODE
35
OUTPUT
35
PROGRAM :5
PROGRAM TO READ CONTENT OF FILE AND DISPLAY TOTAL NUMBER OF VOWELS, CONSONANTS,
LOWERCASE AND UPPERCASE CHARACTERS
SOURCE CODE
35
35
Output
35
PROGRAM :6
PROGRAM TO READ LINE FROM FILE AND WRITE IT TO ANOTHER LINE
EXCEPT FOR THOSE LINE WHICH CONTAINS LETTER 'a'
SOURCE CODE
def createfile():
fout=open("file2.txt","w")
lst=[]
for i in range(6):
line=input("enter sentence")
lst.append(line+'\n')
fout.writelines(lst)
fout.close()
createfile()
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print("## File Copied Successfully! ##")
f1.close()
f2.close()
35
print("present contents of file2copy.txt")
f2=open("file2copy.txt")
print(f2.read())
OUTPUT
Content of file2.txt
a quick brown fox
onetwo three four
five six seven
India is my country
eight nine ten
bye!
## File Copied Successfully! ##
present contents of file2copy.txt one
two three four
five six seven
eight nine ten
bye!
====================================================
35
PROGRAM :7
PROGRAM TO READ CONTENTS OF FILE AND PRINT ONLY FOUR LETTER WORDS PRESENT IN THE FILE
SOURCE CODE
def createfile():
fout=open("file2.txt","w")
lst=[]
for i in range(2):
line=input("enter sentence")
lst.append(line+'\n')
fout.writelines(lst)
fout.close()
createfile()
def wordslen4():
fin=open("file2.txt",'r')
str=fin.read()
wordlist=str.split(' ')
print("4 letter words:")
for w in wordlist:
if len(w)==4:
print(w)
wordslen4()
35
OUTPUT
enter sentence cbse changed class xii syllabus
enter sentence this year on words its is term
wise 4letter words:
cbse
year
term
================================================================
35
PROGRAM :8
PROGRAM TO READ CONTENTS OF FILE AND DISPLAY THOSE LINES STARTS WITH LOWERCASE
CONSONANT AND ENDS WITH A LOWERCASE VOWEL.
SOURCE CODE
def createfile():
fout=open("file2.txt","w")
lst=[]
for i in range(2):
line=input("enter sentence")
lst.append(line+'\n')
fout.writelines(lst)
fout.close()
createfile()
def wordslen4():
fin=open("file2.txt",'r')
lst=fin.readlines()
print("LINES STARTING WITH LOWERCASE CONSONANT ANDS ENDS WITH LOWERCASE
VOWEL")
for l in lst:
if l[0] not in 'aeiou':
if l[-2] in 'aeiou':
print(l)
wordslen4()
35
OUTPUT
enter sentence cbse has changed its exam pattern
enter sentence this year onwards exam will be conducted term wise
LINES STARTING WITH LOWERCASE CONSONANT ANDS ENDS WITH LOWERCASE VOWEL
this year onwards exam will be conducted term wise
=============================================================
35
PROGRAM :9
PROGRAM TO CREATE A BINARY FILE TO STORE ROLLNO AND NAMESEARCH FOR ROLLNO AND
DISPLAY RECORD IF FOUND OTHERWISE "ROLL NO. NOT FOUND
SOURCE CODE
35
35
OUTPUT
35
PROGRAM :10
PROGRAM TO CREATE A BINARY FILE TO STORE ROLLNO, NAME AND MARKS SEARCH FOR ROLLNO
AND UPDATE MARKS IF FOUND OTHERWISE "ROLL NO. NOT FOUND
SOURCE CODE
35
35
Output
35
PROGRAM :11
PROGRAM TO CREATE A BINARY FILE TO STORE ROLLNO, NAME AND MARKS DELETE A
PARTICULAR STUDENT BY SEARCHING HIS ROLL NUMBER
SOURCE CODE
35
35
output
35
PROGRAM:12
PROGRAM TO CREATE A CSV FILE TO STORE EMPNO,NAME,SALARY AND SEARCH FOR EMPLOYEE
BASED ON EMPNUMBER
SOURCE CODE
import csv
with open('myfile1.csv',mode='a') as csvfile:
mywriter = csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter Employee Number "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
mywriter.writerow([eno,name,salary]) print("##
Data Saved... ##")
ans=input("Add More ?")
ans='y'
with open('myfile1.csv',mode='r') as csvfile:
myreader =csv.reader(csvfile,delimiter=',')
while ans=='y':
found=False
e = int(input("Enter Employee Number to search :")) for
row in myreader:
if len(row)!=0:
if int(row[0])==e:
35
print("NAME :",row[1])
print("SALARY :",row[2])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
ans = input("Search More ? (Y)")
OUTPUT
Enter Employee Number 1 Enter
Employee Name Amit Enter
Employee Salary :90000 ## Data
Saved... ##
Add More ?y
Enter Employee Number 2 Enter
Employee Name Sunil Enter
Employee Salary :80000 ## Data
Saved... ##
Add More ?y
Enter Employee Number 3 Enter
35
Employee Name Satya Enter
Employee Salary :75000 ## Data
35
Saved... ##
Add More ?n
Enter Employee Number to search :2
============================
NAME : Sunil
SALARY : 80000
Search More ? (Y)y
Enter Employee Number to search :3
============================
NAME : Satya
SALARY : 75000
Search More ? (Y)y
Enter Employee Number to search :4
========================== EMPNO
NOT FOUND
Search More ? (Y)n
=====================================================================
35
PROGRAM :13
CREATE A LIST OF5 INTEGERS PUSH THE NUMBERS WHICH ARE DIVISIBLE BY 5 OR 7 INTO STACK POP
AND DISPLAY THE STACK CONTENTS
SOURCECODE
#STACK INTEGERS
lst=[]
st=[]
top=None
for k in range(5): n=int(input("enter
a number")) lst.append(n)
def push(): for
k in lst:
if k%5==0 or k%7==0:
st.append(k)
top=len(st)-1
35
def pop():
if len(st)==0:
print("underflow")
else:
print("going to delete",st.pop())
top=len(st)-1
def display():
print("stack contents are..") for i
in range(len(st)-1,-1,-1):
print(st[i])
choice='y'
while choice=='y':
print("menu")
print("1. push ")
print("2.pop")
print("3.display")
ch=int(input("enter ur choice")) if
ch==1:
push() elif
ch==2:
pop()
elif ch==3:
35
35
display()
else:
print("invalid choice")
choice=input("do u want to continue")
OUTPUT:
enter a number13
enter a number25
enter a number21
enter a number28
enter a number15
menu
1. push
2.pop
3.display
enter ur choice1
do u want to continuey
menu
1. push
2.pop
3.display
enter ur choice3 stack
contents are.. 15
28
35
21
25
do u want to continuey
menu
1. push
2.pop
3.display
enter ur choice2
going to delete 15
do u want to continuey
menu
1. p
ush
2.pop
3. display
enter ur choice3 stack
contents are.. 28
21
25
do u want to continuen
35
PROGRAM :14
PROGRAM TO DEMONSTRATE STACK OPERATIONS USING DICTIONARIES
CREATE A DICTIONARY EMPLOYEE WITH EMP NAME AS KEY AND THEIR SALARY AS VALUES. WRITE
FUNCTIONS TO PERFORM FOLLOWING OPERATIONS
(a) PUSH THE NAMES OF THOSE EMPLOYESS INTO STACK WHERE SALARY IS LESS THAN 50000
(b) POP THE ELEMENTS OF THE STACK AND DISPLAY THOSE NAMES
SOURCECODE
stack=[]
emp={}
def push():
for d in emp:
if emp[d]<50000:
stack.append(d)
def pop():
if len(stack)==0:
print("no employees under 50000 salary UNDER FLOW")
else: print("employee going to delete is",stack.pop())
def display():
for i in range(len(stack)-1,-1,-1):
print("employee name::",stack[i])
35
def createdict():
for i in range(4):
ename=input("enter employee name")
sal=int(input("enter emp salary::"))
emp[ename]=sal
#main
createdict()
push()
print("LIST OF EMPLOYEES LESS THAN 50000 SALARY")
display()
print("\n LIST OF EMPLOYEES LESS THAN 50000 SALARY AFTER POPING")
pop()
print("LIST OF EMPLYEE ")
display()
35
OUTPUT
enter employee nameSASI
enteremp salary::65000 enter
employee nameANISH
enteremp salary::23000
enter employee nameGOUTHAM enteremp
salary::77800
enter employee nameLUCKY
enteremp salary::24000
LIST OF EMPLOYEES LESS THAN 50000 SALARY
employee name:: LUCKY
employee name:: ANISH
LIST OF EMPLOYEES LESS THAN 50000 SALARY AFTER POPING
employee going to delete is LUCKY
LIST OF EMPLYEE
employee name:: ANISH
35
PROGRAM:15
WRITE A PROGRAM TO TAKE LIST OF 5 WORDS
PUSH THOSE WORDS WHICH ARENOT HAVING ANY
VOWELS INTO THE STACK POP AND DISPLAY STACK
CONTENT
SOURCE CODE:
st=[]
def push():
for w in words:
for ch in w:
if ch in 'aeiouAEIOU':
break
else:
st.append(w)
def pop():
if len(st)==0:
print(" UNDER FLOW")
else:
print("\n word going to delete is",st.pop())
def display():
print("stack with words without vowels::")
for i in range(len(st)-1,-1,-1):
print(st[i],end='\t')
words=[]
for i in range(5):
w=input("enter a word")
words.append(w)
push()
display()
pop()
print("stack after pop...")
display()
38
OUTPUT:
enter a wordshy
enter a wordcry
enter a wordworls
enter a wordhai
enter a wordfry
stack with words without vowels::
fry cry shy
word going to delete is fry
stack after pop...
stack with words without vowels::
cry shy
38