Data Structures and Sequences In Python

Depending on the use case, Python offers a number of built-in data structures that are methods for efficiently organizing data for access. Sequences are among the most basic and popular data structures.
Sequence is an ordered group of items or values in a container. Sequences must have integer indexes and a specific order. The indexing begins with the first element at zero (0).
Strings, Lists, and Tuples are among the fundamental built-in sequence types in Python. To represent ordered collections, lists and tuples are specifically designated as sequence types. Character sequences are called strings.
The primary sequence covered:
Lists
In Python, lists are regarded as the most popular general-purpose data structure. They are sometimes referred to as the most straightforward sequence type to comprehend and connect to actual items. Lists are sequences that can be changed. This implies that a list’s contents can be altered after it has been formed. Several list methods, including append(), remove(), and pop(), can be used to alter a list. Lists can hold a collection of data of any size, from a few items to millions, and can include objects of several data types.
Additionally, lists are iterable, which makes it simple to navigate through their components. Data can be arranged hierarchically by using nested structures, which are created when lists contain other lists. List comprehensions provide a succinct method for making or changing lists. The.sort() method can be used to sort lists. Other data structures, such as Queues (First-In, First-Out) and Stacks (Last-In, First-Out), can also be simulated with Python lists.
Tuples
Similar to lists, tuples are object sequences. The primary distinction, though, is that tuples are unchangeable sequences. Once formed, tuples cannot be changed. Mathematics is the tuples name and standard notation, which is often a list of items enclosed in parenthesis () and separated by commas. Tuples can be used to express collections of elements that shouldn’t be changed because they are immutable. Sequence operations like indexing and slicing are also supported by tuples.
Strings
At their core, strings are collections of characters. In Python, strings are immutable, much like tuples. This implies that you are unable to directly alter individual characters within an existing string. Strings support operations based on their ordered, indexed nature, just like other sequences.
Because they are indexed and ordered, sequences perform similar operations. Among these operations are:
Indexing: Using a numerical index (position) to access specific elements. At index 0, the first element is located. Negative indices are also an option; for example, -1 denotes the last element, -2 the second-to-last, and so on.
Slicing: Slicing is the process of employing the slice operator [start:end] to extract a segment or sub-sequence from a sequence. A new sequence is produced by slicing that includes components from the beginning index up to the ending index, but excludes the latter. Other slice notations include employing negative indices and [start:end:step].
Concatenation: Concatenation is the process of utilising the + operator to join two or more sequences. A new sequence is produced by this process.
Repetition: Using the operator to repeat a sequence several times. A new sequence is also produced by this process.
Length: Using the built-in len() function, one can ascertain the number of elements in a sequence.
Membership Testing: Membership testing is the process of utilising the in and not in operators to determine whether an element is present in a sequence. Loops are frequently used to process sequences, especially the for loop. A for loop iterates through the elements in a sequence, repeating its code block for each one. For loops frequently employ the range() function to produce a numerical sequence.
Example:
# Example demonstrating Python sequences
# Create a list (mutable sequence)
# Lists are ordered collections of items
my list
print("Original list:", my list)
# Create a tuple (immutable sequence)
# Tuples are also ordered collections . Use parentheses
my tuple
print("Original tuple:", my tuple)
# Create a string (immutable sequence of characters)
# Strings are sequences . Use quotes.
my string = "Python"
print("Original string:", my string)
# Access elements using indexing (position starts at 0)
print("\nAccessing elements by index:")
print("First element of list:", my list) # Index 0
print("Third element of tuple:", my tuple[62]) # Index 2
print("Second character of string:", my string[63]) # Index 1
# Access sub-sequences using slicing
print("\nAccessing sub-sequences using slicing:")
# Slice from index 0 up to (but not including) 3
print("First three elements of list:", my list)
# Using negative indices and slicing ]
print("Last two elements of tuple:", my tuple[-2:])
# Slice from index 2 up to (but not including) 5
print("Characters from index 2 to 4 of string:", my string[2:5])
# Iterate over a sequence using a for loop
print("\nIterating over the list:")
# The for loop repeats for each element in the sequence
for item in my list:
print(item)
print("\nIterating over the string:")
for char in my string:
print(char)
# Demonstrate a sequence operation: length
print("\nLength of list:", len(my list))
print("Length of tuple:", len(my tuple))
print("Length of string:", len(my string))
# Demonstrate list mutability (can be changed)
my list. append(60) # Add an element
print("\nList after append:", my list)
# Demonstrate tuple/string immutability (cannot be changed)
print("\nTuples and strings are immutable once created.")
# Attempting to modify an element by index or append to a tuple/string
# would result in an error.
# my tuple = 99 # This line would cause a TypeError
# my string = 'p' # This line would cause a TypeError
This example demonstrates the creation of lists, tuples, and strings as well as the cross-platform compatibility of standard sequence operations like indexing and slicing. By attempting to alter them, it also demonstrates the distinction between changeable lists and immutable tuples/strings and demonstrates iteration using a for loop. Since sequences are essential building blocks for managing data collections, understanding them is a critical first step in learning Python programming.