****The journey so far: Tuples and Dictionary.
A tuple is a group of items stored using round brackets.Example
fruits = ("Apple", "Banana", "Cherry")
key facts about tuples:
1. Tuples are ordered, so items have a fixed position.
2. Tuples are unchangeable – you can’t modify items directly after creating it.
3. Tuples allow repeated values – the same item can appear more than once.
4. You can access tuple items by their index.
To mutate a tuple a concert it to list. example
temp = list(fruits)
temp[0] = "Mango"
fruits = tuple(temp)
print(fruits) # Output: ('Mango', 'Banana', 'Cherry')
Dictionary is also another data type we have in python.
A dictionary stores information in key-value pairs using curly brackets {}.
Example:book = {
"title": "Python Basics",
"pages": 250
}
Main features of dictionaries:
1. They are unordered – the item order may vary.
2. They are changeable – you can add or update values.
3. Keys must be unique – no duplicate keys are allowed
4. To access a value, you use its key:example in this code "title" and"pages" are key to "python "and 250
Dictionaries can also hold different types of data, including strings, numbers, lists, and tuples.
Dictionary methods are
1. .copy() – Creates a copy of the dictionary.example
copy_book = book.copy()
2. .get("key") – Safely gets the value of a key.example print(book.get("pages")) # Output: 250
3. popitem() – Removes the last item added. example book.popitem()
4. .pop("key") – Removes the specified key and its value. example student.pop("name")
5. .update() – Changes existing value or adds new pair. example .update() – Changes existing value or adds new pair.
Conclusion: The Journey So Far – Tuples and Dictionaries
We’ve explored two important data types in Python: tuples and dictionaries.
Tuples are ordered and unchangeable collections of items stored in round brackets (). You can access items by index, and although you can't change them directly, you can convert them to a list to make changes.
Dictionaries store data in key-value pairs using curly brackets {}. They are flexible and allow updating, adding, or removing items. Keys must be unique, and you access values using the keys.
Both data types are useful for organizing and handling data efficiently, depending on whether you need order and immutability (tuple) or key-based access and flexibility (dictionary).