What is the purpose of collections in Python?
To provide specialized container data types
Common imports include defaultdict, Counter, and deque.
What does the function heapq.heappush do?
Adds an element to the heap
It maintains the heap property.
How do you convert a string to a list of characters in Python?
chars = list(s)
This operation splits the string into its individual characters.
What is the syntax to join a list of characters back into a string?
s = ‘‘.join(chars)
This combines the characters into a single string.
What method would you use to split a string into words?
s.split() or s.split(‘,’)
This separates the string at whitespace or commas.
How do you strip whitespace from a string?
s.strip()
This removes leading and trailing whitespace.
What is the syntax to copy a list in Python?
new_list = old_list[:] or new_list = old_list.copy()
Both methods create a shallow copy of the list.
How do you slice a list to exclude the last element?
arr[:-1]
This returns all elements except the last one.
What is the syntax to reverse a list?
arr[::-1]
This creates a new list that is the reverse of the original.
How do you sort a list in place?
arr.sort()
This modifies the original list to be in sorted order.
What is the function of sorted() in Python?
Returns a new sorted list
It does not modify the original list.
How do you sort a list with a custom key?
sorted(arr, key=lambda x: x[0], reverse=True)
This sorts the list based on the first element of each item.
What does the method d.get(key, default_value) do?
Returns the value for key or default_value if key is not found
This prevents KeyError exceptions.
What does d.setdefault(key, default_value) do?
Sets default_value for key if key is not already in the dictionary
This is useful for initializing dictionary entries.
How do you iterate over a dictionary in Python?
for key, value in d.items():
This allows access to both keys and values.
What does enumerate(arr) return?
(index, value) pairs
This is useful for getting the index along with the value.
What does any(condition for x in arr) return?
True if any element matches the condition
This checks for at least one True condition in the iterable.
What does all(condition for x in arr) return?
True if all elements match the condition
This checks if every element satisfies the condition.
What is the syntax for integer division in Python?
a // b
This rounds down the result to the nearest whole number.
What does float(‘inf’) represent?
Positive infinity
This is used to represent an unbounded upper limit.
What does float(‘-inf’) represent?
Negative infinity
This is used to represent an unbounded lower limit.
What does range(n) produce?
0 to n-1
This generates a sequence of numbers starting from 0 up to n-1.
What is the syntax for creating a custom step in a range?
range(start, end, step)
This allows you to specify the increment between numbers.