Python Tuple
In Python, a tuple is another built-in data structure that is similar to a list. However, unlike lists, tuples are immutable, which means their elements cannot be modified once they are created. Tuples are defined using parentheses (()
) and elements are separated by commas.
Here’s an example of a tuple in Python:
person = ('John', 25, 'USA')
In this example, the variable person
refers to a tuple that contains three elements: 'John'
, 25
, and 'USA'
. The order of elements in a tuple is preserved.
Tuples support various operations and methods, some of which are similar to those of lists. Let’s explore some of the commonly used operations and methods with tuples:
Accessing Elements:
- Individual elements of a tuple can be accessed using their index, just like in lists. For example,
person[0]
returns'John'
.
- Individual elements of a tuple can be accessed using their index, just like in lists. For example,
Tuple Unpacking:
- You can assign the elements of a tuple to separate variables in a single line. This is called tuple unpacking. For example,
name, age, country = person
assigns'John'
toname
,25
toage
, and'USA'
tocountry
.
- You can assign the elements of a tuple to separate variables in a single line. This is called tuple unpacking. For example,
Tuple Concatenation:
- Tuples can be concatenated using the
+
operator. This creates a new tuple that combines the elements of the original tuples. For example,(1, 2, 3) + (4, 5, 6)
returns(1, 2, 3, 4, 5, 6)
.
- Tuples can be concatenated using the
Tuple Repetition:
- Tuples can be repeated using the
*
operator. This creates a new tuple that repeats the elements of the original tuple. For example,(1, 2, 3) * 3
returns(1, 2, 3, 1, 2, 3, 1, 2, 3)
.
- Tuples can be repeated using the
Tuple Length:
- The
len()
function returns the number of elements in a tuple. For example,len(person)
returns3
in the above example.
- The
Immutable Nature:
- Tuples are immutable, which means you cannot modify their elements once they are created. If you try to assign a new value to an element or modify an element directly, you’ll get an error.
Tuples are commonly used when you want to store a collection of related values that should not be changed, such as coordinates, database records, or fixed configurations. They are also useful for returning multiple values from a function using a single tuple.
It’s important to note that although tuples are immutable, if an element of a tuple is mutable (like a list), you can modify the mutable object itself, but not the tuple itself. For example, if a tuple contains a list, you can modify the elements of the list, but you cannot assign a new list to that position in the tuple.
my_tuple = ([1, 2, 3], 'hello')
my_tuple[0].append(4)
# Modifying the list within the tuple print(my_tuple)
# Output: ([1, 2, 3, 4], 'hello')
# The following line will raise an error because tuples are immutable my_tuple[1] = 'world'
I hope this gives you a good understanding of tuples in Python. If you have any more specific questions, feel free to ask!