Tuple
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.
- tuple(): Creates a new tuple from a sequence or iterable.
- count(): Returns the number of occurrences of a value in a tuple.
- index(): Returns the index of the first occurrence of a value in a tuple.
- sorted(): Returns a new sorted list from a tuple.
- max(): Returns the maximum value in a tuple.
- min(): Returns the minimum value in a tuple.
- any(): Returns
True
if any element in the tuple isTrue
, otherwiseFalse
.
- all(): Returns
True
if all elements in the tuple areTrue
, otherwiseFalse
.
- enumerate(): Returns an iterable of tuples, where each tuple contains the index and value of each element in the original tuple
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