?print-pdf
' Created for
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
# trace the values of i and result:
print(f"i: {i}, result: {result}")
return result
print(factorial(5))
# i: 1, result: 1
# i: 2, result: 2
# i: 3, result: 6
# i: 4, result: 24
# i: 5, result: 120
# 120
import logging
# Configure logging
logging.basicConfig(filename='factorial.log', level=logging.DEBUG)
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
logging.debug(f"i: {i}, result: {result}")
return result
print(factorial(5))
DEBUG:root:i: 1, result: 1
DEBUG:root:i: 2, result: 2
DEBUG:root:i: 3, result: 6
DEBUG:root:i: 4, result: 24
DEBUG:root:i: 5, result: 120
import logging
# Configure logging to output messages to both console and file
logging.basicConfig(level=logging.DEBUG,
handlers=[
logging.FileHandler('example.log'), # write log messages to a file
logging.StreamHandler()] # output log messages to the console
)
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
logging.debug(f"i: {i}, result: {result}")
return result
def main():
logging.info("Starting factorial calculation...")
result = factorial(5)
logging.info(f"Factorial of 5 is: {result}")
if __name__ == "__main__":
main()
import logging
# Configure logging to output messages to console
logging.basicConfig(level=logging.WARNING)
# Log messages at different levels
logging.debug("Debug message - Detailed information for debugging purposes")
logging.info("Info message - Confirmation that things are working as expected")
logging.warning("Warning message - Something unexpected happened, but the software is still functioning")
logging.error("Error message - A more serious problem that might cause the application to malfunction")
logging.critical("Critical message - A critical error that could lead to application failure")
# WARNING:root:Warning message - Something unexpected happened, but the software is still functioning
# ERROR:root:Error message - A more serious problem that might cause the application to malfunction
# CRITICAL:root:Critical message - A critical error that could lead to application failure
import logging
# Configure logging with custom format
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.DEBUG)
# Log messages
logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")
# 2025-03-26 12:19:48,626 - DEBUG - Debug message
# 2025-03-26 12:19:48,626 - INFO - Info message
# 2025-03-26 12:19:48,626 - WARNING - Warning message
# 2025-03-26 12:19:48,627 - ERROR - Error message
# 2025-03-26 12:19:48,627 - CRITICAL - Critical message
Command | Keyboard Shortcut |
---|---|
Continue / Pause | F5 |
Step Over | F10 |
Step Into | F11 |
Step Out | Shift+F11 |
Restart | Ctrl+Shift+F5 |
Stop | Shift+F5 |
These slides are based on
customised version of
framework