Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Tuples and Tuple Methods | Other Data Types
Introduction to Python Video Course
course content

Зміст курсу

Introduction to Python Video Course

Introduction to Python Video Course

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

Tuples and Tuple Methods

Tuples

In this chapter, we explore tuples, another fundamental Python data structure ideal for handling immutable (unchangeable) data sequences. Tuples would prove to be incredibly useful in our grocery store scenario.

For instance, we may need to hold records of product details that seldom change or ensure sensitive information, such as product ID numbers, remain consistent and unaltered throughout a program's execution.

Watch as Alex demonstrates how to work with tuples to ensure stable data management in our grocery store context:

Fundamentals of Tuples

Tuples in Python are a basic yet powerful data structure, similar to lists but designed for immutability.

Here's a breakdown of their key characteristics:

Creation

Tuples are created by enclosing comma-separated values in parentheses () (unlike lists, which are created by enclosing comma-separated values in square brackets [].).

Ordering

Similar to lists, the elements in a tuple maintain a specific order. This order is fixed and cannot be changed, which can be helpful for data integrity.

Immutability (Unchangeability)

Once a tuple is created, its elements cannot be changed, added to, or removed. This immutability makes tuples a reliable choice for storing data that should not be altered through the program's lifecycle.

Allowing Duplicates

Similar to lists, tuples can contain multiple instances of the same value, making them suitable for storing repetitive data securely and efficiently. However, it's generally uncommon to find duplicates in tuples for typical use cases.

Examples

So, the primary difference between tuples and lists in Python lies in their mutability. Tuples are created using parentheses () and are immutable, meaning they cannot be modified after creation.

Let's look at this in more detail.

Consider a tuple that categorizes different sections in a grocery store — a set of values unlikely to change frequently:

12345
# Define a tuple with grocery store categories grocery_aisles = ("Produce", "Dairy", "Bakery", "Meat", "Frozen Foods") # Display a tuple on the screen print("Grocery Aisles:", grocery_aisles)
copy

Elements within a tuple can be accessed using index numbers, similar to lists. This indexing works the same way it did with lists, allowing you to retrieve any item based on its position:

12345
# Define a tuple with multiple data types apple_details = ("apple", 34, True, 1.99, "Fuji") # Get an element of a tuple by its index print("Apple Type:", apple_details[4])
copy

Tuples can hold any type of object, just like lists. They can also include mutable objects like nested lists (or dictionaries, which we will talk about a little later). Although tuples are immutable, the mutable objects within them can still be altered.

Take the apple_details tuple - if we need to update a state in the list containing state names, we can accomplish that by using level 1 and level 2 indexing, just as we did with nested lists.

123456
# Tuple containing various data types and a nested list apple_details = ("apple", 34, True, 1.99, "Fuji", ["Washington", "California", "Michigan"]) print(apple_details) # Updating the mutable python list even while nested in an immutable data type (tuple) apple_details[5][2] = "Pennsylvania" print(apple_details)
copy

Tuple Methods

While tuples do not support methods that alter their content, they still provide a few built-in methods to help manage and utilize them effectively. Here are the two built-in methods you can use with tuples:

  • count(): returns the number of times a specified value appears in the tuple;
  • index(): searches the tuple for a specified value and returns the index position of where it was first found.

Note

The same methods can also be used with lists.

12345678910
# Example tuple containing a mix of integers and strings fruits = ("apple", "banana", "cherry", "apple", "banana", "cherry", "apple") # Use the `count()` method to determine how many times "apple" appears in the tuple apple_count = fruits.count("apple") print("Number of times 'apple' appears:", apple_count) # Use the `index()` method to find the first occurrence of "cherry" in the tuple cherry_index = fruits.index("cherry") print("The first occurrence of 'cherry' is at index:", cherry_index)
copy
1. What will be the output of the following Python code?
2. Which line of code correctly finds the index position of the description `"Baby Spinach"` within the `spinachDetails` tuple?

What will be the output of the following Python code?

Виберіть правильну відповідь

Which line of code correctly finds the index position of the description "Baby Spinach" within the spinachDetails tuple?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 4. Розділ 4
We're sorry to hear that something went wrong. What happened?
some-alt