0% found this document useful (0 votes)
25 views

Obs Rograms

The document contains 10 code snippets that demonstrate various Python programming concepts like functions, loops, files handling, data structures etc. The code snippets include programs to create a basic calculator, swap elements in a list, check for palindrome, search an element in a list, create and search a dictionary, split and print words from a text file, count characters in a file, copy lines from one file to another, store and retrieve student records using pickle module and create a marks data file.

Uploaded by

Eris Group
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Obs Rograms

The document contains 10 code snippets that demonstrate various Python programming concepts like functions, loops, files handling, data structures etc. The code snippets include programs to create a basic calculator, swap elements in a list, check for palindrome, search an element in a list, create and search a dictionary, split and print words from a text file, count characters in a file, copy lines from one file to another, store and retrieve student records using pickle module and create a marks data file.

Uploaded by

Eris Group
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1)

def addition(a,b):

sum = a + b

print(a, " + ", b, " = ", sum)

def subtraction(a,b):

difference = a - b

print(a, " - ", b, " = ", difference)

def multiplication(a,b):

product = a * b

print(a, " * ", b, " = ", product)

def division(a,b):

quotiant = a / b

remainder = a % b

print(a, " / ", b, " = ", quotiant)

print(a, " % ", b, " = ", remainder)

while True:

print("$ WELCOME TO CALCULATOR $")

print("IN CHOOSE THE OPERATION TO PERFORM:")

print(" 1. Add to two no")

print(" 2. Sub to two no")

print(" 3. Multiplication to two no")

print(" 4. Division to two no")

print(" 5. Exit")

Choice = int(input("Enter ur Choice:"))

if Choice==1:
print("In Addition of two no:")

a = int(input("Enter the first no:"))

b = int(input("Enter the second no:"))

addition(a,b)

elif Choice==2:

print("In Subtraction of two no:")

a=int(input("Enter the first no:"))

b=int(input("Enter the second no:"))

subtraction(a,b)

elif Choice==3:

print("In Multiplication of two no:")

a = int(input("Enter the first no:"))

b = int(input("Enter the second no:"))

multiplication(a,b)

elif Choice==4:

print("In Division of two no:")

a = int(input("Enter the first no:"))

b = int(input("Enter the second no:"))

division(a,b)

elif Choice==5:

print("THANK YOU ! BYE")

break

else:

print("Invalid input! Please, Try again ^_^")


OUTPUT:
$ WELCOME TO CALCULATOR $
IN CHOOSE THE OPERATION TO PERFORM:
1. Add to two no
2. Sub to two no
3. Multiplication to two no
4. Division to two no
5. Exit
Enter ur Choice:1
In Addition of two no:
Enter the first no:3
Enter the second no:4
3 + 4 = 7
$ WELCOME TO CALCULATOR $
IN CHOOSE THE OPERATION TO PERFORM:
1. Add to two no
2. Sub to two no
3. Multiplication to two no
4. Division to two no
5. Exit
Enter ur Choice:2
In Subtraction of two no:
Enter the first no:5
Enter the second no:7
5 - 7 = -2
$ WELCOME TO CALCULATOR $
IN CHOOSE THE OPERATION TO PERFORM:
1. Add to two no
2. Sub to two no
3. Multiplication to two no
4. Division to two no
5. Exit
Enter ur Choice:3
In Multiplication of two no:
Enter the first no:2
Enter the second no:6
2 * 6 = 12
$ WELCOME TO CALCULATOR $
IN CHOOSE THE OPERATION TO PERFORM:
1. Add to two no
2. Sub to two no
3. Multiplication to two no
4. Division to two no
5. Exit
Enter ur Choice:4
In Division of two no:
Enter the first no:4
Enter the second no:2
4 / 2 = 2.0
4 % 2 = 0
$ WELCOME TO CALCULATOR $
IN CHOOSE THE OPERATION TO PERFORM:
1. Add to two no
2. Sub to two no
3. Multiplication to two no
4. Division to two no
5. Exit
Enter ur Choice:5
THANK YOU ! BYE

2)

def swap(lst):

for i in range (len(lst)-1):

if lst[i+1]%5==0:

lst[i],lst[i+1]=lst[i+1],lst[i]

for i in range (len(lst)):

print (lst[i],end=' ')

lst=[ ]

while True:

print("\n choose the operartion to perform:")

print("1. List creation")

print("2. Swap value:")

print("3. Exit")

ch = int(input("Enter ur choice:"))

if ch == 1:

lst = eval(input("Enter a list:"))

elif ch == 2:

swap(lst)
elif ch == 3:

print("Thank You")

break

else:

print("*Error 404 not found* *_*")

OUTPUT:

choose the operartion to perform:

1. List creation

2. Swap value:

3. Exit

Enter ur choice:1

Enter a list:[3,25,6,13,35,14,45]

choose the operartion to perform:

1. List creation

2. Swap value:

3. Exit

Enter ur choice:2

25 3 6 35 13 45 14

choose the operartion to perform:

1. List creation

2. Swap value:

3. Exit

Enter ur choice:3

Thank You

choose the operartion to perform:

1. List creation
2. Swap value:

3. Exit

Enter ur choice:4

*Error 404 not found* *_*

3)

def palindrome(s):

rev = s[::-1]

if rev == s:

print("*This String is a pallindrome*")

else:

print("*This string is not a pallindrome*")

def count(st, ch):

c = st.count(ch)

return c

#Main Program

while True:

print("Menu")

print("1. Pallindrome")

print("2. To count no of occurances of character")

print("3. Exit")

opt = int(input("Enter ur choice:"))

if opt == 1:

s = input("Enter the string:")

palindrome(s)
elif opt == 2:

st = input("Enter string:")

ch = input("Enter a Character:")

cnt = count(st, ch)

print("*The Character*", ch, "*is present*", cnt, "*times*")

elif opt == 3:

break

else:

print("Error 404 not found @_@")

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)):

if ele == lst [i]:

found = True

print("Position", i, end = "")

if found:

print("Frequency", lst.count(ele))

else:

print("Element not found^_^!")

lst = []

while True:

print("1. List creation")

print("2. Search value")

print("3. Exit")

ch = int(input("Enter ur choice:"))
if ch == 1:

lst = eval(input("Enter list element"))

elif ch == 2:

x = int(input("Enter the element:"))

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):

x = int(input("Enter a key-value to search:"))


if x in d.keys():

print("key", x,"is found,the value is", d.get(x))

else:

print("Key not found")

d = {}

for i in range(1,16):

d[i] = i*i

print(d)

while True:

print("1. Search for value")

print("2. Exit")

ch = int(input("Enter ur choice:"))

if ch == 1:

search(d)

elif ch == 2:

break

else:

print("invalid choice ^_^!!!")

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()

for lines in Contents:

words = lines.split()

for i in words:

print(i + "#", end = " ")

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()

print("the total no of vowels in the file:",vowels)

print("the total no of consonant in the file",consonants)

print("the total no of upper case in the file",upper_case)

print("the total no of lower case in the file",lower_case)

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'

while opt =='y':

Roll_No=int(input("Enter roll no:"))

NAME=input("Enter Name:")

L=[Roll_No,NAME]

pickle.dump(L,f)

opt=input ("Do you want to add another student detail(y/n):")

f.close()
def search():

f=open("student.dat",'rb')

no= int(input("enter roll no of student to search:"))

found=0

try:

while True:

s=pickle.load(f)

if s[0]== no:

print("the search roll no is found & detail are:",s)

found=1

break

except:

f.close()

if found==0:

print("the search roll no is not found")

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"

while opt == "y":

Roll_no = int(input("Enter roll no:"))

Name = input("Enter name:")

Mark = int(input("Enter marks:"))

L = [Roll_no,Name,Mark]

pickle.dump(L,F)

opt = input("Do u want to add another Student detail(y/n):")

F.close()

def Update():

F = open("Marks.dat", "rb+")

no = int(input("Enter Student Roll no to modify marks:"))

found = 0
try:

while True:

Pos = F.tell()

S = pickle.load(F)

if S[0]==no:

print("The Searched Roll.No is found & Details are:", S)

S[2] = int(input("Enter new mark to be Update:"))

F.seek(Pos)

pickle.dump(S,F)

found = 1

F.seek(Pos)

print("Mark updated Successfully & Details are:", S)

break

except:

F.close()

if found == 0:

print("The Searched Roll.No is not found")

#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':

no=int(input("enter employee no:"))

name=input("enter employee name:")


sal=float(input("enter employee salary:"))

L=[no,name,sal]

w.writerow(L)

opt =input("do you want to continue (y/n):")

f.close()

def search():

f=open("emp.csv",'r',newline='\r\n')

no =int (input("enter empolyee no to search"))

found=0

row = csv.reader(f)

for data in row:

if data[0]==str(no):

print("\n employee detail are:")

print("====================================")

print("Name:",data[1])

print("Salary",data[2])

print("====================================")

found=1

break

if found==0:

print("the search empolee no is not found")

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

employee detail are:


====================================
Name: Selva
Salary 10000000.0
====================================

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:"))

name=input("Enter the name:")

marks=float(input("Enter the mark:"))

l=[rollno,name,marks]

writer.writerow(l)

opt=input("Do you want to continue(y/n)?")

while True:

print("Press 1 to read data")

print("Press 2 to write data")

print("Press 3 to exit")

a=int(input("Enter ur choice between 1 to 3:"))

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

You might also like