What does it mean that Python strings are immutable?
A string is an immutable sequence of Unicode characters, so operations that seem to modify it return a new string.
Study 5 Collections and String Processing with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What does it mean that Python strings are immutable?
A string is an immutable sequence of Unicode characters, so operations that seem to modify it return a new string.
How does Python index sequence elements?
Python uses zero-based indexing: the first element is at index 0, and index -1 refers to the final element.
What do the parts of a Python slice mean?
In sequence[start:stop:step], start is included, stop is excluded, and step controls how far the index advances.
How do append() and extend() differ?
append() adds one item to the end; extend() adds every item from an iterable.
What is a Python tuple?
A tuple is an ordered, immutable sequence, making it suitable for fixed groups of related values.
How can you safely read a possibly missing dictionary key?
A dictionary subscription for a missing key raises KeyError; get() can return None or a supplied default instead.
What are the defining properties of a Python set?
A set stores distinct, hashable values and does not support indexing or slicing.
When should you use discard() instead of remove()?
remove() raises an error if the value is absent; discard() does nothing when the value is absent.
What does enumerate() provide during iteration?
enumerate() supplies both the index and the value while iterating, such as for index, color in enumerate(colors).
What is zip() used for?
zip() pairs corresponding elements from multiple iterables, allowing loops such as for name, age in zip(names, ages).
What is the general form of a list comprehension?
A list comprehension follows [expression for item in iterable if condition], with the if clause optional.
How does a dictionary comprehension construct entries?
A dictionary comprehension uses a key–value expression, such as {number: number ** 2 for number in range(5)}.