HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›Tuples & Sets

Tuples & Sets

Immutable tuples and duplicate-free sets — when to use which.

Simple enough for a 5-year-old to understand

Tuple: an immutable list

A tuple is like a list but CANNOT BE CHANGED after it's created. It's written with parentheses.

Example code
1
nokta = (3, 5)
A two-element tuple (e.g. an x, y coordinate).
DataA two-element tuple (e.g. an x, y coordinate).
2
print(nokta[0])
First element: 3
Output / InputFirst element: 3
3
print(nokta[1])
Second element: 5
Output / InputSecond element: 5
DataOutput / Input
💡 Hint: A tuple is safe for fixed data that shouldn't change (coordinates, a color code).

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which data type cannot be changed after it's created?

About this topic

Tuples and sets are two special siblings of the list. A tuple is ordered but IMMUTABLE: once created, you cannot add or remove elements. A set, on the other hand, is unordered and ACCEPTS NO DUPLICATES: if you try to add the same value twice, it only stays once.

These constraints are not limitations, they are purposeful. Immutability makes it impossible for data to be modified by accident and allows tuples to be used as dictionary keys. Uniqueness provides a one-line answer to the question "what are the distinct values in this list" and speeds up membership checks significantly.

What you will learn

  • Creating a tuple and why it cannot be modified
  • Distributing values to variables using tuple unpacking
  • Creating a set and clearing duplicates
  • Set operations: intersection, union, difference
  • When to choose a list versus a tuple or a set

When to use?

You use a tuple for values that carry meaning together and should not change: a pair of coordinates, a color triplet, two results returned from a function. Multiple return values from functions are actually tuples.

You use a set to clean duplicates from a list, find common elements between two sets, or perform fast membership checks on a large number of elements.

Common mistakes

Wrong

point = (1, 2)
point[0] = 5

Tuples are immutable; trying to update an element results in the error "TypeError: 'tuple' object does not support item assignment". If modification is needed, use a list or create a new tuple.

Correct

point = (1, 2)
point = (5, point[1])

Wrong

single = (5)
print(type(single))

When writing a single-element tuple, the comma is MANDATORY. Without the comma, parentheses merely do grouping and you are left with a number instead of a tuple; the type output shows int.

Correct

single = (5,)
print(type(single))

Wrong

empty = {}
empty.add(1)

Empty curly braces create a DICTIONARY, not a set; that is why you get "AttributeError: 'dict' object has no attribute 'add'". The set function must be used for an empty set.

Correct

empty = set()
empty.add(1)

Frequently asked questions

Why should I use a tuple instead of a list?

There are three reasons: immutability prevents accidental modifications, tuples can be dictionary keys, and tuples use slightly less memory and run a bit faster than lists. If the data is constant, use a tuple; if it will change, use a list.

Why doesn't a set preserve the order?

Set elements are stored in a hash structure that provides fast lookups; this structure does not keep track of order. If you need both uniqueness and order, you can use dictionary keys, because dictionaries preserve insertion order.

How do I clean duplicates in a list?

Converting the list to a set and back to a list is the shortest way. However, this operation breaks the order; if you need to preserve the original order, generating a list from dictionary keys is the correct method.

Summary

A tuple is ordered but immutable, a set is unordered but unique; both exist for tasks a list cannot do. The two most classic traps: forgetting the comma in a single-element tuple and empty curly braces creating a dictionary instead of a set.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code