Python Tuples and Sets

Python 8 min min read Updated: Mar 09, 2026 Intermediate
Python Tuples and Sets
Intermediate Topic 3 of 10

Tuples & Sets in Python

Python provides several built-in data structures that allow developers to store and manage collections of data efficiently. Two important data structures are tuples and sets. While both are used to store multiple values, they have different characteristics and use cases.

In this tutorial, we will learn about tuples and sets in Python, understand their features, and explore how they are used in real-world programming.

What is a Tuple in Python?

A tuple is an ordered collection of items that cannot be modified after creation. Unlike lists, tuples are immutable, which means their values cannot be changed once they are defined.

Tuples are created using parentheses ().

python numbers = (10, 20, 30) print(numbers)

In this example, the variable numbers contains a tuple with three elements.

Accessing Tuple Elements

Tuple elements can be accessed using index numbers, just like lists.

python fruits = ("apple", "banana", "mango") print(fruits[0]) print(fruits[1])

The index starts from 0, so the first element is accessed using fruits[0].

Tuple Immutability

Tuples cannot be modified after creation. Attempting to change a value will produce an error.

python numbers = (1, 2, 3) # This will cause an error numbers[1] = 10

This immutability makes tuples useful for storing constant data.

Tuple Packing and Unpacking

Python allows packing multiple values into a tuple and unpacking them into variables.

python person = ("Alice", 25, "Engineer") name, age, profession = person print(name) print(age) print(profession)

This process is called tuple unpacking.

Common Tuple Methods

Method Description
count() Returns the number of occurrences of a value
index() Returns the index of a specific value
python numbers = (1, 2, 2, 3) print(numbers.count(2)) print(numbers.index(3))

What is a Set in Python?

A set is an unordered collection of unique elements. Unlike lists and tuples, sets do not allow duplicate values.

Sets are defined using curly braces {}.

python numbers = {1, 2, 3, 4} print(numbers)

If duplicate values are added, Python automatically removes them.

python numbers = {1, 2, 2, 3, 3, 4} print(numbers)

The output will only contain unique values.

Adding Elements to a Set

Sets allow adding new elements using the add() method.

python numbers = {1, 2, 3} numbers.add(4) print(numbers)

Removing Elements from a Set

You can remove elements from a set using the remove() or discard() methods.

python numbers = {1, 2, 3, 4} numbers.remove(3) print(numbers)

Set Operations

Sets support mathematical operations such as union, intersection, and difference.

Union

python set1 = {1, 2, 3} set2 = {3, 4, 5} print(set1 | set2)

Intersection

python print(set1 & set2)

Difference

python print(set1 - set2)

Looping Through a Set

Although sets are unordered, you can still iterate through them using loops.

python colors = {"red", "blue", "green"} for color in colors: print(color)

Difference Between Tuple and Set

Feature Tuple Set
Order Ordered Unordered
Duplicates Allowed Not Allowed
Mutability Immutable Mutable
Syntax () {}

Real-World Example

python students = ("Rahul", "Amit", "Priya") subjects = {"Math", "Science", "English"} print(students) print(subjects)

This example shows how tuples can store fixed data while sets can manage unique items.

Conclusion

Tuples and sets are useful data structures in Python that help organize and manage collections of data efficiently. Tuples are best used when data should remain constant, while sets are ideal when you need unique elements and mathematical set operations.

Understanding these structures allows developers to write cleaner, more efficient Python programs.

In the next tutorial, we will learn about Dictionaries in Python and how to store data using key-value pairs.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators