Using Assertions (Assert)
Mon 10 August 2026
age = 15
assert age >= 18, "User must be at least 18 years old"
Score: 5
Category: chapter6_exception_handling
Read Moreage = 15
assert age >= 18, "User must be at least 18 years old"
Score: 5
Category: chapter6_exception_handling
Read Moreclass FileMissingError(Exception):
pass
def load_config(filename):
if not filename.endswith(".json"):
raise FileMissingError("Only JSON configuration files supported")
load_config("config.txt")
Score: 5
Category: chapter7_custom_exceptions
Read Moretry:
num = int("10")
except ValueError:
print("Conversion failed")
else:
print("Conversion successful:", num)
Conversion successful: 10
Score: 10
Category: chapter6_exception_handling
Read Moretry:
file = open("data.txt", "r")
except FileNotFoundError:
print("File not found")
finally:
print("Execution completed")
File not found
Execution completed
Score: 10
Category: chapter6_exception_handling
Read Morecounter = 0
def increment():
global counter
counter += 1
increment()
print(counter)
Score: 10
Category: chapter13_global_and_scope
Read Moreimport math
help(math)
Help on built-in module math:
NAME
math
DESCRIPTION
This module provides access to the mathematical functions
defined by the C standard.
FUNCTIONS
acos(x, /)
Return the arc cosine (measured in radians) of x.
The result is between 0 and pi.
acosh(x, /)
Return the inverse …Category: chapter10_modules_and_packages
Read Moredef main():
print("Main function executed")
if __name__ == "__main__":
main()
Main function executed
Score: 10
Category: chapter14_main_function
Read Morefrom my_package import greet
print(greet())
# my_package/__init__.py
from .module1 import greet
Score: 10
Category: chapter10_modules_and_packages
Read Moreimport sys
def main():
print("Arguments received:", sys.argv)
if __name__ == "__main__":
main()
Arguments received: ['C:\\Users\\Ashwin\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\ipykernel_launcher.py', '-f', 'C:\\Users\\Ashwin\\AppData\\Roaming\\jupyter\\runtime\\kernel-381afcb6-19e6-489b-be48-09b4fef13007.json']
Score: 10
Category: chapter14_main_function
Read More# file: demo.py
def main():
print("Main function executed")
if __name__ == "__main__":
main()
Main function executed
Score: 10
Category: chapter14_main_function
Read More