Python
Chapter 3:
Data Structures and Collections
Introduction
3.1 Lists: Versatile Collections
Lists are one of the most commonly used data structures in Python. They are ordered, mutable (modifiable), and can contain elements of different data types. Lists are denoted by square brackets [ ] and can store elements such as numbers, strings, or even other lists. Let's explore some list operations:
3.1.1 Creating Lists
Code
```python
# Creating an empty list
empty_list = []
# Creating a list with elements
fruits = ["apple", "banana", "orange"]
```
3.1.2 Accessing Elements
Code
```python
# Accessing elements using indexing
first_fruit = fruits[0]
second_fruit = fruits[1]
```
3.1.3 Modifying Lists
code
```python
# Modifying elements
fruits[0] = "pear"
# Appending elements to the end of the list
fruits.append("grape")
# Extending the list with another list
more_fruits = ["cherry", "mango"]
fruits.extend(more_fruits)
```
3.1.4 List Slicing
Code
```python
# Slicing lists to get a subset of elements
sliced_fruits = fruits[1:4] # Returns ['banana', 'orange', 'grape']
```
3.2 Tuples: Immutable Collections
Tuples are similar to lists, but with one key difference – they are immutable, meaning their elements cannot be changed after creation. Tuples are denoted by parentheses ( ) and are commonly used for fixed collections of related items, such as coordinates or RGB color codes.
Code
```python
# Creating a tuple
coordinates = (10, 20)
```
3.3 Sets: Unordered Unique Elements
Sets are unordered collections of unique elements, which means they don't allow duplicates. Sets are denoted by curly braces { }. They are useful for tasks that involve membership tests and eliminating duplicates.
Code
```python
# Creating a set
fruits_set = {"apple", "banana", "orange", "apple"}
# The set will only contain unique elements: {'apple', 'banana', 'orange'}
```
3.4 Dictionaries: Key-Value Pairs
Dictionaries are collections of key-value pairs, allowing us to store and retrieve data based on unique keys. Dictionaries are denoted by curly braces { } with key-value pairs separated by colons.
Code
```python
# Creating a dictionary
person = {"name": "Alice", "age": 30, "city": "New York"}
```
3.4.1 Accessing and Modifying Dictionary Elements
Code
```python
# Accessing dictionary values using keys
person_name = person["name"]
# Modifying dictionary values
person["age"] = 31
```
3.4.2 Dictionary Methods
Code
```python
# Getting all keys and values as lists
keys_list = list(person.keys())
values_list = list(person.values())
# Checking if a key exists in the dictionary
if "name" in person:
print("Name exists in the dictionary.")
```
Conclusion
In this chapter, we explored various data structures and collections available in Python, including lists, tuples, sets, and dictionaries. These powerful tools empower us to organize and manipulate data effectively, making our programs more efficient and maintainable. As you continue your Python journey, remember to choose the appropriate data structure for each task to maximize performance and readability. In the next chapter, we will discover Python's file handling capabilities, allowing us to interact with external files and handle data input/output operations. Keep coding and exploring the vast possibilities that Python offers!
0 Comments