That's a fantastic question, showing you're thinking beyond the basics! Python is rich with built-in data structures, each suited for different tasks. Beyond the simple numbers and strings we've just seen, here are the core built-in data structures you'll use frequently:
-
Lists (
list):- What it is: Ordered, mutable (changeable) sequences of items. Items do not have to be of the same type.
- Analogy: A shopping list where you can add, remove, or change items. The order matters.
- Example:
my_list = [1, "hello", 3.14, True] print(my_list[0]) # Accessing elements by index: 1 my_list.append(4) # Adding an item print(my_list) # Output: [1, 'hello', 3.14, True, 4]
-
Tuples (
tuple):- What it is: Ordered, immutable (unchangeable) sequences of items. Once created, you cannot change its contents.
- Analogy: A fixed address (e.g., latitude and longitude) or a record that shouldn't be altered.
- Example:
Tuples are often used for heterogeneous data (different types) and when the sequence's order and contents are fixed.my_tuple = (10, 20, "apple") print(my_tuple[2]) # Accessing elements by index: apple # my_tuple[0] = 5 # This would cause an error because tuples are immutable
-
Dictionaries (
dict):- What it is: Unordered collections of "key-value" pairs. Each key maps to a specific value. Keys must be unique and immutable.
- Analogy: A real-world dictionary where words (keys) have definitions (values), or a phone book where names (keys) have phone numbers (values).
- Example:
my_dict = {"name": "Labby", "age": 1, "skill": "AI Assistant"} print(my_dict["name"]) # Accessing value by key: Labby my_dict["age"] = 2 # Modifying a value my_dict["city"] = "LabEx" # Adding a new key-value pair print(my_dict) # Output: {'name': 'Labby', 'age': 2, 'skill': 'AI Assistant', 'city': 'LabEx'}
-
Sets (
set):- What it is: Unordered collections of unique items. Duplicates are automatically removed.
- Analogy: A mathematical set – think of a bag of items where you only care if an item is present, not how many times it appears or in what order.
- Example:
Sets are very efficient for checking membership and performing set operations (union, intersection, etc.).my_set = {1, 2, 3, 2, 1} # Duplicates 1 and 2 are removed print(my_set) # Output: {1, 2, 3} (order might vary) my_set.add(4) # Adding an item print(my_set) # Output: {1, 2, 3, 4}
These four (List, Tuple, Dictionary, Set) are the workhorses of Python programming when it comes to organizing collections of data.
As you get more advanced, you'll also encounter:
- Strings (
str): While often thought of as a basic type, strings are intrinsically ordered sequences of characters, making them a data structure in their own right. They are immutable like tuples. - Collections Module: Python's
collectionsmodule provides specialized container datatypes likedeque,defaultdict,namedtuple,Counter, etc., for more specific use cases. - Numpy Arrays: For scientific computing and data analysis, the
numpylibrary introduces powerful array-like data structures optimized for numerical operations. - Pandas DataFrames: The
pandaslibrary (built on numpy) offers DataFrames, which are like tables or spreadsheets, incredibly popular for data manipulation.
For now, focus on understanding Lists, Tuples, Dictionaries, and Sets as they form the foundation for managing data in Python. You'll be using them constantly!
This is a great topic to explore as you continue learning Python. Keep asking these insightful questions!