Grokking Python Fundamentals
Ask Author
Back to course home

0% completed

Vote For New Content
Introduction to Tuple in Python
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Tuples in Python are immutable sequences, used to store multiple items in a single variable. They are similar to lists but are immutable, meaning their contents cannot be changed after they are created. This immutability makes tuples a reliable data structure for storing data that must not be modified, and they are often used for data integrity and threading where objects need to be unchanged.

Image

Defining a Tuple

Tuples are defined by placing a sequence of values separated by commas, with or without the use of parentheses. This is known as tuple packing.

Example

In this example, we will define a tuple using parentheses and without them.

Python3
Python3

. . . .

Explanation:

  • my_tuple uses parentheses to define the tuple.
  • another_tuple demonstrates tuple packing without parentheses, automatically creating a tuple from the provided items.

Accessing a Tuple Element

Elements in a tuple can be accessed via their index, similar to lists. The indexing starts at 0.

Example

In this example, we will access elements from a tuple using positive indexing.

Python3
Python3

. . . .

Explanation:

  • my_tuple[0] accesses the first element of my_tuple, which is 1.
  • my_tuple[2] accesses the third element of my_tuple, which is 3.

Updating a Tuple Element

Since tuples are immutable, their elements cannot be changed directly. However, you can simulate updating by converting the tuple to a list, changing the list, and converting it back to a tuple.

Example

In this example, we will simulate updating an element within a tuple by using list conversion.

Python3
Python3

. . . .

Explanation:

  • The tuple is converted to a list to allow for item assignment.
  • The second element is changed to 20.
  • The list is converted back to a tuple, reflecting the update.

Loop Through the Tuple

Iterating over a tuple uses a for-loop to access each item individually.

Example

In this example, we will loop through a tuple to print each item.

Python3
Python3

. . . .

Explanation:

  • The for loop iterates over each element in my_tuple, printing them on the same line separated by spaces.

Tuple Operators

Operators in Python can be used with tuples to perform concatenation, repetition, and membership tests. These operations are similar to those used with lists but are applied to tuples, which are immutable.

Tuple Operators

Here is a summary of operators that can be used with tuples:

OperatorDescriptionExample Usage
+Concatenates two tuples into a new tuple.tuple1 + tuple2
*Repeats the contents of a tuple a given number of times.tuple1 * 3
inChecks if an element exists within the tuple.element in tuple1
not inChecks if an element does not exist within the tuple.element not in tuple1

Example Demonstrating Tuple Operators

In this example, we will use tuple operators to concatenate two tuples, repeat a tuple, and perform membership tests.

Python3
Python3

. . . .

Explanation:

  • Concatenation: Combines tuple1 and tuple2 into concatenated_tuple.
  • Repetition: Creates repeated_tuple by repeating tuple1 three times.
  • Membership tests: Checks for the presence of 3 in tuple1 and the absence of 7.

Slicing the Tuple

Slicing allows you to create a new tuple by extracting elements from an existing tuple based on their indices.

How Slicing Works

  • Positive Indexing: Starts from the beginning of the tuple with index 0 and goes up to n-1 for n elements.
  • Negative Indexing: Starts from the end of the tuple with index -1 for the last element, -2 for the second last, and so forth.

Example

In this example, we will slice a tuple using both positive and negative indexes.

Python3
Python3

. . . .

Explanation:

  • Positive Slice: Extracts elements from index 2 to 6.
  • Negative Slice: Achieves the same result using negative indexing, starting from the end.

This lesson has covered essential aspects of tuples in Python, from basic operations and slicing to the nuances of tuple operators and the flexibility of tuple syntax. Understanding these concepts provides a solid foundation for using tuples effectively in Python programming.

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible