Tuples, Sets, and Dictionaries in Python
In the previous lesson, we learned about Lists and how they are used to store multiple items in a single variable. Today, we will learn about three important Python data structures:
- Tuples
- Sets
- Dictionaries
These data structures help programmers organize and manage data efficiently in different situations.
1. Tuples in Python
A Tuple is a collection of items stored in a single variable.
Tuples are:
- Ordered
- Unchangeable (Immutable)
- Allow duplicate values
Tuples are created using parentheses "()".
Example
languages = ("Python", "Java", "C++")
print(languages)
Output
('Python', 'Java', 'C++')
Accessing Tuple Items
Tuple items are accessed using indexes.
Example
languages = ("Python", "Java", "C++")
print(languages[0])
print(languages[1])
Output
Python
Java
Negative Indexing in Tuples
Example
languages = ("Python", "Java", "C++")
print(languages[-1])
Output
C++
Tuple Length
The "len()" function returns the number of items in a tuple.
Example
numbers = (10, 20, 30)
print(len(numbers))
Output
3
Why Tuples are Important
Tuples are useful when data should not be modified accidentally.
They are commonly used for:
- Fixed data
- Coordinates
- Database records
- Returning multiple values from functions
2. Sets in Python
A Set is a collection of unique items.
Sets are:
- Unordered
- Unchangeable items
- Do not allow duplicates
Sets are created using curly brackets "{}".
Example
numbers = {1, 2, 3, 4}
print(numbers)
Output
{1, 2, 3, 4}
Duplicate Values in Sets
Sets automatically remove duplicate values.
Example
numbers = {1, 2, 2, 3, 4}
print(numbers)
Output
{1, 2, 3, 4}
Adding Items to a Set
The "add()" method inserts a new item into a set.
Example
numbers = {1, 2, 3}
numbers.add(4)
print(numbers)
Output
{1, 2, 3, 4}
Removing Items from a Set
The "remove()" method removes an item from a set.
Example
numbers = {1, 2, 3, 4}
numbers.remove(2)
print(numbers)
Output
{1, 3, 4}
Looping Through a Set
Example
languages = {"Python", "Java", "C++"}
for language in languages:
print(language)
Why Sets are Important
Sets are useful for:
- Removing duplicates
- Membership testing
- Mathematical set operations
- Data filtering
3. Dictionaries in Python
A Dictionary stores data in key-value pairs.
Dictionaries are:
- Ordered
- Changeable
- Do not allow duplicate keys
Dictionaries are created using curly brackets "{}".
Example
student = {
"name": "Augustine",
"age": 21,
"course": "Python"
}
print(student)
Output
{'name': 'Augustine', 'age': 21, 'course': 'Python'}
Accessing Dictionary Values
Values are accessed using keys.
Example
student = {
"name": "Augustine",
"age": 21
}
print(student["name"])
Output
Augustine
Changing Dictionary Values
Example
student = {
"name": "Augustine",
"age": 21
}
student["age"] = 22
print(student)
Output
{'name': 'Augustine', 'age': 22}
Adding Items to a Dictionary
Example
student = {
"name": "Augustine"
}
student["course"] = "Python"
print(student)
Output
{'name': 'Augustine', 'course': 'Python'}
Removing Items from a Dictionary
The "pop()" method removes an item using its key.
Example
student = {
"name": "Augustine",
"age": 21,
"course": "Python"
}
student.pop("age")
print(student)
Output
{'name': 'Augustine', 'course': 'Python'}
Looping Through a Dictionary
Dictionaries can be traversed using loops.
Example
student = {
"name": "Augustine",
"age": 21,
"course": "Python"
}
for key, value in student.items():
print(key, ":", value)
Output
name : Augustine
age : 21
course : Python
Dictionary Methods
Some commonly used dictionary methods include:
Method| Purpose
"keys()"| Returns all keys
"values()"| Returns all values
"items()"| Returns key-value pairs
"get()"| Returns the value of a key
"pop()"| Removes a specified key
Example
student = {
"name": "Augustine",
"age": 21
}
print(student.keys())
print(student.values())
Output
dict_keys(['name', 'age'])
dict_values(['Augustine', 21])
Why Dictionaries are Important
Dictionaries are useful for:
- Storing related information
- Fast data retrieval
- Organizing records
- Managing structured data
They are widely used in databases, APIs, and real-world applications.
Comparing Tuples, Sets, and Dictionaries
Feature| Tuple| Set| Dictionary
Ordered| Yes| No| Yes
Changeable| No| Yes| Yes
Allows Duplicates| Yes| No| Keys: No
Uses Indexes| Yes| No| No
Stores Key-Value Pairs| No| No| Yes
Conclusion
Tuples, Sets, and Dictionaries are powerful data structures in Python that help programmers organize and manage data efficiently.
- Use Tuples when data should not change.
- Use Sets when you need unique values.
- Use Dictionaries when working with key-value pairs.
Understanding these data structures is essential because they are heavily used in Python applications, web development, data science, automation, and software development.
Top comments (0)