This week at BlockFuse Labs, we advanced our understanding of Python data structures by learning about tuples and dictionaries. These structures are crucial for organizing and storing data in efficient and meaningful ways.
# so what is a tuple?
A tuple is an ordered, immutable (cant be changed) collection of items. Unlike lists, tuples cannot be changed (no adding or removing elements once created).some key points to note are :
* Tuples are defined using parentheses ()
* Support indexing and slicing, just like lists
* Can contain different data types and is more memory efficient than list.
except in cases where one has to change the tuple to a list, edit what needs to be altered and revert it back to a tuple agian.
example:
fruits=(mangoe,orange,banana)
note that Even a single element tuple must have a comma: item=(shoes,) # not just (shoes)
# unpacking tuples
person=(moses,20,scientist)
name,age,work=person
print(name) # moses
* Functions like len(), index(), and count() work with tuples
# Dictionary
A dictionary is an unordered, mutable collection of key-value pairs(i.e it must have a key and a value). It’s ideal for mapping and quick lookups.
```
student = {
"name": "James",
"age": 24,
"course": "Python"
}
```
here we see that "name" is the key and "James" is the value.
* Dictionary are Created using curly braces {}
* Keys must be unique and immutable (e.g., strings, numbers, tuples).
* to access the values we use square barackets [] or .get()
* you can remove keys with del or .pop() `del student[name]`
* Nesting Dictionaries
You can store dictionaries inside dictionaries:
```
school = {
"student1": {"name": "Godiya", "age": 20},
"student2": {"name": "Nuhu", "age": 22}
}
```
# Challenges Faced
some times forgetting the comma in single-element tuples led to bugs and also accidentally modifying tuple values, forgetting they’re immutable.Another was Confusing direct key access with .get(), especially when keys didn’t exist.