0% found this document useful (0 votes)
29 views6 pages

Week-4 Lab Programs

Uploaded by

yakshitkanna
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)
29 views6 pages

Week-4 Lab Programs

Uploaded by

yakshitkanna
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

Week-4 (python programming)

18.
Experiment:
Write a program to sort words in a file and put them in another file.
The output file should have only lower-case words, so any upper-case words from source must be lowered
Aim:
To sort words in a file and put them in another file.
The output file should have only lower-case words, so any upper-case words from source must be lowered
Description:
 Create a file Data1.txt and type some words and save that file.
 Open a file with Data1.txt in read mode and store in ifile (input file) variable.
 Open a file with Data2.txt in write mode and store in ofile (output file)variable.
 Call read() function on ifile and store in sentence variable
 Call split() function on sentence and store in words variable
 call sort() on words
 lambda checks its converted into lower case or not and then use map() then store in temp

 pass temp to list() and then store in output

 write that output into output file


Program:
ifile=open("Data1.txt","r")
ofile=open("Data2.txt","w")

sentence=ifile.read()
print(sentence)

words=sentence.split()
words.sort()

print(words)
temp = map(lambda x:x.lower(),words)
output = list(temp)

print(output)
for k in output:
ofile.write(k+' ')
print('success')
Output:
fox elephant dog cat bat apple
['apple', 'bat', 'cat', 'dog', 'elephant', 'fox']
['apple', 'bat', 'cat', 'dog', 'elephant', 'fox']
success
19.
Experiment:
Python program to print each line of a file in reverse order
Aim:
To print Python program to print each line of a file in reverse order
Description:
 Create a file Data1.txt and type some words and save that file.
 Read the content of the file
 Print in reverse order
Program:
ifile=open("Data3.txt","r")

filecontent = ifile.read()

print("Content in Reverse Order")

filecontent = filecontent[::-1]
print(filecontent)
Output:
Content in Reverse Order
elppa
tab
tac
god
tnahpele
xof
20
Experiment:
Python program to compute the number of characters, words and lines in a file.
Aim:
To compute the number of characters, words and lines in a file
Description:
 Declare 4 variables and assign to zero
 Open the file in read mode
 Write logic and then print output
Program:

number_of_words = 0
number_of_lines = 0
number_of_characters = 0
number_of_spaces = 0

with open("Data1.txt", 'r') as file:


for line in file:
number_of_words = number_of_words+len(line.split())
temp=line.split()
number_of_lines =number_of_lines+1
number_of_characters = number_of_characters+len(line)
number_of_spaces = number_of_spaces+len(temp) - 1

print("No of words: ", number_of_words)


print("No of lines: ", number_of_lines)
print("No of characters: ", number_of_characters)
print("No of spces: ", number_of_spaces)

Output:
No of words: 6
No of lines: 4
No of characters: 32
No of spces: 2
21. Experiment:
Write a program to create, display, append, insert and reverse the order of the items in the array
Aim:
To create, display, append, insert and reverse the order of the items in the array
Description:
 Create an array() Dictionaries are mutable. We can add new items or change the value of existing items
using an assignment operator.
 Create a menu 1.CREATE 2.Display 3.APPEND 4.INSERT 5.REVERSE 0.EXIT
 Enter your choice and store in n
 Call the functions using ifelse if logic
Program:
import sys

def create_array():
for i in range(0,n):
x=int(input("Enter the element:"))
arr.append(x)

def display_array():
print("Elements of array are:")
print(arr)

def append_array():
ele = int(input("Enter element to append:"))
arr.append(ele)

def insert_array():
pos=int(input("Enter the position where to insert element in array:"))
ele=int(input("Enter element to insert:"))
arr.insert(pos,ele)

def reverse_array():
print("Elements of array in reverse order:")
arr.reverse()
print(arr)
ch=50

arr=list()

n=int(input("Enter number of elements in array:"))

while ch!=0:
print("1.CREATE")
print("2.Display")
print("3.APPEND")
print("4.INSERT")
print("5.REVERSE")
print("0.EXIT")

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


if ch==1:
create_array()
elif ch==2:
display_array()
elif ch==3:
append_array()
elif ch==4:
insert_array()
elif ch==5:
reverse_array()
else:
sys.exit()
Output:
Enter number of elements in array:6
1.CREATE
2.Display
3.APPEND
4.INSERT
5.REVERSE
0.EXIT
Enter your choice:1
Enter the element:10
Enter the element:20
Enter the element:30
Enter the element:40
Enter the element:50
Enter the element:60
1.CREATE
2.Display
3.APPEND
4.INSERT
5.REVERSE
0.EXIT
Enter your choice:5
Elements of array in reverse order:
[60, 50, 40, 30, 20, 10]
1.CREATE
2.Display
3.APPEND
4.INSERT
5.REVERSE
0.EXIT
Enter your choice:3
Enter element to append:99
1.CREATE
2.Display
3.APPEND
4.INSERT
5.REVERSE
0.EXIT
Enter your choice:2
Elements of array are:
[60, 50, 40, 30, 20, 10, 99]
1.CREATE
2.Display
3.APPEND
4.INSERT
5.REVERSE
0.EXIT
Enter your choice:0
An exception has occurred, use %tb to see the full traceback.

SystemExit

You might also like