Python Lec 05
Python Lec 05
•
Immutability: Tuples are designed to be fixed. This ensures data integrity and
prevents accidental modifications.
Tuple Methods
• .index(x): Returns the index of the first occurrence of element x in the tuple.
Raises a ValueError if the element is not found.
• .count(x): Counts the number of times element x appears in the tuple.
• Code example:
Tuple Slicing
• Extracting sub-tuples: Similar to lists, you can extract portions of a tuple using
slicing syntax [start:end:step]. The elements from start (inclusive) to end
(exclusive) are extracted with a step of step.
• Code example:
Dictionaries
• Unordered collections: Dictionaries store key-value pairs, allowing you to
associate unique keys with corresponding values. Unlike tuples, dictionaries are
mutable, meaning you can modify their contents after creation.
• Real-life example: Phonebook entries where the name (key) is associated with
a phone number (value).
• Code example:
Code example: