← Back to Chapters

Python Tuples

? Python Tuples

⚡ Quick Overview

A tuple in Python is an ordered collection of items that is immutable, meaning it cannot be changed after creation. Tuples are written using round brackets () and are often used for data that should stay constant.

? Key Concepts

  • Ordered → items have a fixed order that will not change.
  • Immutable → you cannot add, remove, or modify items once the tuple is created.
  • Duplicates allowed → tuples can contain repeated values.
  • Mixed data types → a tuple can contain strings, numbers, booleans, etc., all together.
  • Hashable → tuples can be used as dictionary keys (if their elements are also immutable).

? Syntax and Structure

Tuples are usually defined with parentheses (), with items separated by commas:

? Basic Tuple Syntax
my_tuple = ("apple", "banana", "cherry")

Python also allows creating tuples without parentheses when there is no ambiguity: my_tuple = "apple", "banana", "cherry". Internally, it is still a tuple.

? Code Examples

? Basic Tuple Creation

This example creates a simple tuple of fruits and prints it.

? View Code Example
my_tuple = ("apple", "banana", "cherry")
print(my_tuple)

? Accessing Tuple Items

You can access tuple items using indexing. Indexing starts at 0 from the left, and negative indices start from -1 from the right.

? View Code Example
my_tuple = ("apple", "banana", "cherry")
print(my_tuple[0])   # apple
print(my_tuple[-1])  # cherry

? Finding Tuple Length

Use the len() function to get the number of items in a tuple.

? View Code Example
my_tuple = ("apple", "banana", "cherry")
print(len(my_tuple))

? Single-Item Tuples

To create a tuple with only one item, you must include a trailing comma. Without it, Python treats the value as a normal variable (like a string), not a tuple.

? View Code Example
single_tuple = ("apple",)
print(type(single_tuple))   # <class 'tuple'>

not_a_tuple = ("apple")
print(type(not_a_tuple))    # <class 'str'>

? Tuples with Different Data Types

Tuples can store values of different data types in a single collection.

? View Code Example
mixed_tuple = ("abc", 34, True, 40.5)
print(mixed_tuple)

? Output and Explanation

? What the Code Prints

  • print(my_tuple) → prints the entire tuple, e.g. ("apple", "banana", "cherry").
  • print(my_tuple[0]) → prints the first item: apple.
  • print(my_tuple[-1]) → prints the last item: cherry.
  • print(len(my_tuple)) → prints the number of items, here 3.
  • For the single-item example:
    • type(single_tuple)<class 'tuple'>
    • type(not_a_tuple)<class 'str'>
  • print(mixed_tuple) → prints all the mixed values as a tuple, e.g. ("abc", 34, True, 40.5).

? Common Use Cases

  • Storing fixed configuration values that should not change.
  • Returning multiple values from a function in a structured way.
  • Using tuples as keys in dictionaries when you need a composite key.
  • Representing records such as coordinates: (x, y), dates, etc.

? Tips & Best Practices

  • Use tuples for data that should be read-only and not modified.
  • Remember to add a trailing comma when creating a single-item tuple: ("apple",).
  • Use tuples as dictionary keys when the key needs to be a combination of multiple immutable values.
  • Choose lists ([]) when you need to frequently add, remove, or update items.
  • Parentheses () can be optional when defining tuples, but using them improves readability.

? Try It Yourself

  • Create a tuple with 5 numbers and print the third number using indexing.
  • Make a tuple with different data types and print its length using len().
  • Create a single-item tuple and verify its type with type().
  • Write code that tries to change a tuple item and observe the TypeError that occurs.