Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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.

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.
question mark

What happens if you try to modify a tuple element?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show more examples of defining tuples in Python?

What happens if I create a tuple with just one element?

When should I use a tuple instead of a list?

Awesome!

Completion rate improved to 5

bookTuples

Swipe um das Menü anzuzeigen

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.

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.
question mark

What happens if you try to modify a tuple element?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 2
some-alt