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:
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))
Create a range from 0 to 10 with a step of 3 and print it as a list.
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)
Slice the list from index 2 to 5 with a step of 1 and print the result.
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)
Change the first element of the list to "new" and print the result.
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)
Create a range, convert to list, slice it, and modify an element.
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!