0% found this document useful (0 votes)
2 views2 pages

Python Lab

The document contains Python code that performs various tasks including reading and writing files, handling exceptions, zipping files, and defining classes for complex numbers and student records. It demonstrates file manipulation, error handling, and object-oriented programming concepts. The code includes functions for adding complex numbers and calculating student marks and percentages.

Uploaded by

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

Python Lab

The document contains Python code that performs various tasks including reading and writing files, handling exceptions, zipping files, and defining classes for complex numbers and student records. It demonstrates file manipulation, error handling, and object-oriented programming concepts. The code includes functions for adding complex numbers and calculating student marks and percentages.

Uploaded by

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

inputfile = open('input.

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

def DIVEXP(a, b):


assert a > 0, "a should be greater than 0"
try:
c = a / b
except ZeroDivisionError:
print('HandledzeroDivisionError')
return c
print(DIVEXP(1, 2))
print(DIVEXP(-10, 3))
print(DIVEXP(5, 0))
print(DIVEXP(15, 3))

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

You might also like