Python Tuple is a data structure for storing a sequence of unchangeable Python objects. The tuple is similar to lists in that the value of the items placed in the list may be modified, however, the tuple is unchangeable and its value cannot be changed.
Creating a Python Tuple
The collection of comma-separated “,” values contained by small () brackets is known as a tuple. The use of parentheses is optional, however, it is recommended.
Note: creating a Python tuple without using parentheses is called Tuple Packing.
Example 01: This example shows the addition of elements in a Tuple.
#Creating an empty Tuple Tuple1 = () print("Initial empty Tuple: ") print (Tuple1) #Creating a Tuple #with the use of string Tuple1 = ('Softhunt', 'Tutorials') print("\nTuple with the use of String: ") print(Tuple1) # Creating a Tuple with # the use of list list1 = [1, 2, 4, 5, 6] print("\nTuple using List: ") print(tuple(list1)) #Creating a Tuple #with the use of built-in function Tuple1 = tuple('Softhunt') print("\nTuple with the use of function: ") print(Tuple1)
Example 02: This example shows the creation of a tuple with mixed data types. Tuples can have any number of elements and any form of data (like strings, integers, lists, etc.). Tuples can also be made from a single element, although this is more difficult. It is not enough to have one element in parentheses; a trailing ‘comma’ is require to make it a tuple.
#Creating a Tuple #with Mixed Datatype Tuple1 = (5, 'Softhunt', 7, 'Tutorials') print("\nTuple with Mixed Datatypes: ") print(Tuple1) #Creating a Tuple #with nested tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('python', 'Softhunt') Tuple3 = (Tuple1, Tuple2) print("\nTuple with nested tuples: ") print(Tuple3) #Creating a Tuple #with repetition Tuple1 = ('Softhunt',) * 3 print("\nTuple with repetition: ") print(Tuple1) #Creating a Tuple #with the use of loop Tuple1 = ('Softhunt') n = 5 print("\nTuple with a loop") for i in range(int(n)): Tuple1 = (Tuple1,) print(Tuple1)
Accessing of Tuples
Tuples are immutable and often include a sequence of heterogeneous elements that may be access by unpacking or indexing (or even by attribute in the case of named tuples). Lists are changeable, and their elements are generally homogenous, and they may be access by iterating over lists.
Note: The number of variables on the left-hand side of the unpacking tuple should be equal to the number of values in the provided tuple a.
#Accessing Tuple #with Indexing Tuple1 = tuple("Softhunt") print("\nFirst element of Tuple: ") print(Tuple1) #Tuple unpacking Tuple1 = ("Python", "Softhunt", "Tutorials") #This line unpack #values of Tuple1 a, b, c = Tuple1 print("\nValues after unpacking: ") print(a) print(b) print(c)
Add OR Concatenation of Tuples
The process of connecting two or more Tuples is known as tuple concatenation. The ‘+’ operator is used for concatenation. Tuples are always concatenate from the end of the original tuple. On Tuples, other arithmetic operations do not apply.
Note: Concatenation may only combine data types of the same type; combining a list and tuple results in an error.
# Concatenation of tuples Tuple1 = (0, 1, 2, 3) Tuple2 = ('Python', 'Softhunt', 'Tutorials') Tuple3 = Tuple1 + Tuple2 # Printing first Tuple print("Tuple 1: ") print(Tuple1) # Printing Second Tuple print("\nTuple2: ") print(Tuple2) # Printing Final Tuple print("\nTuples after Concatenation: ") print(Tuple3)
Slicing of Tuple
Slicing a Tuple allows you to retrieve a specified range or slice of sub-elements from it. Lists and arrays can be sliced as well. Slicing allows you to obtain a collection of elements instead of a single element when indexing in a list.
Note: Negative Increment values are also useful for reversing the Tuples sequence.
# Slicing of a Tuple # Slicing of a Tuple # with Numbers Tuple1 = tuple('Softhunt') # Removing First element print("Removal of First Element: ") print(Tuple1[1:]) # Reversing the Tuple print("\nTuple after sequence of Element is reversed: ") print(Tuple1[::-1]) # Printing elements of a Range print("\nPrinting elements between Range 4-9: ") print(Tuple1[4:9])
Deleting a Tuple
Tuples are immutable, which means they can’t be deleted in parts. The del() function is used to remove the whole tuple.
Note: When printing a Tuple after it has been deleted, an error occurs.
# Deleting a Tuple Tuple1 = (0, 1, 2, 3, 4) del Tuple1 print(Tuple1)
Python Tuple Methods
|index()||find in the tuple and returns the index of the supplied item in the tuple where it exists|
|count()||returns the frequency of occurrence of a specified value|
Python Tuple Functions
|all()||Returns true if all elements is true or if a tuple is empty|
|any()||return true if any element of the tuple is true. if the tuple is empty, return false|
|len()||Returns length of the tuple or size of the tuple|
|enumerate()||Returns enumerate object of tuple|
|max()||return maximum element of a given tuple|
|min()||return minimum element of a given tuple|
|sum()||Sums up the numbers in the tuple|
|sorted()||input elements in the tuple and return a new sorted list|
|tuple()||Convert an iterable to a tuple.|