Govt. Exams
Entrance Exams
Lambda functions are functions in Python. type(lambda x: x) returns <class 'function'>.
x = 5 is a class variable. It can be accessed via A.x and modified via A.x = new_value or instance modification.
Dictionary comprehension creates {0: 0, 1: 1, 2: 4} where keys are from range(3) and values are their squares.
The 'with' statement ensures the file is automatically closed when the block exits, even if an error occurs.
eval() evaluates the string as Python code following operator precedence. 2+3*4 = 2+12 = 14.
The * operator in unpacking collects remaining elements. a gets [1, 2] and b gets 3.
Sets remove duplicate elements. set([1, 2, 2, 3, 3, 3]) returns {1, 2, 3}.
Lambda creates an anonymous function. f = lambda x: x2 creates a function that returns x squared. f(5) = 52 = 25.
The * operator unpacks the list. print(*[1, 2, 3]) becomes print(1, 2, 3) which prints '1 2 3' with spaces.
int('abc') raises a ValueError. The except block catches it and prints 'error' (without quotes).