Python Lab
Python Lab
txt', 'r')
outputfile = open('output.txt', 'w')
lines = inputfile.readlines()
print('Number of lines in the file is:', len(lines))
cleanedlines = []
for line in lines:
line = line.strip()
cleanedlines.append(line)
cleanedlines.sort()
for line in cleanedlines:
outputfile.write(line + '\n')
import zipfile
import os
file_path = [ ]
directory = 'C:\\jnnce'
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_path.append(filepath)
zipobject = zipfile.ZipFile('C:\\jnnce.zip','w')
for file in file_paths:
zipobject.write(file)
zipobject.close()
print('All files zipped successfully')
class complex:
def __init__(self, r=0, i=0):
self.real = r
self.imag = i
def input(self):
self.real = int(input('Enter the real num: '))
self.imag = int(input('Enter the imag num: '))
def display(self):
print(self.real, "+", self.imag, "i", sep='')
def addcomplex(self, c1, c2):
c3 = complex()
c3.real = c1.real + c2.real
c3.imag = c1.imag + c2.imag
return c3
o1 = complex(1, 2)
o2 = complex(3, 4)
o3 = complex()
o3 = o3.addcomplex(o1, o2)
o3.display()
compobj = []
sum = complex()
n = int(input('Enter number of objects: '))
for i in range(n):
ob = complex()
print('Enter complex number', i + 1)
ob.input()
compobj.append(ob)
for i in range(n):
sum.real = sum.real + compobj[i].real
sum.imag = sum.imag + compobj[i].imag
print('Complex numbers are:')
for i in range(n):
compobj[i].display()
print('Sum of complex numbers is:')
sum.display()
class student:
def __init__(self, name = '', usn = '', marks = [0,0,0,0]):
self.name = name
self.usn = usn
self.marks = marks
def getmarks(self):
print('enter details of student')
self.name = input('enter student name:')
self.usn = input('enter student USN:')
self.marks[0] = int(input('enter Python marks:'))
self.marks[1] = int(input('enter Java marks:'))
self.marks[2] = int(input('enter Com AT marks:'))
def display(self):
self.marks[3] = self.marks[0] + self.marks[1] + self.marks[2]
print('Student details are:')
print('Student name:', self.name)
print('Student USN:', self.usn)
print('Python marks:', self.marks[0])
print('Java marks:', self.marks[1])
print('Com AT marks:', self.marks[2])
print('Percentage:', self.marks[3] / 3)
s = student()
s.getmarks()
s.display()