🟢 Beginner  ·  Lesson 13

Tuples and Sets in Python

Tuples and Sets

Besides lists, Python has two more collections: tuple (ordered but cannot be changed) and set (unordered, only unique items). Each has its own job.

Tuple — Ordered & Fixed

A tuple uses round brackets ( ). It is immutable — once created, items cannot be changed. Use it for data that should not change, like coordinates or fixed config.

Python – tuple.py
point = (10, 20)
print(point[0])      # access like a list
print(len(point))
# point[0] = 5  ->  ERROR: tuples cannot change
10 2

Set — Unique & Unordered

A set uses curly braces { } and automatically removes duplicates. It has no order, so no indexing.

Python – set.py
nums = {1, 2, 2, 3, 3, 3}
print(nums)          # duplicates gone
nums.add(4)
print(nums)
{1, 2, 3} {1, 2, 3, 4}

Program 1: Tuple Basics

Python – tbasics.py
colors = ("red", "green", "blue")
for c in colors:
    print(c)
print("Total:", len(colors))
red green blue Total: 3

Program 2: Returning Multiple Values

Tuples are great for returning more than one value from a function.

Python – minmax.py
def min_max(nums):
    return min(nums), max(nums)   # returns a tuple

low, high = min_max([5, 2, 9, 1])
print("Low:", low, "High:", high)
Low: 1 High: 9

The function returns two values as a tuple; we unpack them into low and high.

Program 3: Remove Duplicates with Set

Python – dedupe.py
marks = [85, 90, 85, 78, 90, 100]
unique = set(marks)
print("Unique marks:", unique)
print("How many unique:", len(unique))
Unique marks: {100, 85, 90, 78} How many unique: 4

Converting a list to a set instantly removes repeated values.

Program 4: Set Operations

Python – setops.py
maths = {"Aman", "Riya", "Karan"}
science = {"Riya", "Karan", "Sara"}

print("Both subjects :", maths & science)   # intersection
print("Any subject   :", maths | science)   # union
print("Only maths    :", maths - science)   # difference
Both subjects : {'Riya', 'Karan'} Any subject : {'Aman', 'Riya', 'Karan', 'Sara'} Only maths : {'Aman'}
  • & = in both (intersection).
  • | = in either (union).
  • - = in first but not second (difference).

List vs Tuple vs Set

FeatureList [ ]Tuple ( )Set { }
OrderedYesYesNo
ChangeableYesNoYes (add/remove)
DuplicatesAllowedAllowedNot allowed
IndexingYesYesNo

Common Mistakes

  • Trying to change a tuple item (it is immutable).
  • Expecting a set to keep order or allow indexing.
  • Writing a 1-item tuple as (5) — it must be (5,) with a comma.

Practice Tasks

  1. Store the days of the week in a tuple and print each.
  2. Take a list with duplicates and print only unique values.
  3. Find common elements between two lists using sets.
  4. Write a function that returns both sum and average as a tuple.

Summary

  • Tuple: ordered, immutable, allows duplicates — use ( ).
  • Set: unordered, unique items, no indexing — use { }.
  • Set operations: & intersection, | union, - difference.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.