Course Content
Introduction to Python (copy)
Introduction to Python (copy)
Tuple Operations
While tuples themselves cannot be altered, Python provides operations to create and combine them effectively.
Creation
The tuple()
function creates a tuple from an iterable object (strings, sets, lists), allowing for the conversion of lists or other iterables into tuples.
Concatenation
You can join two or more tuples into a new tuple using the +
operator, allowing you to combine data sequentially without altering the original tuples.
Note
Remember, that to use tuple methods, like
count()
andindex()
, you will need to use dot notation, just as we did with list methods.
Let's explore how we can utilize the tuple constructor, concatenation, and tuple methods in a practical application.
Example Application
Imagine you have lists storing items that went on sale over the past 3 months. You need to convert them to tuples, concatenate the tuples, and then determine how many times an item has been put on sale in the last quarter. You also need to determine the index position to locate the first occurrence of an item.
# Define lists with items that have been put on sale, recording each sale occurrence for different months janSales_list = ["apples", "oranges", "apples"] febSales_list = ["bananas", "oranges", "bananas"] marSales_list = ["apples", "bananas", "apples"] # Convert the lists to tuples to ensure immutability (unchangeable) janSales = tuple(janSales_list) febSales = tuple(febSales_list) marSales = tuple(marSales_list) # Concatenate all monthly sales into a single tuple for the quarter quarterlySales = janSales + febSales + marSales print("Consolidated quarterly sales:", quarterlySales) # Use the `count()` method to determine how many times "apples" have been on sale during the quarter apples_sale_count = quarterlySales.count("apples") print("Apples have been on sale:", apples_sale_count, "times.") # Use the `index()` method to find the first occurrence of "apples" in the quarterly sales first_apple_sale_index = quarterlySales.index("apples") print("The first sale of apples this quarter was at index:", first_apple_sale_index)
Swipe to start coding
Manage the contents of a grocery store shelf using tuples, ensuring data integrity and performing some analytical checks.
- Convert the list
shelf1_update
of new items to a tuple calledshelf1_update_tuple
. - Concatenate
shelf1_update_tuple
with the existing tupleshelf1
to create a new tupleshelf1_concat
. - Count how many times
"celery"
appears inshelf1_concat
and store the result incelery_count
. - Find the index of the first occurrence of
"celery"
inshelf1_concat
and store it incelery_index
.
Output Requirements
- Print the updated shelf contents:
"Updated Shelf #1: <$shelf1_concat>"
. - Print the count of
"celery"
:"Number of Celery: <$celery_count>"
. - Print the index of the first occurrence of
"celery"
:"Celery Index: <$celery_index>"
.
Solution
Thanks for your feedback!
Tuple Operations
While tuples themselves cannot be altered, Python provides operations to create and combine them effectively.
Creation
The tuple()
function creates a tuple from an iterable object (strings, sets, lists), allowing for the conversion of lists or other iterables into tuples.
Concatenation
You can join two or more tuples into a new tuple using the +
operator, allowing you to combine data sequentially without altering the original tuples.
Note
Remember, that to use tuple methods, like
count()
andindex()
, you will need to use dot notation, just as we did with list methods.
Let's explore how we can utilize the tuple constructor, concatenation, and tuple methods in a practical application.
Example Application
Imagine you have lists storing items that went on sale over the past 3 months. You need to convert them to tuples, concatenate the tuples, and then determine how many times an item has been put on sale in the last quarter. You also need to determine the index position to locate the first occurrence of an item.
# Define lists with items that have been put on sale, recording each sale occurrence for different months janSales_list = ["apples", "oranges", "apples"] febSales_list = ["bananas", "oranges", "bananas"] marSales_list = ["apples", "bananas", "apples"] # Convert the lists to tuples to ensure immutability (unchangeable) janSales = tuple(janSales_list) febSales = tuple(febSales_list) marSales = tuple(marSales_list) # Concatenate all monthly sales into a single tuple for the quarter quarterlySales = janSales + febSales + marSales print("Consolidated quarterly sales:", quarterlySales) # Use the `count()` method to determine how many times "apples" have been on sale during the quarter apples_sale_count = quarterlySales.count("apples") print("Apples have been on sale:", apples_sale_count, "times.") # Use the `index()` method to find the first occurrence of "apples" in the quarterly sales first_apple_sale_index = quarterlySales.index("apples") print("The first sale of apples this quarter was at index:", first_apple_sale_index)
Swipe to start coding
Manage the contents of a grocery store shelf using tuples, ensuring data integrity and performing some analytical checks.
- Convert the list
shelf1_update
of new items to a tuple calledshelf1_update_tuple
. - Concatenate
shelf1_update_tuple
with the existing tupleshelf1
to create a new tupleshelf1_concat
. - Count how many times
"celery"
appears inshelf1_concat
and store the result incelery_count
. - Find the index of the first occurrence of
"celery"
inshelf1_concat
and store it incelery_index
.
Output Requirements
- Print the updated shelf contents:
"Updated Shelf #1: <$shelf1_concat>"
. - Print the count of
"celery"
:"Number of Celery: <$celery_count>"
. - Print the index of the first occurrence of
"celery"
:"Celery Index: <$celery_index>"
.
Solution
Thanks for your feedback!