Course Content
Python Data Structures
Python Data Structures
Creating a Tuple
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:
single_movie = ("Inception",) # Single-element tuple print(single_movie)
Note
Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.
not_a_tuple = ("Inception") print(type(not_a_tuple))
Let's explore how to create tuples.
Creating an empty tuple
empty_tuple = () print(empty_tuple)
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:
movie_title = tuple("Inception") print(movie_title)
Creating a tuple from a list
You can also use the tuple()
function to create a tuple from a list:
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
Swipe to show code editor
Create a tuple named movie_details
that stores the title, director, release year, and genre of a movie (e.g., "Inception"
, "Christopher Nolan"
, 2010
, "Science Fiction"
).
Thanks for your feedback!
Creating a Tuple
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:
single_movie = ("Inception",) # Single-element tuple print(single_movie)
Note
Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.
not_a_tuple = ("Inception") print(type(not_a_tuple))
Let's explore how to create tuples.
Creating an empty tuple
empty_tuple = () print(empty_tuple)
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:
movie_title = tuple("Inception") print(movie_title)
Creating a tuple from a list
You can also use the tuple()
function to create a tuple from a list:
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
Swipe to show code editor
Create a tuple named movie_details
that stores the title, director, release year, and genre of a movie (e.g., "Inception"
, "Christopher Nolan"
, 2010
, "Science Fiction"
).
Thanks for your feedback!
Creating a Tuple
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:
single_movie = ("Inception",) # Single-element tuple print(single_movie)
Note
Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.
not_a_tuple = ("Inception") print(type(not_a_tuple))
Let's explore how to create tuples.
Creating an empty tuple
empty_tuple = () print(empty_tuple)
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:
movie_title = tuple("Inception") print(movie_title)
Creating a tuple from a list
You can also use the tuple()
function to create a tuple from a list:
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
Swipe to show code editor
Create a tuple named movie_details
that stores the title, director, release year, and genre of a movie (e.g., "Inception"
, "Christopher Nolan"
, 2010
, "Science Fiction"
).
Thanks for your feedback!
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:
single_movie = ("Inception",) # Single-element tuple print(single_movie)
Note
Without the comma, Python will interpret the parentheses as regular grouping, not as a tuple.
not_a_tuple = ("Inception") print(type(not_a_tuple))
Let's explore how to create tuples.
Creating an empty tuple
empty_tuple = () print(empty_tuple)
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:
movie_title = tuple("Inception") print(movie_title)
Creating a tuple from a list
You can also use the tuple()
function to create a tuple from a list:
movie_list = ["Inception", "Interstellar", "Tenet"] movies_tuple = tuple(movie_list) print(movies_tuple)
Swipe to show code editor
Create a tuple named movie_details
that stores the title, director, release year, and genre of a movie (e.g., "Inception"
, "Christopher Nolan"
, 2010
, "Science Fiction"
).