0Pricing
Learn AI with Python · Lesson

Tuples

Understand the benefits of immutable sequences and how to use them.

Tuples is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Tuples

Tuples are immutable sequences in Python, meaning their elements cannot be changed after creation. They are often used to group related data.

In this lesson, you’ll learn how to create and use tuples, perform operations on them, and understand their immutability.

Tuples — illustration 1

2

Creating Tuples

A tuple is created by placing elements inside parentheses (), separated by commas:

# Creating a tuple
fruits = ("apple", "banana", "cherry")
print(fruits)

3

4

Accessing Tuple Elements

Like lists, you can access tuple elements using indexing:

# Accessing tuple elements
fruits = ("apple", "banana", "cherry")
print(fruits[1])  # Outputs: banana

5

Immutability of Tuples

Tuples are immutable, meaning their elements cannot be changed after creation:

# Immutability of tuples
fruits = ("apple", "banana", "cherry")
# Uncommenting the next line will raise an error
# fruits[1] = "grape"

6

Tuple Operations

Tuples support operations like concatenation and repetition:

# Tuple operations
t1 = (1, 2, 3)
t2 = (4, 5)
print(t1 + t2)  # Outputs: (1, 2, 3, 4, 5)
print(t1 * 2)  # Outputs: (1, 2, 3, 1, 2, 3)

7

Tuple Packing and Unpacking

You can pack multiple values into a tuple and unpack them into variables:

# Tuple packing and unpacking
t = (1, 2, 3)
a, b, c = t
print(a, b, c)  # Outputs: 1 2 3

8

Using Tuples as Keys

Tuples can be used as keys in dictionaries because they are immutable:

# Using tuples as dictionary keys
locations = {(40.7128, -74.0060): "New York", (34.0522, -118.2437): "Los Angeles"}
print(locations[(40.7128, -74.0060)])  # Outputs: New York

9

10

Common Mistakes with Tuples

Here are some mistakes to avoid:

  • Trying to modify elements of a tuple (it’s immutable).
  • Confusing tuples with lists due to similar syntax.
  • Using mutable objects (like lists) inside tuples, as they can still be modified.

11

What Did We Learn?

In this lesson, you learned:

  • How to create and use tuples in Python.
  • The immutability of tuples and its implications.
  • How to use tuples in dictionary keys and other scenarios.

Great job! Let’s move to the next topic.

Tuples — illustration 11

11

What Did We Learn?

In this lesson, you learned:

  • How to create and manipulate lists in Python.
  • Common list operations and methods.
  • How to use nested lists and iterate over lists.

Great job! Let’s move to the next topic.

Tuples — illustration 12

Frequently asked questions

Is the “Tuples” lesson free?

Yes — the full text of “Tuples” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Tuples”?

Understand the benefits of immutable sequences and how to use them. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Tuples” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Lists
  2. Tuples
  3. Sets
  4. Dictionaries
  5. Strings as Data Structures
← Back to Learn AI with Python