Syntax:
We need to take special care when we declare tuple with single element.
If we omit comma (,) here, it will be a normal integer variable.
In the first example, the type is tuple.
In the second example, the type is integer.
Other way of declaring a tuple:
How to enter into Python interpreter?
Open Linux terminal and type “python”, then hit enter so we will see python interpreter. For python3+ version, type “python3”, these are the following info we are going to see on the terminal. If we want to check python version, type “python -v”.
Output:
[GCC 5.2.0] on Linux
Type "help", "copyright", "credits", or "license" for more information.
>>>
The following operations can be performed on tuple:
Tuple Slice
This is useful when we want only part of the tuple.
Note: Tuple index always starts from 0. Tuple can be traversed in forward and reverse direction (using negative index).
Example:
Forward traverse index: [0,1,2,3…]
reverse traverse index: […,-3,-2,-1] here t[-1]=10.5, t[-2]=”good”,
t[-3]=”hi”, t[-4]=”4”,…
Syntax:
Here, stop is excluded. If we provide only start, it will extract all the elements from start to end of tuple. If we provide only stop, it will extract from 0th index to stop index. We can omit both start and stop, in that case, we need to provide at least colon (t[:]). If we don’t provide step value default, the value will be 1.
Ex:
In this example, we would want to extract elements “1,2,3,4”.
Suppose we want to extract elements “3,4,5,’i’,’hi’,10.5”
Suppose we want to extract elements “2,3,4,5,’I’,’hi’ ” (using reverse index)
Suppose we want to reverse a tuple
Nested Tuples
We can declare tuple in a tuple, i.e., nested tuples.
Consider nested tuple as another tuple and its index also starts from 0.
We can access nested tuples elements as below:
- Find nested tuple index in main tuple
- Find nested tuple index
Ex:
In the example below, we want extract “3” from nested tuple. Here, the main tuple index is “t[2]”, and nested tuple “(3,4,5)” index is “0”. So, the final expression is “t[2][0]”.
In the second example, we extracted “b” from nested tuple using expression “t[5][1]”.
Length
This method returns number of elements in tuple.
Syntax:
Access tuple by element using loop
Syntax:
For variable in tuple variable:
Repetition
This is useful when we want to repeat the tuple for given number.
Syntax:
Example:
Here, the tuple is repeated 2 times, as shown below.
Concatenation
This concatenates or combines 2 tuples.
Syntax:
Search element in a tuple
This return “True” if element found in tuple else return “False”.
Syntax:
Element not in tuple
Index
This method is used to find the index of element in tuple. If found returns “index of the element” else value error exception is raised.
Syntax:
Count
This method is used to count occurrence of element in tuple.
Syntax:
Delete tuple
We can’t remove individual elements from tuples since it is immutable. But we can delete entire tuple.
Syntax:
In the above example, we declared tuple t and printed t. After that, we deleted a tuple using “del t” and tried to print tuple. It throws nameerror exception because “tuple t” doesn’t exist.
Minimum
This method is used to find minimum value of element in a tuple.
Syntax:
Maximum
This method is used to find minimum value of element in a tuple.
Syntax:
Compare 2 tuples
This method is used to compare elements of 2 tuples.
- Return 0 if elements of both tuples are equal
- Return 1 if elements of the first tuple are greater than the second tuple
- Return -1 if elements of the first tuple are less than the second tuple
Syntax:
If elements types are mismatched, then element is converted to int type.
Tuples are compared index by index. The 1st element of the 1st tuple is compared to the 1st element of the 2nd tuple. If they are not equal, this is the result of the comparison, else the 2nd element is considered, then the 3rd element, and so on.
Conclusion
Tuple is immutable data type, and any operation we perform should be stored in another tuple variable. It is faster compared to the other data types (ex: list, dictionary). Since tuple is immutable in our program, the data is not going to change the entire software life cycle, we can use tuple like system configuration data.
The above is most commonly and generally used operation on tuple. If we want to check what all the operations are supported for tuple, type dir(tuple) on interpreter and hit enter. It will display all methods/function. If we want to check documentation for tuple method/function, type help(tuple) and hit enter.