Python Sequences: Lists, Tuples, Strings

Specifically, we’ll dive into lists and tuples (because who doesn’t love a good pun?).

To kick things off, let’s tackle lists. Lists are like your favorite snack food: they can contain anything you want! Whether it’s chips, candy, or both (we don’t judge), the beauty of Python lists is that they allow us to store any type of data in a single variable.

Here’s an example:

# Define a variable called "my_list" and assign it a list containing three elements: an integer, a string, and a boolean value
my_list = [1, "two", True]

# Print the contents of the list to the console
print(my_list) # Output: [1, 'two', True]

Notice how we can mix and match different types of data in a list? That’s the power of Python!

Now tuples. Tuples are like lists, but with a twist they’re immutable (meaning you can’t change them once created). This might seem limiting at first, but it actually has some benefits: for one, it makes tuple operations faster than list operations because there’s no need to allocate memory for new data.

Here’s an example of creating a tuple:

# Creating a tuple with three elements: an integer, a string, and a boolean
my_tuple = (1, "two", True)

# Printing the tuple
print(my_tuple) # Output: (1, 'two', True)

Notice how we enclose the values in parentheses instead of square brackets? That’s what sets tuples apart from lists.

But wait! What if you want to modify a list or tuple after creating it? Let’s say you have a list like this:

# Creating a list with three elements
my_list = [1, "two", True]

# Printing the list
print(my_list) # Output: [1, 'two', True]

# Creating a tuple with three elements
my_tuple = (1, "two", True)

# Printing the tuple
print(my_tuple) # Output: (1, 'two', True)

# Modifying the list by adding a new element
my_list.append("three")

# Printing the modified list
print(my_list) # Output: [1, 'two', True, 'three']

# Modifying the tuple by creating a new tuple with additional elements
my_tuple = my_tuple + (3, "four")

# Printing the modified tuple
print(my_tuple) # Output: (1, 'two', True, 3, 'four')

You can replace a single value in the list by indexing and simple assignment:

# This script replaces a single value in a list and prints the updated list.

# Create a list with three elements
my_list = [1, False, True]

# Replace the second element (index 1) with the value 42
my_list[1] = 42

# Print the updated list
print(my_list) # Output: [1, 42, True]

Or you can delete an element from the list using the del command:

# This script deletes an element from a list and prints the updated list.

# Create a list with elements 1 and True
my_list = [1, True]

# Delete the element at index 1 from the list
del my_list[1]

# Print the updated list
print(my_list) # Output: [1]

But what if you want to change several contiguous elements in a list at one time? Let’s say you have this list:

# Define a list with 5 elements
my_list = [1, 2, 3, 4, 5]

# Print the list to show its current elements
print(my_list) # Output: [1, 2, 3, 4, 5]

# Change the elements at index 1, 2, and 3 to new values
my_list[1:4] = [6, 7, 8]

# Print the updated list
print(my_list) # Output: [1, 6, 7, 8, 5]

# The code above uses list slicing to change multiple elements at once.
# The first number in the square brackets represents the starting index,
# while the second number represents the ending index (not inclusive).
# The third number, if included, represents the step size.
# In this case, we are starting at index 1, ending at index 4 (not inclusive),
# and replacing those elements with the new values 6, 7, and 8.
# This results in the elements at index 1, 2, and 3 being changed to 6, 7, and 8 respectively.

You can replace several contiguous elements by slicing and assignment:

# This script demonstrates how to replace several contiguous elements in a list using slicing and assignment.

# Create a list with some elements
my_list = [1, 2, 3, 4, 5]

# Use slicing to select elements 1 to 4 (excluding 4) and replace them with 42 and "hello"
my_list[1:4] = [42, "hello"]

# Print the updated list
print(my_list) # Output: [1, 42, 'hello', 5]

# The first line creates a list with some elements
my_list = [1, 2, 3, 4, 5]

# The second line uses slicing to select elements 1 to 4 (excluding 4) and replaces them with 42 and "hello"
my_list[1:4] = [42, "hello"]

# The third line prints the updated list
print(my_list) # Output: [1, 42, 'hello', 5]

Or you can delete several contiguous elements using slicing with the del command:

# This script deletes elements from a list using slicing and the del command

# Create a list with elements
my_list = [1, 2, 3, 4, 5, 'hello', 'world']

# Use slicing to select elements 1 to 3 (excluding 3) and delete them from the list
del my_list[1:3] # del command deletes elements from a list

# Print the updated list
print(my_list) # Output: [1, 4, 5, 'hello', 'world']

Lists are like your favorite snack food, tuples are like books (but without changing pages), and strings are like your favorite book that you can eat!

SICORPS