Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Creating a Tuple in Python: Defining Immutable Data Structures | Mastering Python Tuples
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Creating a Tuple in Python: Defining Immutable Data Structures

In Python, a tuple is a data structure that consists of a sequence of values enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
single_movie = ("Inception",) # Single-element tuple print(single_movie)
copy

Note

Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.

12
not_a_tuple = ("Inception") print(type(not_a_tuple))
copy

Let's explore how to create tuples.

Creating an empty tuple

12
empty_tuple = () print(empty_tuple)
copy

Creating a tuple using the tuple() function

The tuple() function converts an iterable into a tuple. For example, you can convert a string into a tuple of its characters:

12
movie_title = tuple("Inception") print(movie_title)
copy

Creating a tuple from a list

You can also use the tuple() function to create a tuple from a list:

123
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
copy
Task

Swipe to start coding

Your goal is to initialize the tuple space_movies.

  • Use the following movies:
    '2001: A Space Odyssey', 'Interstellar', 'Star Wars: Episode IV - A New Hope', 'Gravity', 'The Martian'
  • To initialize the tuple, use parentheses ().
  • Tuples are immutable, meaning you cannot add elements to them after they are created. Make sure to include all the movies in the tuple during initialization.

Note

It's better to copy and paste the list of movies to avoid mistakes in the titles.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
toggle bottom row

book
Creating a Tuple in Python: Defining Immutable Data Structures

In Python, a tuple is a data structure that consists of a sequence of values enclosed in parentheses, with elements separated by commas. Tuples are similar to lists, but the key distinction is that tuples are immutable data structures.

Immutable data structures can't be modified after they're created. Tuples, just like lists, can hold any number of elements, and the data type of each element can vary. It's crucial to note that a tuple with only one element is defined with a comma following the element, as shown:

12
single_movie = ("Inception",) # Single-element tuple print(single_movie)
copy

Note

Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.

12
not_a_tuple = ("Inception") print(type(not_a_tuple))
copy

Let's explore how to create tuples.

Creating an empty tuple

12
empty_tuple = () print(empty_tuple)
copy

Creating a tuple using the tuple() function

The tuple() function converts an iterable into a tuple. For example, you can convert a string into a tuple of its characters:

12
movie_title = tuple("Inception") print(movie_title)
copy

Creating a tuple from a list

You can also use the tuple() function to create a tuple from a list:

123
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
copy
Task

Swipe to start coding

Your goal is to initialize the tuple space_movies.

  • Use the following movies:
    '2001: A Space Odyssey', 'Interstellar', 'Star Wars: Episode IV - A New Hope', 'Gravity', 'The Martian'
  • To initialize the tuple, use parentheses ().
  • Tuples are immutable, meaning you cannot add elements to them after they are created. Make sure to include all the movies in the tuple during initialization.

Note

It's better to copy and paste the list of movies to avoid mistakes in the titles.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt