Chapter 10 Assignment: Advanced Python Tools

This assignment covers all topics in Chapter 10: Regular Expressions, Exception Handling, Useful Python Tools, and Comprehensions. Attempt the questions based on your comfort level.

Basic Questions (10)

  1. Write a regex pattern to find all email addresses in a string.
  2. Use try-except to handle division by zero in a function.
  3. Demonstrate the use of enumerate() with a list of fruits.
  4. Write a list comprehension to generate squares of numbers from 1 to 10.
  5. Use zip() to combine two lists into a list of tuples.
  6. Write a regex to extract all digits from a given string.
  7. Explain what finally does in exception handling.
  8. Use a dictionary comprehension to map numbers 1-5 to their cubes.
  9. Write a set comprehension to find unique vowels in a given sentence.
  10. Write a simple use case of the filter() function.

Intermediate Questions (10)

  1. Write a regex pattern to validate IPv4 addresses.
  2. Implement nested try-except blocks for handling multiple exception types.
  3. Use functools.reduce() to find the factorial of a number.
  4. Write a list comprehension with an if-else conditional inside it.
  5. Use itertools.combinations() to find all 3-item combinations of a list.
  6. Write a regex to capture dates in 'YYYY-MM-DD' format.
  7. Explain how logging can replace print debugging with an example.
  8. Use dictionary comprehension to invert keys and values of a given dictionary.
  9. Write a set comprehension that excludes certain elements based on a condition.
  10. Explain the difference between except Exception as e and except:.

Advanced Questions (10)

  1. Write a regex to parse and extract components from a URL (protocol, domain, path).
  2. Create a custom exception class and demonstrate raising and catching it.
  3. Write a function that uses pdb to debug a sorting algorithm.
  4. Use list comprehension with multiple nested loops and conditional filtering.
  5. Write a complex regex that matches phone numbers in multiple international formats.
  6. Demonstrate use of warnings module to generate and handle warnings in Python.
  7. Write a dictionary comprehension to flatten a nested dictionary (one level deep).
  8. Write a set comprehension to find common elements across multiple sets.
  9. Explain and demonstrate how traceback can be used to get detailed error info in exceptions.
  10. Combine multiple Python tools (e.g., regex, comprehensions, and exception handling) in one program that reads and validates input data, processes it, and handles errors gracefully.