Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Tuples | Data Structures
Introduction to Python with AI

bookTuples

A tuple is like a list but immutable β€” once created, its contents cannot be changed.

They're useful for grouping values that should stay fixed, such as coordinates (x, y), RGB colors (255, 0, 0), or other constant data.

Note
Note
  • How do you define a tuple in Python? Give code example.
  • Show what happens if you try to modify a tuple in Python? Give code example and explain the result.
  • How do you access items in a tuple and unpack them into variables? Give code example for both.

Defining a Tuple

You define a tuple using parentheses (()) instead of square brackets:

coordinates = (10, 20)

You can also create a tuple without parentheses: x = 1, 2, 3 β€” Python understands the comma means "tuple".

Immutability

Tuples cannot be modified β€” you can't add, remove, or change values. Trying to do so raises a TypeError.

This makes them ideal for fixed configurations, constants, or safe return values from functions.

Accessing and Unpacking Tuples

Like lists, tuples use indexes starting at 0. For example, colors[0] is "red", and colors[-1] is "blue".

Tuples also support unpacking β€” assigning values to variables in one step: x, y = (10, 20) sets x = 10 and y = 20.

This is especially useful when functions return multiple values.

Summary

  • Tuples store multiple values, like lists β€” but are immutable;
  • Use parentheses or commas to define them;
  • Access items by index β€” just like lists;
  • Once created, tuples can't be changed;
  • Use them when you need fixed, reliable data.

Try It Yourself

  1. Define a tuple with three elements;
  2. Access the second element and print it;
  3. Try to replace one value β€” and see what happens;
  4. Try to unpack your tuple.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give me more examples of when to use tuples instead of lists?

What happens if I try to change a value in a tuple?

Can you explain tuple unpacking with more examples?

Awesome!

Completion rate improved to 5

bookTuples

Swipe to show menu

A tuple is like a list but immutable β€” once created, its contents cannot be changed.

They're useful for grouping values that should stay fixed, such as coordinates (x, y), RGB colors (255, 0, 0), or other constant data.

Note
Note
  • How do you define a tuple in Python? Give code example.
  • Show what happens if you try to modify a tuple in Python? Give code example and explain the result.
  • How do you access items in a tuple and unpack them into variables? Give code example for both.

Defining a Tuple

You define a tuple using parentheses (()) instead of square brackets:

coordinates = (10, 20)

You can also create a tuple without parentheses: x = 1, 2, 3 β€” Python understands the comma means "tuple".

Immutability

Tuples cannot be modified β€” you can't add, remove, or change values. Trying to do so raises a TypeError.

This makes them ideal for fixed configurations, constants, or safe return values from functions.

Accessing and Unpacking Tuples

Like lists, tuples use indexes starting at 0. For example, colors[0] is "red", and colors[-1] is "blue".

Tuples also support unpacking β€” assigning values to variables in one step: x, y = (10, 20) sets x = 10 and y = 20.

This is especially useful when functions return multiple values.

Summary

  • Tuples store multiple values, like lists β€” but are immutable;
  • Use parentheses or commas to define them;
  • Access items by index β€” just like lists;
  • Once created, tuples can't be changed;
  • Use them when you need fixed, reliable data.

Try It Yourself

  1. Define a tuple with three elements;
  2. Access the second element and print it;
  3. Try to replace one value β€” and see what happens;
  4. Try to unpack your tuple.
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt