Skip to main content

Ranges, Sets, and Tuples

More Ways to Organize Data

· 3 min read

In the previous unit, we learned about list comprehension. Now let's explore three more data structures: ranges, sets, and tuples. Each has different properties that make it useful in specific situations.

Ranges, Sets, and Tuples

Ranges

A range generates a sequence of numbers. You've seen it in for loops. It's immutable, so once created, you can't change it.

numbers = list(range(10))
print(numbers) # Outputs: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

By default, range starts at 0 and stops before the number you specify. You can also set a start value and step size:

numbers = list(range(2, 10, 2))
print(numbers) # Outputs: [2, 4, 6, 8]

Sets

A set stores unique values in no particular order. If you add a duplicate, the set ignores it.

fruits = {"apple", "banana", "cherry"}
print(fruits) # Order may vary

Sets are useful when you need to eliminate duplicates or check membership quickly. You can add and remove items, but you can't access them by index since sets have no order.

fruits.add("orange")
fruits.remove("banana")

Tuples

A tuple looks like a list but uses parentheses and is immutable. Once you create a tuple, you can't change its contents.

fruits = ("apple", "banana", "cherry")
print(fruits[0]) # Outputs: apple

Tuples are useful for data that shouldn't change, like coordinates or configuration values. They're also slightly more memory-efficient than lists.

Comparing the Three

Lists, sets, and tuples each have their place. Lists are ordered and mutable, defined with square brackets []. Sets are unordered, mutable, and prevent duplicates, defined with curly braces {}. Tuples are ordered and immutable, defined with parentheses ().

Choose based on what you need: order, mutability, or uniqueness.

Project: Drawing with Different Data Structures

Let's use all three structures in a Turtle program. A range controls shape sizes, a set provides colors, and a tuple holds the starting position.

import turtle

screen = turtle.Screen()
screen.setup(width=800, height=600)
screen.bgcolor("white")

t = turtle.Turtle()

# Range: sizes from 50 to 130 in steps of 20
sizes = range(50, 150, 20)

# Set: unique colors (order not guaranteed)
colors = {"red", "green", "blue", "orange", "purple"}

# Tuple: fixed starting position
start_pos = (-200, 0)

t.penup()
t.goto(start_pos)
t.pendown()

for size in sizes:
t.color(colors.pop())
for _ in range(4):
t.forward(size)
t.right(90)
t.penup()
t.forward(size + 20)
t.pendown()

t.hideturtle()
turtle.done()

The range(50, 150, 20) generates sizes 50, 70, 90, 110, and 130. The colors.pop() removes and returns a random color from the set for each square. The tuple start_pos holds the x and y coordinates as a fixed pair.

Try modifying the range step, adding colors to the set, or changing the starting position to see how each data structure affects the output.

In the next unit, we'll explore dictionaries, Python's key-value data structure.