The document provides answers to questions from Modules 4 and 5 of an Introduction to Python Programming course. It covers topics such as the logging module, file backup using zipfile, differences between shutil.copy() and shutil.copytree(), creating a print_time function, defining and accessing classes, and the concepts of pure functions and modifiers. Each answer includes key points and examples to illustrate the concepts discussed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views2 pages
Python_Module4_5_Answers
The document provides answers to questions from Modules 4 and 5 of an Introduction to Python Programming course. It covers topics such as the logging module, file backup using zipfile, differences between shutil.copy() and shutil.copytree(), creating a print_time function, defining and accessing classes, and the concepts of pure functions and modifiers. Each answer includes key points and examples to illustrate the concepts discussed.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
BPLCK205B - Introduction to Python Programming
Answers to Module 4 and Module 5 Question Bank
Module 4 Answers
Q1. Explain the logging module and debug the factorial of a number program
1. logging is a standard module to track events.
2. Supports levels like DEBUG, INFO, etc. 3. Useful for debugging recursive programs. 4. basicConfig() sets up logging format. 5. Used in production for diagnostics. 6. Supports logging to files or console. 7. Factorial uses recursion with logs. 8. Example: import logging logging.basicConfig(level=logging.DEBUG) def fact(n): return 1 if n==0 else n*fact(n-1) print(fact(5))
Q2. Backup folder to ZIP using modules
1. Use zipfile and os modules.
2. Walk through folder using os.walk(). 3. Use ZipFile.write() to add files. 4. zipfile.ZipFile() used to create archive. 5. Make filename dynamic using folder name. 6. Can compress entire directory. 7. Ensures backup safety. 8. Example: import zipfile, os def backup(folder): ... (code omitted for brevity)
Q3. Difference between shutil.copy() and shutil.copytree()
1. shutil.copy() copies individual files.
2. shutil.copytree() copies full folder tree. 3. copy() needs src and dst filenames. 4. copytree() creates new dir at dst. 5. copy() overwrites if file exists. 6. copytree() raises error if dir exists.
Module 5 Answers
Q1. Function print_time() that takes a time object
BPLCK205B - Introduction to Python Programming
Answers to Module 4 and Module 5 Question Bank
1. Create class with hour, min, sec.
2. Function prints time as hh:mm:ss. 3. Uses format specifier for leading 0s. 4. Good for displaying clocks. 5. Reusable in other time-related programs. 6. Example: class Time: ... def print_time(t): ...
Q2. What is class and how to define/access
1. Class = blueprint for object.
2. Defined with class keyword. 3. __init__() initializes data. 4. Access using object.method/attribute. 5. self refers to instance. 6. Example: class Car: def __init__(self, brand): self.brand = brand