We have recently finished the chapter "Python Functions". Now our next chapter is "Data Structures". This is an important chapter because it helps us store and manage multiple values at once.
What is Data Structure?
We have seen variables that store a single value at a time. But what should we do when we want to store multiple values? Here, data structures provide an efficient way to store more than one value in Python. There are 4 types of main built-in data structures: List, Tuple, Sets, and Dictionaries. Each one has different features and use cases.
Python List
list is an ordered and mutable data structure that allows support duplicate value. It is denoted by square brackets [ ] to store values.
Example:
numbers = [10, 20, 30, 40]
print(numbers)
If you want to access a value from a list, you must use its index number. In Python, the index number starts from 0.
numbers = [10, 20, 30, 40]
print(numbers[0])
print(numbers[2])
Add & Remove Elementsnumbers.append(50) #add value
numbers.remove(30) #remove value
Python Tuples
Tuples is an ordered and immutable data structure that allows duplicate value. It is denoted by small brackets ( ) to store values.
Example:
colors = ("red", "green", "blue")
print(colors)
If you want to access a value from a Tuples;
print(colors[0])
Adding and removing elements is not applicable in tuples because they are immutable, i.e., you cannot modify them.
Python Sets
Sets are an unordered list and a collection of unique items. This is the best suited for removing duplicate items.
numbers = (1, 1, 2, 3, 4, 4, 5) print(numbers)
Output: (1, 2, 3, 4, 5)
Adding element: numbers.add(6)
Python Dictionaries
Dictionaries store data in key-value pair. It is use curely bracket to manage element.
Quillix_tutorial = {
"website": "Quillix",
"course":"Python",
"duration":"6month"
}
To access element or value from dictionary:
print (Quillix_tutorial["course"])To add new data:
Quillix_tutorial["chapter"] = "Data Structure"
print(Quillix_tutorial)
| Data Structure | Ordered | Changeable | Duplicates |
|---|---|---|---|
| List | Yes | Yes | Yes |
| Tuple | Yes | No | Yes |
| Set | No | Yes | No |
| Dictionary | Yes | Yes | Keys unique |
Final Thoughts
Here is our next chapter: Python Data Structures. I hope you have successfully completed the previous chapter and practiced the examples as well. Please share this post with others too. Thank you for supporting and sharing our Python tutorials.
Keep reading! Our next chapter will be Python Strings.

.png)