Count and Index4:38 with Ashley Boucher
Count and Index are two other Python sequence methods that give us useful information about our sequence data.
In addition to membership testing, we can also use methods to determine how many 0:00 times a given object appears in a sequence. 0:04 And then finally, where or what index a particular object falls in a sequence. 0:06 A method is just a function that is called on a specific object. 0:12 Sequences, like most everything in Python, are objects. 0:15 And they have special methods that can be called on them. 0:18 For the first part of this video, we'll be talking about the count method. 0:21 Count is a built-in sequence method. 0:25 It receives one argument, the object that we're counting. 0:27 Let's pick up with the last example. 0:30 Now that we know the string tuple is inside docs, 0:32 let's figure out how many times it appears. 0:34 To use the count method, 0:37 we'll first provide the sequence we wish to count from. 0:38 Then we have a dot and then the name of the method, 0:42 Followed by parens, just like with regular functions. 0:48 And then we pass in our argument, which is the string tuple. 0:51 All of this code together says that we want to know how many times the string 0:55 tuple occurs in docs. 0:59 I'll wrap this in a print statement so we can see the result, and 1:01 then I'll save it and run it. 1:03 Okay, the Python interpreter says that the count method returned the number 1, 1:17 meaning the string tuple appears in this excerpt one time. 1:21 But I'm looking at it right now, and I see tuple in there three times. 1:24 Is the method wrong? 1:30 Nope, all of these operators and methods are looking for exact matches. 1:32 When it comes to strings, that means everything is case sensitive. 1:35 This excerpt only includes the string tuple with a lower case t one time. 1:39 Both other times, tuple begins with a capital t, and 1:43 thus aren't considered a match. 1:46 See the following instruction step for 1:48 more examples on how to use count with different sequence types. 1:50 Python also gives us a way to find the index of the first occurrence of an object 1:54 inside a sequence. 1:58 This is done using the index method. 1:59 The syntax is very similar to the count method. 2:01 And just like the count method, 2:04 the index method takes a single argument, the object we want to find. 2:05 So in our example here, I'll just change count to index. 2:09 Then I'll save it and run it. 2:15 Cool, it printed out 105. 2:20 What does that mean? 2:22 It means that the lowercase string tuple is first found at the 105 index in 2:24 the docs string. 2:28 Or in other words, our matched tuple string starts at the 106th character. 2:30 I'm gonna erase all this and 2:35 show another example of the index method with a short list. 2:36 Here, I'm going to make an alphabetized list of a few Treehouse teachers. 2:43 I want to find the index that Nicole appears in this list. 3:00 So to do that, I'll do teachers.index, 3:03 and then I'll pass Nicole as my argument. 3:08 I'll wrap this in a print statement. 3:15 And then now we'll run. 3:20 Okay, this code printed out the number 2, 3:28 meaning that the index of the item that matches Nicole is 2. 3:30 That makes sense, we can see that Nicole is the third element in the list. 3:34 So what happens if we add Nicole's name to the list a second time? 3:38 Let's run this again. 3:47 It's still printing out 2. 3:51 That's because the index method only returns the index of the very first 3:53 occurrence of an object, no matter how many times its present in a sequence. 3:56 And now what happens if we delete the Nicole items from the list altogether? 4:01 Okay, we're getting a ValueError. 4:16 It says, 'Nicole' does not exist in list. 4:18 This is important to remember. 4:21 If you're using this method, always account for 4:22 the fact that if the object doesn't exist, you'll get an error, not an integer. 4:25 And your code won't run. 4:29 All right, when you're ready, join me in the final video of Python Sequences 4:31 to learn about concatenation and multiplication. 4:34
You need to sign up for Treehouse in order to download course files.Sign up