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)
- Write a regex pattern to find all email addresses in a string.
- Use try-except to handle division by zero in a function.
- Demonstrate the use of
enumerate()
with a list of fruits.
- Write a list comprehension to generate squares of numbers from 1 to 10.
- Use
zip()
to combine two lists into a list of tuples.
- Write a regex to extract all digits from a given string.
- Explain what
finally
does in exception handling.
- Use a dictionary comprehension to map numbers 1-5 to their cubes.
- Write a set comprehension to find unique vowels in a given sentence.
- Write a simple use case of the
filter()
function.
Intermediate Questions (10)
- Write a regex pattern to validate IPv4 addresses.
- Implement nested try-except blocks for handling multiple exception types.
- Use
functools.reduce()
to find the factorial of a number.
- Write a list comprehension with an if-else conditional inside it.
- Use
itertools.combinations()
to find all 3-item combinations of a list.
- Write a regex to capture dates in 'YYYY-MM-DD' format.
- Explain how
logging
can replace print debugging with an example.
- Use dictionary comprehension to invert keys and values of a given dictionary.
- Write a set comprehension that excludes certain elements based on a condition.
- Explain the difference between
except Exception as e
and except:
.
Advanced Questions (10)
- Write a regex to parse and extract components from a URL (protocol, domain, path).
- Create a custom exception class and demonstrate raising and catching it.
- Write a function that uses
pdb
to debug a sorting algorithm.
- Use list comprehension with multiple nested loops and conditional filtering.
- Write a complex regex that matches phone numbers in multiple international formats.
- Demonstrate use of
warnings
module to generate and handle warnings in Python.
- Write a dictionary comprehension to flatten a nested dictionary (one level deep).
- Write a set comprehension to find common elements across multiple sets.
- Explain and demonstrate how
traceback
can be used to get detailed error info in exceptions.
- 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.