Skip to content
Raymond Cheung edited this page Mar 30, 2024 · 7 revisions

Python Questions

  1. Decorator Usage: Write a Python decorator that logs the time taken by a function to execute.

  2. List Comprehension: Use list comprehension to create a list of squares of numbers from 1 to 10.

  3. Counter Usage: Given a list of words, use the Counter class from the collections module to count the frequency of each word.

  4. Generator Function: Write a generator function that yields the Fibonacci sequence indefinitely.

  5. Context Manager: Implement a context manager that measures the execution time of a code block using the with statement.

  6. Lambda Function: Use a lambda function to sort a list of tuples based on the second element of each tuple.

  7. Map and Filter: Use map and filter functions to convert a list of strings to uppercase and filter out strings containing vowels.

  8. Partial Function Application: Use the functools.partial function to create a partial function that adds a fixed value to any number.

  9. JSON Parsing: Given a JSON string, parse it using the json module and extract specific data fields.

  10. Regular Expressions: Write a regular expression to validate email addresses.

  11. File Handling: Read data from a CSV file using the csv module and calculate the total sum of a specific column.

  12. Class Inheritance: Create a class hierarchy with a base class and multiple derived classes, demonstrating inheritance and method overriding.

  13. Exception Handling: Write code that demonstrates the use of try-except blocks to handle exceptions gracefully.

  14. Dictionary Comprehension: Use dictionary comprehension to create a dictionary mapping each word in a list to its length.

  15. Generator Expression: Use a generator expression to generate all prime numbers less than 1000.

  16. Pickle Usage: Serialize and deserialize a Python object using the pickle module.

  17. Assertions: Write assertions to validate certain conditions in a function and raise appropriate errors if the conditions are not met.

  18. Contextlib Module: Use the contextlib module to create a context manager that suppresses a specific type of exception.

  19. String Formatting: Use f-strings or the format method to format strings with placeholders for variables.

  20. Datetime Module: Write code to calculate the difference between two dates using the datetime module.

Clone this wiki locally