[x for x in range(5)]{x for x in range(5)}[x: for x in range(5)]for x in range(5) [x][x**2 for x in range(5)]
[0, 1, 2, 3, 4][1, 4, 9, 16, 25][0, 1, 4, 9, 16][0, 2, 4, 6, 8]{x for x in range(5)}[x for x in range(5)]set(x for x in range(5))(x for x in range(5)){key: value for key, value in zip(range(5), range(5))}dict{key: value for key, value in zip(range(5), range(5))}{key, value for key, value in zip(range(5), range(5))}zip{key: value for key, value in zip(range(5), range(5))}@decorator syntax in Python?
lambda()staticmethod()setattr()property()return instead of yield.yield keyword.next() function do in Python?
(x**2 for x in range(5))[x**2 for x in range(5)]{x**2 for x in range(5)}(x for x in range(5))regexreregexpre.patternre.search() function do?
\w\d\D\bre.sub() function do in Python?
withtryfinallyexcept__enter__() method of a context manager do?
__exit__() method of a context manager do?
__init__()__call__()__new__()__metaclass__()type and overriding __new__ or __init__.object.class keyword.metaclass module.gc.collect() method is called.del keyword is used.sys.memory()gc.memory()sys.getsizeof()get.memory()| Qno | Answer |
|---|---|
| 1 | a) [x for x in range(5)] |
| 2 | b) [1, 4, 9, 16, 25] |
| 3 | a) {x for x in range(5)} |
| 4 | a) {key: value for key, value in zip(range(5), range(5))} |
| 5 | a) A function that modifies the behavior of another function. |
| 6 | d) To call a decorator function on a function. |
| 7 | b) Decorators are used to modify or enhance functions or methods without changing their actual code. |
| 8 | b) staticmethod() |
| 9 | a) A type of function that returns a sequence of values. |
| 10 | c) It yields values one at a time using the yield keyword. |
| 11 | b) It returns the next value from the generator. |
| 12 | a) (x**2 for x in range(5)) |
| 13 | b) re |
| 14 | a) It returns the first match of the pattern in the string. |
| 15 | b) \d |
| 16 | a) It replaces a matched pattern with a new string. |
| 17 | b) To automatically handle resource cleanup like file handling or network connections. |
| 18 | a) with |
| 19 | a) Initializes the context and sets up the resource. |
| 20 | a) Exits the context manager and handles exceptions. |
| 21 | a) A class that creates other classes. |
| 22 | c) __new__() |
| 23 | a) By subclassing type and overriding __new__ or __init__. |
| 24 | a) To define how classes behave and how they are instantiated. |
| 25 | a) By using reference counting and garbage collection. |
| 26 | b) To free up memory by reclaiming objects that are no longer in use. |
| 27 | b) To track the number of references to an object in memory. |
| 28 | c) Both a and b. |
| 29 | c) Using sys.getsizeof() |
| 30 | b) The object is added to the garbage collection queue for cleanup. |