Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring Other Features

00:00 In Python, tuples are sequences and sequences share many common operations. We’ve already gone over iteration, but there are yet even more we can discuss.

00:10 In this lesson, we’ll explore some of the most useful operations: tuple methods, count() and index(), built-in Python functions, len(), min() and max(), Python operators in and not in and comparators less than, greater than, and equals. For the last time, let’s hit the REPL. Let’s start with tuple methods.

00:32 These are functions that are called from a tuple object. First, you need a tuple. Define the tuple fruits as the following series of strings: “apple”, “banana”, “orange”, “apple”, “apple”, “kiwi”, and “banana”.

00:48 You can use the .count() method to find how many times an item is present in the tuple. Calling fruits.count() and passing the string “apple” results in the integer 3 being returned because there are three copies of the string “apple” in the tuple, whereas if I pass the string “banana”,

01:06 the result is the integer 2.

01:09 Passing a string not found in the tuple results in zero.

01:15 On the other hand, you can use the .index() method to find the first occurrence of an item in the tuple. Calling fruits.index() and passing the string “apple” returns the integer zero, which is the location of the first occurrence of the string “apple”.

01:30 However, if you pass an item that is not found in the tuple, the .index() method will raise a ValueError.

01:37 So it’s important to remember you should not use tuple.index() unless you have already verified that the item you’re looking for is in the tuple.

01:47 Now let’s look at built-in functions for sequences. Create a tuple holding the following six integers: 4, 8, 15, 16, 23, and 42.

01:59 Call that tuple numbers.

02:01 Now for any sequence type, including tuples, you can find the number of elements in the sequence by using the len() function. This is also sometimes called the cardinality of the sequence. numbers has six elements, and you can verify that using len().

02:18 Now if all the elements in the tuple are comparable, you can also use the min() function to find the smallest element and the max() function to find the largest element.

02:28 The most common use cases for these are numeric types and strings. Calling min(numbers) returns the number 4, calling max() with numbers as the argument, returns the number 42.

02:42 And finally, if all elements in the tuple are numeric, you can use the sum() function to get the sum of elements in the tuple. The sum of numbers is 108.

02:52 We can keep using the same tuple to see Python’s in and not in operators. You can use these operators to form Boolean expressions that test an item for inclusion in a container and return True or False. They’re opposites of each other.

03:06 When in would return True, not in returns False, and vice versa. So a question: Is the integer 42 in numbers?

03:15 All you have to do is write the code 42 in numbers and the output is True. Is the integer 42 not in numbers?

03:25 Again, just write 42 not in numbers and the evaluation of the expression is False. Is the integer 108 in numbers? False.

03:37 How about the integer 108 not in numbers, and that is True.

03:45 And now we come to comparison. When you compare two tuples, Python uses what’s known as lexicographical ordering, which is like how dictionaries sort words one letter at a time.

03:56 It looks at the first element of each tuple and if they differ, that is what decides the result of the comparison. If the elements are equal, Python moves on to the second element.

04:05 If they’re equal, Python moves on to the third element and so on. If all elements are equal, well, both tuples are equal. Let’s look at a few examples using tuple literals.

04:16 Test equality with the double equal sign. On the left-hand side, a tuple with the integers 2 and 3. On the right-hand side, another tuple with the integers 2 and 3.

04:26 This expression evaluates to True because both elements are equal. You can test less than using the less than symbol. On the left-hand side, the integers 5 and 6. On the right-hand side, the integers 7 and 5. We’re testing if the left-hand side is less than the right-hand side, and this also evaluates to True because 5 is less than 7.

04:48 The greater than symbol works the same way, but tests for the opposite condition. Again, 5, 6 on the left-hand side, 7, 5 on the right-hand side, and this expression evaluates to False because 5 is not greater than 7.

05:02 You can also test for less than or equal by using, you guessed it, the less than or equal operator. On the left-hand side, the integers 4, 3.

05:10 On the right-hand side, the integers 4, 3. Is 4, 3 less than or equal to 4, 3?

05:17 This evaluates to True. Tuples of differing lengths can also be compared. On the left-hand side, a tuple of 5, 6. On the right-hand side, a tuple of just one value, 8, and we’re testing if the left-hand side is less than the right-hand side.

05:33 This evaluates to True because 5 is less than 8. There’s no need to compare the second element in the first tuple.

05:41 But what happens when the elements compared have mismatched types? When only testing for equality, mismatched types aren’t an issue. You can test this by testing the equality of two tuples, the string “python” and the int 42 on the left with the string “python” and the string “42” on the right.

05:59 This evaluates to False because the string “42” is not equal to the integer 42. But if you try to compare in terms of greater or less than, you will encounter an error.

06:13 This is because Python has no concept of comparing integers with strings. It’s important to keep these finer details in mind because they could lead to tricky bugs and errors if you aren’t careful.

06:25 Okay, wow, you’ve dived deep into the world of tuples. Next up, I’ll leave you with some guidelines and advice on when tuples are the right data structure to use in your Python code.

Become a Member to join the conversation.