The tuple Type in Python

Life is 10% what happens to us and 90% how we react to it. If you don't build your dream, someone else will hire you to help them build theirs.

The tuple Type in Python

Let’s discuss the tuple class in Python. The tuple class provides an immutable sequence of elements, similar to the list class, which provided a mutable sequence of elements. As a result, it will have fewer methods as it has no methods available to actually change an existing tuple. First of all, we have a tuple() function, which acts as a constructor to create an empty_tuple by default. We can see the empty_tuple, when printed, shows up as a pair of empty parenthesis. The same tuple constructor function can be used to convert things like sequences of characters in a string into individual elements in a tuple. You can see if the tuple() function is applied to the string hello, then each individual string that made up the larger string becomes a element within the tuple.

 # The tuple class provides a immutable sequence of elements 
 empty_tuple =tuple() 
 print('empty_tuple ->', empty_tuple) 
 tuple_str = tuple('hello')
 print('tuple_str ->', tuple_str) 
 tuple_list = tuple([1, 2, [3, 5, 7]])
 print('tuple_list ->', tuple_list) 
 empty_tuple = () 
 print('empty_tuple->', empty_tuple) 
 singleton_tuple = (1,) 
 print('singleton_tuple ->',singleton_tuple) 
 tuple_syn = (3, 4, 'a', 'b') 
 print('tuple_syn ->',tuple_syn) 
 print("'a' in tuple_syn ->", 'a' in tuple_syn) 
 print("1 notin tuple_syn ->", 1 not in tuple_syn) 
 print('tuple_str ->', tuple_str)
 print('min(tuple_str) ->', min(tuple_str)) 
 print('max(tuple_str) ->',max(tuple_str)) 
 print('sorted(tuple_str) ->', sorted(tuple_str))
 print('tuple_str.count("o") ->', tuple_str.count("o"))
 print('tuple_str.index("o") ->', tuple_str.index("o"))
 print('len(tuple_str) ->', len(tuple_str)) 
 
 The Output : empty_tuple -> () 
 tuple_str -> ('h', 'e', 'l', 'l', 'o')
 tuple_list -> (1, 2, [3, 5, 7]) 
 empty_tuple -> () 
 singleton_tuple ->(1,) 
 tuple_syn -> (3, 4, 'a', 'b') 
 'a' in tuple_syn -> True 
 1 not in tuple_syn -> True 
 tuple_str -> ('h', 'e', 'l', 'l', 'o') 
 min(tuple_str)-> e 
 max(tuple_str) -> o 
 sorted(tuple_str) -> ('e', 'h', 'l', 'l', 'o')
 tuple_str.count ("o") -> 1 
 tuple_str.index ("o") -> 4 
 len(tuple_str) ->5




The basic tuple syntax is just to use the parenthesis around a comma-separated list of different objects. In this case, those objects are simply integers and strings, although they could be any objects at all like other lists, tuples, dictionaries, bytes, bytes arrays, etc. There’s only a few things that you can do with tuples because they are immutable; you could test for membership with the in operator. If that element is a member of that tuple, then this will return true. And as “a” is in tuple_syn, you can see that it does return true. You can also use not in to test for non-membership since 1 is not in tuple_syn, it’s not a member, that returns true as well.

 tuple_syn = (3, 4,'a', 'b') 
 print('tuple_syn ->', tuple_syn) 
 print("'a' in tuple_syn ->",'a' in tuple_syn) 
 print("1 not in tuple_syn ->", 1 not in tuple_syn)
 
 The output: tuple_syn -> (3, 4, 'a', 'b') 
 'a' in tuple_syn-> True 
 1 not in tuple_syn -> True

Looking at that tuple string again we see, once more, that it is the individual strings that make up the word “hello.” We could apply the min() function to find the smallest element. In this case the one closest to the beginning of the alphabet, or we can apply the max() function to find the largest element. In this case the “o,” which is closest to the end of the alphabet. There is no sort method or reverse method for a tuple like there was for a list, but you can apply the sorted() function to a tuple if you wanted a sequence that was in a specific order. However, that sequence will be a list sequence and not actually a tuple.

 tuple_str = tuple('hello') 
 print('tuple_str ->', tuple_str)
 print('min(tuple_str) ->', min(tuple_str)) 
 print('max(tuple_str) ->',max(tuple_str)) 
 print('sorted(tuple_str) ->', sorted(tuple_str)) 
 
 The outputs: 
 tuple_str -> ('h', 'e', 'l', 'l', 'o') 
 min(tuple_str) -> e
 max(tuple_str) -> o 
 sorted(tuple_str) -> ('e', 'h', 'l', 'l', 'o')

Couple other methods that are available for the tuple, are count() and index(). Count() will let you know how many times a particular element appears and index() will let you know at what index position. In this case, that string hello, or the individual strings that make it up, appears just with the “o” one time. You can see the index position is 4, once again I’d remind you that Python uses zero-based indexing. So we count from the beginning 0, 1, 2, 3, 4 until we got the index position of that element. And finally, you can apply the length() function to a tuple to find out the total number of elements that are present within that object. We’ll see more about tuples in the upcoming demonstration on how slicing works in Python. For now that’s a good introduction to how tuples work within Python.

 tuple_str = tuple('hello') 
 print('tuple_str.count("o") ->',tuple_str.count("o")) 
 print('tuple_str.index("o") ->',tuple_str.index("o")) 
 print('len(tuple_str) ->', len(tuple_str)) 
 
 The output : tuple_str.count ("o") -> 1 
 tuple_str.index ("o") -> 4
 len(tuple_str) -> 5