Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Tuple Operations | Other Data Types
Introduction to Python (copy)
course content

Course Content

Introduction to Python (copy)

Introduction to Python (copy)

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

book
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() and index(), 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.

123456789101112131415161718192021
# 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)
copy
Task

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 called shelf1_update_tuple.
  • Concatenate shelf1_update_tuple with the existing tuple shelf1 to create a new tuple shelf1_concat.
  • Count how many times "celery" appears in shelf1_concat and store the result in celery_count.
  • Find the index of the first occurrence of "celery" in shelf1_concat and store it in celery_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

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 4. Chapter 5
toggle bottom row

book
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() and index(), 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.

123456789101112131415161718192021
# 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)
copy
Task

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 called shelf1_update_tuple.
  • Concatenate shelf1_update_tuple with the existing tuple shelf1 to create a new tuple shelf1_concat.
  • Count how many times "celery" appears in shelf1_concat and store the result in celery_count.
  • Find the index of the first occurrence of "celery" in shelf1_concat and store it in celery_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

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 4. Chapter 5
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