Obs Rograms
Obs Rograms
def addition(a,b):
sum = a + b
def subtraction(a,b):
difference = a - b
def multiplication(a,b):
product = a * b
def division(a,b):
quotiant = a / b
remainder = a % b
while True:
print(" 5. Exit")
if Choice==1:
print("In Addition of two no:")
addition(a,b)
elif Choice==2:
subtraction(a,b)
elif Choice==3:
multiplication(a,b)
elif Choice==4:
division(a,b)
elif Choice==5:
break
else:
2)
def swap(lst):
if lst[i+1]%5==0:
lst[i],lst[i+1]=lst[i+1],lst[i]
lst=[ ]
while True:
print("3. Exit")
ch = int(input("Enter ur choice:"))
if ch == 1:
elif ch == 2:
swap(lst)
elif ch == 3:
print("Thank You")
break
else:
OUTPUT:
1. List creation
2. Swap value:
3. Exit
Enter ur choice:1
Enter a list:[3,25,6,13,35,14,45]
1. List creation
2. Swap value:
3. Exit
Enter ur choice:2
25 3 6 35 13 45 14
1. List creation
2. Swap value:
3. Exit
Enter ur choice:3
Thank You
1. List creation
2. Swap value:
3. Exit
Enter ur choice:4
3)
def palindrome(s):
rev = s[::-1]
if rev == s:
else:
c = st.count(ch)
return c
#Main Program
while True:
print("Menu")
print("1. Pallindrome")
print("3. Exit")
if opt == 1:
palindrome(s)
elif opt == 2:
st = input("Enter string:")
ch = input("Enter a Character:")
elif opt == 3:
break
else:
OUTPUT:
1. Pallindrome
2. To count no of occurances of character
3. Exit
Enter ur choice:1
Enter the string:TENET
*This String is a pallindrome*
Menu
1. Pallindrome
2. To count no of occurances of character
3. Exit
Enter ur choice:2
Enter string:TENET
Enter a Character:E
*The Character* E *is present* 2 *times*
Menu
1. Pallindrome
2. To count no of occurances of character
3. Exit
Enter ur choice:4
Error 404 not found @_@
4)
def search(lst,ele):
found = False
for i in range(len(lst)):
found = True
if found:
print("Frequency", lst.count(ele))
else:
lst = []
while True:
print("3. Exit")
ch = int(input("Enter ur choice:"))
if ch == 1:
elif ch == 2:
search(lst, x)
elif ch == 3:
break
else:
print("Invalid Choice^_^!")
OUTPUT:
1. List creation
2. Search value
3. Exit
Enter ur choice:1
Enter list element[1,5,7,25,45,7]
1. List creation
2. Search value
3. Exit
Enter ur choice:2
Enter the element:5
Element not found^_^!
Position 1Frequency 1
Frequency 1
Frequency 1
Frequency 1
Frequency 1
1. List creation
2. Search value
3. Exit
Enter ur choice:1
Enter list element[1,5,7,25,45,7]
1. List creation
2. Search value
3. Exit
Enter ur choice:2
Enter the element:1
Position 0Frequency 1
Frequency 1
Frequency 1
Frequency 1
Frequency 1
Frequency 1
1. List creation
2. Search value
3. Exit
Enter ur choice:3
5)
def search(d):
else:
d = {}
for i in range(1,16):
d[i] = i*i
print(d)
while True:
print("2. Exit")
ch = int(input("Enter ur choice:"))
if ch == 1:
search(d)
elif ch == 2:
break
else:
OUTPUT:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196,
15: 225}
1. Search for value
2. Exit
Enter ur choice:1
Enter a key-value to search:1
key 1 is found,the value is 1
1. Search for value
2. Exit
Enter ur choice:7
invalid choice ^_^!!!
6)
f = open("Story.txt","r")
Contents = f.readlines()
words = lines.split()
for i in words:
print(" ")
f.close()
OUTPUT:
Like# Moon# sky#
7)
f=open ("story.txt","r")
content = f.read()
vowels=0
consonants=0
lower_case=0
upper_case=0
for ch in content:
if ch in 'aeiouAEIOU':
vowels+=1
if ch in 'bcdfjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
consonants+=1
if ch.islower():
lower_case=lower_case+1
if ch.isupper():
upper_case+=1
f.close()
OUTPUT:
the total no of vowels in the file: 158
the total no of consonant in the file 171
the total no of upper case in the file 21
the total no of lower case in the file 332
8)
f1=open("sample.txt","r")
f2=open("new.txt","w")
while True:
line=f1.readline()
if line=="":
break
if line[0]=='a' or line[0]=='A':
f2.write(line)
print("all line which are starting with character a or A has been copied sucssful into new.txt")
f1.close()
f2.close()
OUTPUT:
all line which are starting with character a or A has been copied successful into new.txt
9)
import pickle
def Create():
f=open("student.dat",'ab')
opt ='y'
NAME=input("Enter Name:")
L=[Roll_No,NAME]
pickle.dump(L,f)
f.close()
def search():
f=open("student.dat",'rb')
found=0
try:
while True:
s=pickle.load(f)
if s[0]== no:
found=1
break
except:
f.close()
if found==0:
Create()
search()
OUTPUT:
Enter roll no:1
Enter Name:Deepak
Do you want to add another student detail(y/n):y
Enter roll no:2
Enter Name:Selva
Do you want to add another student detail(y/n):y
Enter roll no:3
Enter Name:Dhanush
Do you want to add another student detail(y/n):y
Enter roll no:4
Enter Name:Kamalesh
Do you want to add another student detail(y/n):n
enter roll no of student to search:1
the search roll no is found & detail are: [1, 'Deepak']
10)
import pickle
def Create():
F = open("Marks.dat", "ab")
opt = "y"
L = [Roll_no,Name,Mark]
pickle.dump(L,F)
F.close()
def Update():
F = open("Marks.dat", "rb+")
found = 0
try:
while True:
Pos = F.tell()
S = pickle.load(F)
if S[0]==no:
F.seek(Pos)
pickle.dump(S,F)
found = 1
F.seek(Pos)
break
except:
F.close()
if found == 0:
#Main Program
Create()
Update()
OUTPUT:
Enter roll no:1
Enter name:Selva
Enter marks:500
Do u want to add another Student detail(y/n):y
Enter roll no:2
Enter name:Dhaanush
Enter marks:-1
Do u want to add another Student detail(y/n):y
Enter roll no:3
Enter name:Deepak
Enter marks:500
Do u want to add another Student detail(y/n):y
Enter roll no:4
Enter name:Kamalesh
Enter marks:-20
Do u want to add another Student detail(y/n):n
Enter Student Roll no to modify marks:2
The Searched Roll.No is found & Details are: [2, 'Dhaanush', -1]
Enter new mark to be Update:-9
Mark updated Successfully & Details are: [2, 'Dhaanush', -9]
11)
import csv
def Create():
f=open("emp.csv",'a',newline='')
w=csv.writer(f)
opt='y'
while opt=='y':
L=[no,name,sal]
w.writerow(L)
f.close()
def search():
f=open("emp.csv",'r',newline='\r\n')
found=0
row = csv.reader(f)
if data[0]==str(no):
print("====================================")
print("Name:",data[1])
print("Salary",data[2])
print("====================================")
found=1
break
if found==0:
f.close()
Create()
search()
OUTPUT
enter employee no:1
enter employee name:Selva
enter employee salary:1000000
do you want to continue (y/n):y
enter employee no:2
enter employee name:Dhaanush
enter employee salary:10000
do you want to continue (y/n):n
enter empolyee no to search1
12)
import csv
def readcsv():
f=open ("Marks.csv",newline="\r\n")
r=csv.reader(f)
for data in r:
print(data)
def writecsv():
f=open("Marks.csv","w")
writer=csv.writer(f)
opt="y"
while opt=="y":
rollno=int(input("Enter the rollno:"))
l=[rollno,name,marks]
writer.writerow(l)
while True:
print("Press 3 to exit")
if a==1:
readcsv()
elif a==2:
writecsv()
else:
break
OUTPUT
Press 1 to read data
Press 2 to write data
Press 3 to exit
Enter ur choice between 1 to 3:1
Press 1 to read data
Press 2 to write data
Press 3 to exit
Enter ur choice between 1 to 3:2
Enter the rollno:1
Enter the name:Selva
Enter the mark:499
Do you want to continue(y/n)?y
Enter the rollno:2
Enter the name:Dhaanush
Enter the mark:20
Do you want to continue(y/n)?n
Press 1 to read data
Press 2 to write data
Press 3 to exit
Enter ur choice between 1 to 3:1
['1', 'Selva', '499.0']
['2', 'Dhaanush', '20.0']
Press 1 to read data
Press 2 to write data
Press 3 to exit
Enter ur choice between 1 to 3:3