Skip to content

Instantly share code, notes, and snippets.

@dawidbeno
Created August 14, 2025 09:12
Show Gist options
  • Save dawidbeno/5679b0e9682361046637d19ea894ecf9 to your computer and use it in GitHub Desktop.
Save dawidbeno/5679b0e9682361046637d19ea894ecf9 to your computer and use it in GitHub Desktop.
Python basic built-in data structures
# python_data_structures.py
#
# Description: A quick reference and example guide for the basic built-in
# data structures in Python: Lists, Tuples, Dictionaries, and Sets.
# --- 1. Lists ---
# An ordered and mutable (changeable) collection of items.
# Use cases: Storing collections of items that might need to change,
# like a to-do list or a list of users.
print("--- Lists ---")
my_tasks = ["email boss", "buy milk", "call mom"]
print(f"Initial list: {my_tasks}")
# Add an item to the end
my_tasks.append("go to gym")
print(f"After append: {my_tasks}")
# Modify an item by its index
my_tasks[0] = "draft project report"
print(f"After modification: {my_tasks}")
# Remove an item
my_tasks.remove("buy milk")
print(f"After removing 'buy milk': {my_tasks}")
print("-" * 20 + "\n")
# --- 2. Tuples ---
# An ordered and immutable (unchangeable) collection of items.
# Use cases: Protecting data that should not be changed, like RGB color
# values or geographic coordinates.
print("--- Tuples ---")
# A tuple representing a point in 3D space
point_3d = (10, 20, 5)
print(f"My tuple: {point_3d}")
# Access an item by its index
print(f"First element: {point_3d[0]}")
# Trying to change a tuple will raise a TypeError.
# The following line is commented out because it would cause an error:
# point_3d[0] = 15
print("Tuples are immutable. You cannot change them.")
print("-" * 20 + "\n")
# --- 3. Dictionaries ---
# An unordered (in Python < 3.7) and mutable collection of key-value pairs.
# Use cases: Storing data that can be accessed by a specific identifier,
# like a user's profile information. Very fast lookups.
print("--- Dictionaries ---")
user_profile = {
"username": "dev123",
"email": "[email protected]",
"level": 5
}
print(f"Initial dictionary: {user_profile}")
# Access a value by its key
print(f"User's email: {user_profile['email']}")
# Add a new key-value pair
user_profile["is_active"] = True
print(f"After adding a key: {user_profile}")
# Modify a value
user_profile["level"] = 6
print(f"After modifying a value: {user_profile}")
print("-" * 20 + "\n")
# --- 4. Sets ---
# An unordered and mutable collection of unique items.
# Use cases: Removing duplicates from a list, fast membership testing,
# and mathematical set operations (union, intersection).
print("--- Sets ---")
tags_list = ["python", "dev", "automation", "python", "dev"]
print(f"Original list with duplicates: {tags_list}")
# Convert to a set to get unique items
unique_tags = set(tags_list)
print(f"Set with unique items: {unique_tags}")
# Check for membership (this is very fast in sets)
print(f"Is 'python' in the set? {'python' in unique_tags}")
print(f"Is 'java' in the set? {'java' in unique_tags}")
# Add an item to the set
unique_tags.add("cloud")
print(f"After adding 'cloud': {unique_tags}")
print("-" * 20 + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment