The basic Python data structures in Python include list, set, tuples, and dictionary.
Each of the data structures is unique in its own way. Data structures are “containers” that organize and group data according to type. The data structures differ based on mutability and order.
# List
# A list is an ordered collection of elements that can be of any type.
# Lists are mutable, meaning that they can be modified after they are created.
# Define a list of integers
numbers = [2, 5, 7, 1, 9, 3]
# Access the first element of the list
first_number = numbers[0]
# Access the last element of the list
last_number = numbers[-1]
# Add a new number to the end of the list
numbers.append(6)
# Remove the first element of the list
removed_number = numbers.pop(0)
# Tuple
# A tuple is an ordered collection of elements that can be of any type.
# Tuples are immutable, meaning that they cannot be modified after they are created.
# Define a tuple of strings
fruits = ("apple", "banana", "cherry")
# Access the first element of the tuple
first_fruit = fruits[0]
# Set
# A set is an unordered collection of unique elements that can be of any type.
# Sets are mutable, meaning that they can be modified after they are created.
# Define a set of integers
numbers_set = {2, 5, 7, 1, 9, 3}
# Add a new number to the set
numbers_set.add(6)
# Remove an element from the set
numbers_set.remove(7)
# Dictionary
# A dictionary is an unordered collection of key-value pairs.
# The keys must be unique and immutable, but the values can be of any type.
# Dictionaries are mutable, meaning that they can be modified after they are created.
# Define a dictionary of integers and their squares
squares = {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# Access the value associated with a key
square_of_3 = squares[3]
# Add a new key-value pair to the dictionary
squares[6] = 36
# Remove a key-value pair from the dictionary
removed_square = squares.pop(4)
# Stack
# A stack is a collection of elements that supports two main operations:
# push, which adds an element to the top of the stack, and
# pop, which removes and returns the top element from the stack.
# Stacks are typically implemented using a list.
# Define a stack of integers
stack = []
# Push some elements onto the stack
stack.append(1)
stack.append(2)
stack.append(3)
# Pop an element from the stack
popped_element = stack.pop()
# Queue
# A queue is a collection of elements that supports two main operations:
# enqueue, which adds an element to the back of the queue, and
# dequeue, which removes and returns the front element from the queue.
# Queues are typically implemented using a list or a deque.
# Define a queue of integers
queue = []
# Enqueue some elements onto the queue
queue.append(1)
queue.append(2)
queue.append(3)
# Dequeue an element from the queue
dequeued_element = queue.pop(0)
# Tree
# A tree is a hierarchical data structure consisting of nodes connected by edges.
# Each node can have any number of children, but only one parent.
# Trees are commonly used to represent hierarchical relationships.
# Define a tree of integers using nested lists
tree = [1, [2, [4, [], []], [5, [], []]], [3, [6, [], []], [7, [], []]]]