### **SOFTWARE ENGINEERING AT BLOCKFUSE LABS: WEEK 8**
In week 8 of the program, we progressed into learning about more advanced data types in Python — **tuples** and **dictionaries**. These data types are powerful for storing and managing structured data, and they offer a variety of built-in methods and operations. This article will introduce you to both concepts, how they are used, and some of the important methods associated with them.
---
### **WEEK 8 RECAP: COURSES/TOPICS**
---
#### **TUPLES**
A **tuple** is a collection data type in Python that is very similar to a list. However, unlike lists, **tuples are immutable**, meaning their contents cannot be changed after creation. Tuples are created using parentheses `()` and are commonly used for grouping related data that should not be modified.
**Characteristics of Tuples:**
* Ordered
* Immutable
* Allow duplicate values
* Can store elements of different data types
**Example:**
```python
person = ("John", 28, "Engineer")
```
This creates a tuple with three elements: a string, an integer, and another string.
---
#### **Tuple Methods:**
Although tuples are immutable, Python provides a few useful methods:
* `count(value)`: Returns the number of times a specified value appears in the tuple.
* `index(value)`: Returns the index of the first occurrence of the specified value.
**Example:**
```python
numbers = (1, 2, 3, 2, 4, 2)
print(numbers.count(2)) # Output: 3
print(numbers.index(3)) # Output: 2
```
You can also access elements in a tuple using **indexing** and **slicing**, similar to lists.
---
#### **DICTIONARIES**
A **dictionary** is an unordered, mutable collection of key-value pairs. Each value in a dictionary is accessed using its corresponding key. Dictionaries are created using curly braces `{}` with key-value pairs separated by colons.
**Characteristics of Dictionaries:**
* Unordered (prior to Python 3.7)
* Mutable
* Keys must be unique and immutable
* Values can be of any type
**Example:**
```python
student = {
"name": "Alice",
"age": 22,
"major": "Computer Science"
}
```
---
#### **Dictionary Methods:**
Dictionaries come with several built-in methods to manipulate and retrieve data:
* `get(key)`: Returns the value for the specified key.
* `keys()`: Returns a view object of all the keys.
* `values()`: Returns a view object of all the values.
* `items()`: Returns a view object of key-value pairs (tuples).
* `update({key: value})`: Updates the dictionary with the specified key-value pair(s).
* `pop(key)`: Removes the item with the specified key.
**Example:**
```python
student = {"name": "Alice", "age": 22}
print(student.get("name")) # Output: Alice
student.update({"age": 23}) # Updates age
student["major"] = "Engineering" # Adds new key-value pair
print(student.keys()) # Output: dict_keys(['name', 'age', 'major'])
```
---
### **CONCLUSION**
Understanding tuples and dictionaries is essential for managing structured data in Python. Tuples are useful when dealing with fixed collections, while dictionaries shine when data is better represented as key-value pairs.