Introduction
In this lesson, we will explore the collection data types available in Python, namely Sets, Lists, and Dictionaries. These data types allow you to store multiple items in a single variable, making it easier to manipulate data and organize complex information. Understanding these collections is crucial for any aspiring Python programmer, as they serve as the foundation for handling larger datasets and performing various operations on them.
Sets
A Set is an unordered collection of unique items. This means that a set does not allow duplicate elements and is defined using curly braces or the set() function.
Characteristics
- Unordered: The items in a set have no defined order.
- Unique: Every element in a set must be unique.
- Mutable: You can add or remove items from a set after it is created.
Example
Here’s a simple example of creating a set and performing operations on it:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Adding elements
my_set.add(6)
# Trying to add a duplicate element
my_set.add(3) # This will have no effect
# Displaying the set
print(my_set)
{1, 2, 3, 4, 5, 6}
Lists
A List is an ordered collection of elements that can contain duplicate items. Lists are defined using square brackets and can store heterogeneous data types, i.e., a mix of different data types.
Characteristics
- Ordered: The items in a list maintain their order and can be accessed by their index.
- Mutable: Similar to sets, lists can be modified after creation.
- Allows duplicates: Lists can contain multiple instances of the same element.
Example
Here’s how you can create a list and manipulate it:
# Creating a list
my_list = [1, 2, 3, 4, 5, 5]
# Appending an element
my_list.append(6)
# Accessing the first element
first_element = my_list[0]
# Displaying the list and the first element
print(my_list)
print(first_element)
[1, 2, 3, 4, 5, 5, 6]
1
Dictionary
A Dictionary is an unordered collection of key-value pairs. Each key must be unique, and values can be of any data type. Dictionaries are defined using curly braces and provide a way to associate values with unique keys.
Characteristics
- Unordered: The items do not have a specific order.
- Key-value pairs: Each entry consists of a unique key and a value.
- Mutable: Similar to lists and sets, dictionaries can be modified.
Example
Here’s an example of how to work with dictionaries:
# Creating a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Accessing a value using a key
age = my_dict['age']
# Adding a new key-value pair
my_dict['job'] = 'Engineer'
# Displaying the dictionary and the age
print(my_dict)
print(age)
{'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
30
Summary
In this lesson, we covered the fundamental collection data types of Python: Sets, Lists, Arrays, and Dictionaries. Each of these data types serves a specific purpose and offers unique capabilities, allowing you to store and manipulate collections of data effectively. Understanding these collections will enable you to build more complex programs that can process multiple data points efficiently.
