Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practicals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

5.

READ A TEXT FILE AND DISPLAY THE NUMBER OF VOWELS/ CONSONANTS/


UPPERCASE/ LOWERCASE CHARACTERS IN THE FILE.
AIM:

To read the content of file and display the total number of consonants, uppercase, vowels
and lower-case characters
ALGORITHM :
Step 1 : Open the text file ‘File1.txt’ as read only mode
Step 2 : Initialize all needed variable for counting vowels, consonants, lowercase and uppercase
characters
Step 3 : To read all characters from the file one by one
Step 4 : To check the character is vowel if yes, increment the variable ‘V’
Step 5 : To check all characters from the file as consonant, uppercase and lowercase if yes,
increment the variables c, u an l respectively up to end
of file(EOL)

CODING:

#Program to read content of file and display total number of vowels, consonants, lowercase and
uppercase characters

f = open("File1.txt")
v=c=u=l=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
f.close()

File1.txt
India is my country
I love python
Python learning is fun

OUTPUT :
Total Vowels in file : 16
Total Consonants in file : 30
Total Capital letters in file : 3
Total Small letters in file : 43

RESULT :
Thus the program has been executed successfully for finding number of vowels, consonants,
uppercases, lowercases are in the file and output was verified.

6. REMOVE ALL THE LINES THAT CONTAIN THE CHARACTER `A' IN A FILE AND
WRITE IT TO ANOTHER FILE.

AIM:

To read the content from a file line by line and write it to another file except for those
lines contains letter “a” in it.
ALGORITHM :
Step 1: To create two text files
Step 2: One file is opened as read only mode and next one is for writing
Step 3: To read the content from the file ‘File1.txt’ as line by line
Step 4: To check the line has a character “a”, if exist, read next line from
‘File1.txt’
Step 5: If not exist the character “a” in the line then to write into another
file “File1copy.txt’ and follow the above Step 3 up to end of file
CODING :

#Program to read line from file and write it to another line except for those line which contains letter
“a”
f1 = open("file1.txt")
f2 = open("file1copy.txt","w")
for line in f1:
if 'a' not in line:
f2.write(line)
print("## File Copied Successfully! ##")
f1.close()
f2.close()
File 1.txt:
a quick brown fox
one two three four
five six seven
India is my country
eight nine ten
File1copy.txt :
one two three four
five six seven
eight nine ten
bye!
RESULT:
Thus the program has been executed successfully for copying a file to another file except
those lines has letter ‘a’ and output was verified.

7. Create a binary file with name and roll number. Search for a given roll number and display the
name, if not found display appropriate message.
AIM:
To create binary file to store Rollno and Name, Search any Rollno and display name if
Rollno found otherwise “Rollno not found”
ALGORITHM :
Step 1: Import the module pickle for creation of binary file
Step 2: Open a file ‘Student.dat’ as binary mode for writing
Step 3: Get Roll Number and Name of the student from the user and write into
the file.
Step 4: User can get roll number and display their name , if exist
Step 5: If roll number not exist then it will display as ‘Roll number not exist in the
file’.
Coding :

#Program to create a binary file to store Rollno and name, Search for Rollno and display record if
found, otherwise "Roll no. not found"
import pickle
student=[]
#Binary file opened as Append and Write Mode
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
#Binary file opened as Read Mode
f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
output:
Enter Roll Number :1
Enter Name :sam
Add More ?(Y)y
Enter Roll Number :2
Enter Name :david
Add More ?(Y)y
Enter Roll Number :3
Enter Name :riyaz
Add More ?(Y)n
Enter Roll number to search :2
## Name is : david ##
Search more ?(Y) :n
>>>
Result:
Thus the program has been executed successfully for searching the student name in
binary file when roll number is exist and output was verified

8. CREATE A BINARY FILE WITH ROLL NUMBER, NAME AND MARKS. INPUT A
ROLL NUMBER AND UPDATE THE MARKS.

AIM:
To create binary file to store Rollno, Name and Marks and update the marks of entered
Rollno
ALGORITHM :
Step 1: Import the module pickle for creation of binary file
Step 2: Open a file ‘Student.dat’ as binary mode for writing
Step 3: Get Roll Number, Name and Mark of the student from the user and
write into the file.
Step 4: User can get roll number and mark from the user and if Roll number is
exist in the file then mark is updated
Step 5: If Roll number does not exist the file it will return as Roll number not
exist in the file.
CODING:

import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2],"##")
m = int(input("Enter new marks :"))
s[2]=m
print("## After Updating the Mark is",s[2])
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
OUTPUT:
Enter Roll Number :1
Enter Name :ESHA
Enter Marks :56
Add More ?(Y)Y
Enter Roll Number :2
Enter Name :NEHA
Enter Marks :67
Add More ?(Y)N
Enter Roll number to update :2
## Name is : NEHA ##
## Current Marks is : 67 ##
Enter new marks :89
## After Updating the Mark is 89
Update more ?(Y) :Y
Enter Roll number to update :4
####Sorry! Roll number not found ####
Update more ?(Y) :N
>>>

RESULT:
Thus the program has been executed successfully for searching the student name and
update their mark in binary file when roll number is exist and output was verified
9. WRITE A RANDOM NUMBER GENERATOR THAT GENERATES RANDOM
NUMBERS BETWEEN 1 AND 6 (SIMULATES A DICE).
AIM :
To generate random number between 1 and 6 using a Dice
ALGORITHM :
Step 1: Import the module random for generating random number generation
Step 2: Define a function to rolladice() in that initialize two variables.
Step 3: Check the condition to generate random number between 1 and 6 using randint().
Step 4: Check the condition if counter is greater than 6,if condition is true pass the value
else return to mylist
Step 5: Get the input from the user to roll the dice or not.
CODING :
# Generates a random number between 1 and 6
import random
def rolladice():
counter=0
mylist=[]
while(counter)<6:
randomnumber=random.randint(1,6)
mylist.append(randomnumber)
counter=counter+1
if (counter)>=6:
pass
else:
return mylist
n=1
while(n==1):
n=int(input("enter 1 to roll a dice and get a random number"))
print(rolladice())
OUTPUT:
enter 1 to roll a dice and get a random number1
[5]
enter 1 to roll a dice and get a random number1
[5]
enter 1 to roll a dice and get a random number1
[4]
enter 1 to roll a dice and get a random number1
[3]
enter 1 to roll a dice and get a random number3
RESULT :
Thus the program has been executed successfully for generating the random numbers
between 1 and 6 using a dice and output was verified.

10.TAKE A SAMPLE OF TEN PHISHING E-MAILS (OR ANY TEXT FILE) AND FIND
MOST COMMONLY OCCURRING WORD(S)
AIM:
To take a sample of Ten phising E-Mails and find most commonly occurring word(s).
ALGORITHM:
Step 1: To create a list with collection of E-mail addresses.
Step 2: To split each E-mail address with the character ‘@’.
Step 3: Count commonly occurring words in each E-mail address.
Step 4: Display commonly occurring words.
CODING:
# To take 10 sample phishing mail and count the most commonly occurring word(s).

phisingemail=["offers@wish.com","irctcpromotions@irctc.co.in","welcome@wish.com","reductio
n@myntra.com","yourlucky@mymoney.com","spinthewheel@mylottery.com","youarethewinner@
mymoney.com","luckyjackpot@americanlottery.com","claimtheprize@mymoney.com","dealwinne
r@snapdeal.com"]
myd={}
for e in phisingemail:
x=e.split('@')
for w in x:
if w not in myd:
myd[w]=1
else:
myd[w]+=1
key_max=max(myd,key=myd.get)
print("most common occurring word is :",key_max)

OUTPUT:
Most common occurring word is: mymoney.com
RESULT:
Thus the program has been executed successfully for counting the phishing E-Mails and
output was verified.
11. Program to create CSV file and store empno, name and salary and search any empno and
display name, salary and if not found appropriate message.
AIM:
To create CSV file and store empno, name and salary and search any empno and display
name, salary and if not found appropriate message.
ALGORITHM:
Step 1: Import the module csv for creation of CSV file
Step 2: Open a file ‘sample.csv’ as append mode for writing and reading.
Step 3: Get Employee Number, Employee Name and Employee salary of the
employee from the user and write into the file.
Step 4: Search the Employee number, if found display the name and salary of the
Employee.
Step 5: If Employee number does not exist the file it will return as Employee
number not exist in the file.
CODING:
import csv
f=open('sample.csv','a')
mywriter=csv.writer(f,delimiter=' ')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter the number"))
ename=input("Enter the Employee name")
esalary=int(input("Enter the Employee salary"))
mywriter.writerow([eno,ename,esalary])
print("Data Saved")
ans=input("Add more")
f.close()
ans='y'
x=open('sample.csv','r')
myreader=csv.reader(x,delimiter=' ')
while ans=='y':
found=False
e=int(input("Enter the number to be searched"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("===============")
print("name:",row[1])
print("salary:",row[2])
found=True
break
if not found:
print("===================")
print("Emp not found")
print("===================")
ans=input("search more?(Y)")
x.close()

OUTPUT:
Enter the number 1
Enter the Employee name annie
Enter the Employee salary 4500
Data Saved
Add morey
Enter the number 2
Enter the Employee name aldo
Enter the Employee salary 450000
Data Saved
Add more n
Enter the number to be searched 2
===============
name: aldo
salary: 450000
search more?(Y)y
Enter the number to be searched 5
===================
Emp not found
===================
search more?(Y) n
>>>
RESULT:
Hence the program is executed successfully to create CSV file and store empno, name and
salary and search any empno and display name, salary and if not found appropriate message.
12. STACK OPERATIONS
AIM:
To implement stack operations of push,pop and display using definitions.
Algorithm:
Step 1: Created definitions for Push, Pop and Display
Step 2: Calling these definitions in the main program through Menu driven options to the
user to choose the required options
Step 3 : Stack will be appended with an element while Push () function is called
Step 4:An element is deleted when Pop() function is called
Step 5: All the elements of stack will be displayed while Display() function is called
Coding:
def push(s):
x=int(input("enter a number"))
s.append(x)
def pop(s):
if s!=[]:
t=s.pop()
print("deleted item is:",t)
else:
print("stack is empty")
def display(s):
if s!=[]:
for i in s:
print(i)
else:
print("empty stack")
s=[]
a='y'
while a=='y':
print("1.push\n2.pop\n3.display\n4.exit")
ch=int(input("enter your choice"))
if ch==1:
push(s)
elif ch==2:
pop(s)
elif ch==3:
display(s)
elif ch==4:
break
else:
print("invalid option")
ch=str(input("do you want to continue"))

OUTPUT:
1.push
2.pop
3.display
4.exit
enter your choice1
enter a number23
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice1
enter a number455
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice3
23
455
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice2
deleted item is: 455
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice5
invalid option
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice4
>>>

RESULT:
Hence the stack operation is executed successfully.

13. STACK OPERATION TO PUSH EVEN NUMBERS


AIM:
To implement stack operations to push even numbers ,pop and display using
definitions.
Algorithm:
Step 1: Created definitions for Push, Pop and Display
Step 2: Calling these definitions in the main program through Menu driven options to the
user to choose the required options
Step 3 : Stack will be appended with an even element while Push () function is called
Step 4:An element is deleted when Pop() function is called
Step 5: All the elements of stack will be displayed while Display() function is called
Coding:
def push(s):
x=int(input("enter a number"))
if x%2==0:
s.append(x)
else:
print("number is not even number")
def pop(s):
if s!=[]:
t=s.pop()
print("deleted item is:",t)
else:
print("stack is empty")
def display(s):
if s!=[]:
for i in s:
print(i)
else:
print("empty stack")
s=[]
a='y'
while a=='y':
print("1.push\n2.pop\n3.display\n4.exit")
ch=int(input("enter your choice"))
if ch==1:
push(s)
elif ch==2:
pop(s)
elif ch==3:
display(s)
elif ch==4:
break
else:
print("invalid option")
ch=str(input("do you want to continue"))

output:
1.push
2.pop
3.display
4.exit
enter your choice1
enter a number 23
number is not even number
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 1
enter a number 34
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 1
enter a number 68
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 2
deleted item is: 68
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 3
34
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice4
>>>
RESULT:
Hence the program is executed successfully.

14. Stack operation to push odd numbers

AIM:
To implement stack operations to push odd numbers ,pop and display using
definitions.
Algorithm:
Step 1: Created definitions for Push, Pop and Display
Step 2: Calling these definitions in the main program through Menu driven options to the
user to choose the required options
Step 3 : Stack will be appended with an odd element while Push () function is called
Step 4:An element is deleted when Pop() function is called
Step 5: All the elements of stack will be displayed while Display() function is called
Coding:
def push(s):
x=int(input("enter a number"))
if x%2!=0:
s.append(x)
else:
print("number is not odd number")
def pop(s):
if s!=[]:
t=s.pop()
print("deleted item is:",t)
else:
print("stack is empty")
def display(s):
if s!=[]:
for i in s:
print(i)
else:
print("empty stack")
s=[]
a='y'
while a=='y':
print("1.push\n2.pop\n3.display\n4.exit")
ch=int(input("enter your choice"))
if ch==1:
push(s)
elif ch==2:
pop(s)
elif ch==3:
display(s)
elif ch==4:
break
else:
print("invalid option")
ch=str(input("do you want to continue"))

output:
1.push
2.pop
3.display
4.exit
enter your choice1
enter a number 34
number is not odd number
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 1
enter a number 35
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 1
enter a number 89
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 3
35
89
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 2
deleted item is: 89
do you want to continue y
1.push
2.pop
3.display
4.exit
enter your choice 4
>>>

RESULT:
Hence the program is executed successfully.

You might also like