Ranges and Slicing - Accessing Parts of Data

Ranges and slicing let you work with sequences of data efficiently. The range() function generates sequences of numbers, while slicing extracts parts of lists and strings. We'll also explore mutability - whether data can be changed after creation.

In this lesson, you'll learn:

1. Creating Ranges with range()

range() generates a sequence of numbers. Call it with 1 argument (stop), 2 arguments (start, stop), or 3 arguments (start, stop, step). The stop value is exclusive, meaning the sequence goes up to but doesn't include that number. Converting to a list with list() shows the actual values since range creates a memory-efficient generator.

# Range with 1 argument: range(stop)
numbers = range(5)
print("Range(5):", list(numbers))
# Range with 2 arguments: range(start, stop)
numbers = range(2, 8)
print("Range(2, 8):", list(numbers))
# Range with 3 arguments: range(start, stop, step)
numbers = range(1, 10, 2)
print("Range(1, 10, 2):", list(numbers))

Exercise: Create a Range

Create a range from 0 to 10 with a step of 3 and print it as a list.

numbers = range(0, 10, 3) # TODO: Print the range as a list

2. Slicing Lists and Strings

Slicing extracts parts of lists or strings using [start:stop:step]. The start index is inclusive, stop is exclusive, and step controls the increment between elements. This syntax provides powerful ways to extract subsequences, reverse sequences, or sample every nth element.

# Basic slicing: [start:stop]
fruits = ["apple", "banana", "cherry", "date"]
slice_result = fruits[1:3]
print("Fruits[1:3]:", slice_result)
# Slicing with step: [start:stop:step]
text = "python programming"
slice_result = text[0:10:2]
print("Text[0:10:2]:", slice_result)

Exercise: Slice a List

Slice the list from index 2 to 5 with a step of 1 and print the result.

fruits = ["apple", "banana", "cherry", "date", "elderberry"] # TODO: Slice from index 2 to 5 and print

3. Mutability: Lists vs Strings

Lists are mutable (can be changed in place), but strings are immutable (cannot be changed without recreating a new instance). Attempting to modify a string creates a new string object instead. The is operator checks object identity, revealing whether variables reference the same object in memory after operations.

# Lists are mutable
my_list = [1, 2, 3]
my_list[0] = 10
print("Modified list:", my_list)
# Strings are immutable (this will error)
my_string = "hello"
# my_string[0] = "H"  # Uncomment to see error
print("Original string:", my_string)
# Using 'is' to check identity
list1 = [1, 2, 3]
list2 = list1
list1[0] = 99
print("list1:", list1, "list2:", list2, "Same object?", list1 is list2)

Exercise: Test Mutability

Change the first element of the list to "new" and print the result.

my_list = ["apple", "banana", "cherry"] # TODO: Change the first element to "new" and print

🎯 Bring It All Together: Range and Slicing Demo

Combine range(), slicing, and mutability to process a list. This example demonstrates how to generate sequences, extract portions, and modify list contents while understanding the implications of mutability.

numbers = list(range(0, 10, 2))
print("Original:", numbers)
sliced = numbers[1:4]
print("Sliced[1:4]:", sliced)
numbers[2] = 99
print("After mutation:", numbers)

Final Exercise: Range and Slice

Create a range, convert to list, slice it, and modify an element.

numbers = list(range(5)) # TODO: Print original, slice [1:3], modify index 1 to 100, print modified

📝 What You've Learned

You've learned range() with 1-3 arguments, slicing syntax parallel to range(), and mutability differences between lists and strings.

Next Steps: Apply these to more complex data processing!