Creating A Basic Package
Mon 10 August 2026
# my_package/module1.py
def greet():
return "Hello from module1"
# main.py
import my_package.module1
print(my_package.module1.greet())
Score: 10
Category: chapter10_modules_and_packages
Read More# my_package/module1.py
def greet():
return "Hello from module1"
# main.py
import my_package.module1
print(my_package.module1.greet())
Score: 10
Category: chapter10_modules_and_packages
Read Moreimport os
os.mkdir("new_folder")
os.makedirs("parent/child/grandchild")
Score: 10
Category: chapter12_file_handling
Read More# file: calculator.py
def multiply(a, b):
return a * b
# main.py
import calculator
print(calculator.multiply(4, 5)) # Output: 20
Score: 10
Category: chapter10_modules_and_packages
Read Morewith open("sample.txt", "w") as file:
file.write("Hello, Python File System")
Score: 10
Category: chapter12_file_handling
Read Moreclass NegativeNumberError(Exception):
pass
def check_number(n):
if n < 0:
raise NegativeNumberError("Negative numbers are not allowed")
check_number(-3)
---------------------------------------------------------------------------
NegativeNumberError Traceback (most recent call last)
Cell In[1], line 8
5 if n < 0:
6 raise NegativeNumberError("Negative numbers are not allowed")
----> 8 check_number(-3)
Cell In[1], line 6 …Category: chapter7_custom_exceptions
Read Moreclass ApplicationError(Exception):
pass
class DatabaseError(ApplicationError):
pass
class NetworkError(ApplicationError):
pass
Score: 10
Category: chapter7_custom_exceptions
Read Moreclass Car:
def __init__(self, brand):
self.brand = brand
c1 = Car("Toyota")
c2 = Car("Tesla")
print(c1.brand)
print(c2.brand)
Toyota
Tesla
Score: 10
Category: chapter1_oop_basics
Read Moreimport csv
with open("custom.csv", "w", newline="") as file:
writer = csv.writer(file, delimiter="|")
writer.writerow(["Name", "Age", "Country"])
writer.writerow(["Alice", 25, "India"])
Score: 10
Category: chapter11_csv_handling
Read Moreclass InvalidAgeError(Exception):
pass
def validate_age(age):
if age < 18:
raise InvalidAgeError("Age must be 18 or above")
try:
validate_age(16)
except InvalidAgeError as e:
print(e)
Age must be 18 or above
Score: 10
Category: chapter7_custom_exceptions
Read Moreclass TransactionError(Exception):
def __init__(self, code, message):
self.code = code
self.message = message
super().__init__(f"[{code}] {message}")
raise TransactionError(403, "Unauthorized action")
Score: 5
Category: chapter7_custom_exceptions
Read More