Tuples
Learn about the tuple object in Python.
We'll cover the following...
We'll cover the following...
Definition
A tuple consists of two or more values separated by commas and enclosed in parentheses, for example, ("John", 23)
.
Python 3.5
tup1 = (1, 2, 3) # Tuple of integersprint(tup1)tup2 = (7, "Great", 1.52, True) # Tuple of mixed data typesprint(tup2)
Tuples are useful for keeping a small number of values together as a single unit. For example, you might want to write a function that returns the x-y coordinates of an object, or the largest and smallest values in a list.
Python 3.5
def get_largest_smallest(my_list):largest = max(my_list) # Getting largest value from list using max built-in functionsmallest = min(my_list) # Getting smallest value from list using min built-in functionreturn (largest, smallest) # Returning a tuple containing the largest and smallest valuemy_list = [1,8,6,5,3,2]print(get_largest_smallest(my_list)) # Calling function -> (8, 1)
...