Tuple
my_tuple = (1, "hello", True)
The main characteristics of a tuple are:
- Defined by round bracts, and each element in the tuple is separated by a comma
- Collection fo elements that can be of any data type
- Iterable object: can be divided in smaller parts
- Immutable Object: which means that can not be modified
- Orderd: ammit the repetiton of elements and can be sliced and indexed
Why use tuples?
If an array is not to be changed, it is preferable to use a tuple rather than a list for the following reasons:
- The computation is faster. Processing time for a tuple is less than for a list. > - Memory space is less. Tuples consume less memory space than lists.
The sintax for a one-element tuple is:
tuple = (x, )
Concatenation and Replication
Note
Work’s in the same way of lits
- read: [[Python Lists#concatenation-and-replication|Concatenation and Replication]] to know more
Comparison Operators
Note
Work’s in the same way of lits
- read: [[Python Lists#comparison-operators|Comparison Operators]] to know more
Built-in Functions
Tuples are immutable data structures in Python, and they provide a convenient way to group related data together. Here are the 10 most commonly used tuple functions in Python:
- len(): Returns the length of a tuple.
t = (1, 2, 3, 4, 5)
print(len(t)) # Output: 5
- tuple(): Creates a new tuple from a sequence or iterable.
s = "hello"
t = tuple(s)
print(t) # Output: ('h', 'e', 'l', 'l', 'o')
- count(): Returns the number of occurrences of a value in a tuple.
t = (1, 2, 2, 3, 3, 3, 4, 5)
print(t.count(3)) # Output: 3
- index(): Returns the index of the first occurrence of a value in a tuple.
t = (1, 2, 3, 4, 5)
print(t.index(3)) # Output: 2
- sorted(): Returns a new sorted list from a tuple.
t = (5, 3, 1, 2, 4)
sorted_t = sorted(t)
print(sorted_t) # Output: [1, 2, 3, 4, 5]
- max(): Returns the maximum value in a tuple.
t = (1, 2, 3, 4, 5)
print(max(t)) # Output: 5
- min(): Returns the minimum value in a tuple.
t = (1, 2, 3, 4, 5)
print(min(t)) # Output: 1
- any(): Returns
True
if any element in the tuple isTrue
, otherwiseFalse
.
t = (False, 0, "", 4)
print(any(t)) # Output: True
- all(): Returns
True
if all elements in the tuple areTrue
, otherwiseFalse
.
t = (True, 1, "hello")
print(all(t)) # Output: True
- enumerate(): Returns an iterable of tuples, where each tuple contains the index and value of each element in the original tuple
t = ("apple", "banana", "cherry")
for index, value in enumerate(t):
print(index, value)
# Output:
# 0 apple
# 1 banana
# 2 cherry
Slicing of a List
Note
Work’s in the same way of lists
- read: ** Slicing of a List** to know more
Quartz Index Exampleof a List
Note
Work’s in the same way of lists
- read: ** Index of a List** to know more
Difference:
There is a main difference between lists and tuples, tuples are immutable.
tuple = (1,2,3,4)
tuple[0] = 5
-⇒ is impossible, you can’t change an elemet of a tuple
Loops in a List
Note
Work’s in the same way of lists
- read: ** Loops in a List** to know more