Python Assignment 03
Python Assignment 03
ASSIGNMENT-03
Wednesday, June 18, 2025 PM 7:33
➢ The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working
directory.
>>> import zipfile, os
>>> os.chdir('C:\\') # move to the folder with example.zip
>>> exampleZip = zipfile.ZipFile('example.zip')
>>> exampleZip.extractall()
>>> exampleZip.close()
ANS: ➢ Assertions :
➢ An assertion is a sanity check to make sure your code isn’t doing something obviously wrong.
➢ These sanity checks are performed by assert statements. If the sanity check fails, then an AssertionError exception is raised.
➢ The shutil (or shell utilities) module has functions to let you copy, move, rename, and delete files in your Python programs
1.1
➢ Calling os.unlink(path) will delete the file at path. ➢ Calling os.rmdir(path) will delete the folder at path. This folder must
be empty of any files or folders
➢ Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be deleted.
import os
for filename in os.listdir():
if filename.endswith('.rxt'):
os.unlink(filename)
● __init__ is a special method called the constructor, which initializes the object's attributes.
● self.x and self.y are attributes of the Point object representing its coordinates.
➢ Creating an Object :
To create an object from a class, you call the class like a function:
p = Point(3.0, 4.0)
● p is now a Point object with coordinates x = 3.0 and y = 4.0.
Example Output
When you print the object:
print(p)
# Output: Point(3.0, 4.0)
Python tells you the class (Point) and memory location (in hexadecimal).
Disabling Logging:
➢ The logging.disable() function disables these so that you don’t have to go into your program and remove all the logging
calls by hand.
➢ pass logging.disable() a logging level, and it will suppress all log messages at that level or lower
>>> import logging
>>> logging.basicConfig(level=logging.INFO, format=' %(asctime)s -%(levelname)s - %(message)s')
>>> logging.critical('Critical error! Critical error!')
2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
>>> logging.disable(logging.CRITICAL)
>>> logging.critical('Critical error! Critical error!')
https://onedrive.live.com/edit.aspx?resid=470D93A974EDE48C!se918ae3ec5864c3eadca27076b80a9ce&migratedtospo=true&wd=target%28Quick Notes.one%7Cae602f84-c49c-415c-a10a-c52072cb1469%2FUnti… 6/11
6/18/25, 8:26 PM OneNote
2. Instead of writing a new function for every possible type (strings, lists, etc.), the function works for any type that can be
counted or processed in a similar way.
3. This makes your code more reusable, saving time and effort.
def add_elements(sequence):
return sum(sequence)
print(add_elements([1, 2, 3])) # Output: 6
print(add_elements(["a", "b", "c"]))
Output: "abc"
9. Develop a program that uses class Student which prompts the user to enter marks in three subjects and
calculates total marks percentage and displays the score card details.
ANS:
class Student:
def init (self, name = "", usn = "", score = [0,0,0,0]):
self.name = name
self.usn = usn
self.score = score
def getMarks(self):
self.name = input("Enter student Name : ")
self.usn = input("Enter student USN : ")
self.score[0] = int(input("Enter marks in Subject 1 : "))
self.score[1] = int(input("Enter marks in Subject 2 : "))
self.score[2] = int(input("Enter marks in Subject 3 : "))
self.score[3] = self.score[0] + self.score[1] + self.score[2]
def display(self):
percentage = self.score[3]/3
spcstr = "=" * 81
print(spcstr)
print("SCORE CARD DETAILS".center(81))
print(spcstr)
print("%15s"%("NAME"),"%12s"%("USN"), "%8s"%"MARKS1","%8s"%"MARKS2","%8s"%"
MARKS3","%8s"%"TOTAL","%12s"%("PERCENTA GE"))
print(spcstr)
print("%15s"%self.name,"%12s"%self.usn, "%8d"%self.score[0],"%8d"%self.
score[1],"%8d"%self.score[2],"%8d"%self.score[3]," %12.2f"%percentage)
print(spcstr)
def main():
s1 = Student()
s1.getMarks()
s1.display()
main()
For example, if you're creating a Time object that has hours, minutes, and seconds, you want to initialize these values when the
object is created
Example: __init__ for a Time class
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
point1 = Point()
point2 = Point(3, 4)
print(point1.x, point1.y) # Output: 0.0
print(point2.x, point2.y) # Output: 3.4