Q. What is the basic syntax of Python list comprehension, and how does it differ from using a for loop to create a list? Provide an example of a list comprehension that squares the elements in a given list of integers.
numbers = [1, 2, 3, 4, 5] -iterable and variable
squares = [num**2 for num in numbers] -expression
print(squares) -output
Q. What is a decorator in Python?
Q. Explain the concept of decorators in Python. How do they work, and what are some common use cases for them? Provide an example of a simple decorator function from the reading.
In this example, we have a time function that will display Hello, world! after 1 second. The decorator function is taking in the time function, and adding a seperate function that will track the execution time of the function.
Some common uses for decorators are caching, logging actions, authorization based on user permissions. They can really be used for any function that needs a new piece added on but you dont want to change the source code.