Python tuples are meant to hold various types of data, pretty much like lists one of the only differences are that instead of using “[ ]” you use “()”, but are still indexed and can be updated and modified pretty much like Tuples.
You can declare a variable tuple empty:
Or you can initialize it:
And you can even place different types of data in the same tuple:
There is a variable assignment which is a tuple assignment.
That will respectively assign 25 to int and 31 to int2. Now imagine you want to trade two variable’s values:
With tuple assignment this is super easy:
And, an error will be produced if you try to assign less or more variables than you should:
are indexed by integers starting on position 0 and until n-1, being n the length of the Tuple.
Example:
Has length 5 and can be indexed like this:
0 | 1 | 2 | 3 | 4 |
Output:
Which will print out every member of Tuple4.
But we can also access it with negative numbers which do it from the end to the start. Like this:
-5 | -4 | -3 | -2 | -1 |
Output:
This will print the Tuple4 backward
Or we can also use a for to iterate over the Tuple.
Output:
This consists of a while loop that increments a variable until it is out-of-bounds of Tuple4.
Important: Since the Tuple indexes start at 0, then the last index is the size of the Tuple - 1.
We can repeating the contents by x times. Example:
We can also concatenate Tuples by adding them.
Example:
Will print a bigger Tuple containing Tuple4 and Tuple6
There is a way to finding out if a specific value exists in a specific Tuple.
Example:
This will print out True because the string “Hey” indeed exists in Tuple4.
Using the Membership operator, we can iterate over every member of a Tuple.
Output:
For each iteration of this loop, the temporary variable member will be assigned a value of the Tuple being printed out and passing on to the next value.
To get the size of a specific Tuple. Using the method len().
Example:
This code will print out the size of Tuple4 which is 5.
Tuples can be sliced to show only desired values or to iterate over desired indexes.
First-line will print from index 1 to 3 and the second will print starting on index 2.
Tuples can be very useful to hold data, like student names for a runtime, trying to compare students or sort the Tuple alphabetically. Since Tuples are very versatile on what data can be held, it eases a lot of its use.
Since tuples are quite like lists, both are used in similar situations as well.
However, there are certain advantages of implementing a tuple over a list.
- We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
- Since tuples are immutable, iterating through tuple is faster than with list. -> slight performance boost.
Function |
Description |
E.g |
len(Tuple) |
It is used to calculate the length of the Tuple. |
len( Tuple7 ) will return 3 |
max(Tuple) |
It returns the maximum element of the Tuple. |
max( Tuple7 ) will return 8 |
min(Tuple) |
It returns the minimum element of the Tuple. |
min( Tuple7 ) will return 1 |
Tuple(seq) |
It converts a list to a tuple. |
Tuple([1,2,3,4,5,6,7]) will create the Tuple ( 1, 2 , 3, 4, 5, 6, 7) |
Python Tuple built-in methods
Function |
Description |
E.g |
Tuple.count(obj) |
It returns the number of occurrences of the specified object in the Tuple. |
Tuple9 = ("Hello", "World" ) print(Tuple9.count("Hello")) It will print out 1 which is the number of times the string “Hello” is in Tuple9. |
Tuple.index(obj) |
It returns the lowest index in the Tuple that object appears.
|
print(Tuple9.index("Hello")) It prints out 0 which is the index of “Hello”.
|