Govt. Exams
Entrance Exams
any() returns True if at least one element in the iterable is True.
List comprehension: i**2 for i in [0,1,2,3] produces [0, 1, 4, 9].
dict.items() returns key-value pairs as tuples, converted to a list: [('a', 1), ('b', 2)].
int('10.5') raises TypeError because '10.5' is a string representation of a float, not a valid integer string.
The | operator performs set union, combining all unique elements from both sets. Result is {1, 2, 3, 4, 5}.
x = [1, 2, 3]
y = x[::-1]
print(y)
What will be the output?
The slice notation [::-1] reverses the list. [1, 2, 3] reversed is [3, 2, 1].
A list with 3 elements has valid indices 0, 1, 2. Index 3 is out of range and raises IndexError.
x = [1, 2, 3]
y = x
x.append(4)
print(y)
y = x creates a reference to the same list, not a copy. When x is modified, y reflects the changes.
The operator is right-associative. So 2 3 2 = 2 (3 2) = 2 9 = 512.
x = [1, 2, 3]
x.append([4, 5])
print(len(x))
append() adds the entire list [4, 5] as a single element. So x becomes [1, 2, 3, [4, 5]] with length 4.