Skip to ContentGo to accessibility page

3.5 Tuple basics

Learning objectives

By the end of this section you should be able to

  • Describe the features and benefits of a tuple.
  • Develop a program that creates and uses a tuple successfully.
  • Identify and discuss the mutability of a tuple.

Creating tuples and accessing elements

A tuple is a sequence of comma separated values that can contain elements of different types. A tuple must be created with commas between values, and conventionally the sequence is surrounded by parentheses. Each element is accessed by index, starting with the first element at index 0.

tuple_1 = (2, 3, 4)
print(f'tuple_1: {tuple_1}')
print(tuple_1[1])
print()
data_13 = ('Aimee Perry', 96, [94, 100, 97, 93])
print(f'data_13: {data_13}')
print(data_13[2])
  
tuple_1: (2, 3, 4)
3

data_13: ('Aimee Perry', 96, [94, 100, 97, 93])
[94, 100, 97, 93]
Table 3.4 Example tuples.

Concepts in Practice

Creating tuples and accessing elements

1.
Consider the example above. Which accesses tuple_1's first element?
  1. tuple_1[0]
  2. tuple_1[1]
  3. tuple_1[2]
2.
Which creates a valid tuple?
  1. tuple_2 = (42, 26, 13)
  2. tuple_3 = (42, 26, 13.5)
  3. both
3.
Which creates a tuple with three elements?
  1. my_tuple = 'a', 'b', 'c'
  2. my_tuple = ['a', 'b', 'c']
  3. my_tuple = ('a', 'b', 'c')

Tuple properties

How do tuples compare to lists? Tuples are ordered and allow duplicates, like lists, but have different mutability. An immutable object cannot be modified after creation. A mutable object can be modified after creation. Tuples are immutable, whereas lists are mutable.

Checkpoint

Mutability of a tuple vs. a list

Mutability of a tuple vs. a list; ch 3, video 6

Concepts in Practice

Using tuples

4.
Consider the final program in the example above. What is the value of my_tuple?
  1. (0.693, 0.414, 3.142)
  2. (0.693, 1.414, 3.142)
  3. Error
5.
Why does the following code produce an error?
tuple_1 = ('alpha', 'bravo', 'charlie')
tuple_1.append('charlie')
  1. Append isn't allowed.
  2. Duplicates aren't allowed.
  3. Strings aren't allowed
6.
A programmer wants to create a sequence of constants for easy reference that can't be changed anywhere in the program. Which sequence would be the most appropriate?
  1. list
  2. tuple

Mutability and performance

Tuples are immutable and have a fixed size, so tuples use less memory. Overall, tuples are faster to create and access, resulting in better performance that can be noticeable with large amounts of data.

Try It

Creating a tuple from a list

Suppose a programmer wants to create a tuple from a list to prevent future changes. The tuple() function creates a tuple from an object like a list. Ex: my_tuple = tuple(my_list) creates a tuple from the list my_list. Update the program below to create a tuple final_grades from the list grades.

Try It

Creating a tuple with user input

Write a program that reads in two strings and two integers from input and creates a tuple, my_data, with the four values.

Given input:

    x
    y
    15
    20

The output is:

    my_data: ('x', 'y', 15, 20)
Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.