About Lesson
Characteristics and Usage
- Tuples:
- Definition: An ordered collection of items that are immutable (cannot be changed after creation).
- Creating Tuples: python
my_tuple = (1, 2, 3)
- Accessing Elements: Use indexing similar to lists. python
print(my_tuple[0]) # Output: 1
- Usage: Useful for fixed collections of items, such as coordinates or database records.
- Sets:
- Definition: An unordered collection of unique items. Sets are mutable.
- Creating Sets: python
my_set = {1, 2, 3, 4, 5}
- Adding Elements: Use
add()
to add an element. pythonmy_set.add(6) print(my_set) # Output: {1, 2, 3, 4, 5, 6}
- Removing Elements: Use
remove()
to remove a specific element. pythonmy_set.remove(3) print(my_set) # Output: {1, 2, 4, 5, 6}
- Usage: Useful for membership testing and eliminating duplicate entries.