1 00:00:00,120 --> 00:00:04,236 In addition to membership testing, we can also use methods to determine how many 2 00:00:04,236 --> 00:00:06,486 times a given object appears in a sequence. 3 00:00:06,486 --> 00:00:10,980 And then finally, where or what index a particular object falls in a sequence. 4 00:00:12,160 --> 00:00:15,880 A method is just a function that is called on a specific object. 5 00:00:15,880 --> 00:00:18,601 Sequences, like most everything in Python, are objects. 6 00:00:18,601 --> 00:00:21,730 And they have special methods that can be called on them. 7 00:00:21,730 --> 00:00:25,060 For the first part of this video, we'll be talking about the count method. 8 00:00:25,060 --> 00:00:27,150 Count is a built-in sequence method. 9 00:00:27,150 --> 00:00:30,200 It receives one argument, the object that we're counting. 10 00:00:30,200 --> 00:00:32,100 Let's pick up with the last example. 11 00:00:32,100 --> 00:00:34,750 Now that we know the string tuple is inside docs, 12 00:00:34,750 --> 00:00:37,220 let's figure out how many times it appears. 13 00:00:37,220 --> 00:00:38,285 To use the count method, 14 00:00:38,285 --> 00:00:40,738 we'll first provide the sequence we wish to count from. 15 00:00:42,667 --> 00:00:46,367 Then we have a dot and then the name of the method, 16 00:00:48,292 --> 00:00:51,431 Followed by parens, just like with regular functions. 17 00:00:51,431 --> 00:00:55,544 And then we pass in our argument, which is the string tuple. 18 00:00:55,544 --> 00:00:59,853 All of this code together says that we want to know how many times the string 19 00:00:59,853 --> 00:01:01,184 tuple occurs in docs. 20 00:01:01,184 --> 00:01:03,627 I'll wrap this in a print statement so we can see the result, and 21 00:01:03,627 --> 00:01:04,770 then I'll save it and run it. 22 00:01:17,429 --> 00:01:21,672 Okay, the Python interpreter says that the count method returned the number 1, 23 00:01:21,672 --> 00:01:24,760 meaning the string tuple appears in this excerpt one time. 24 00:01:24,760 --> 00:01:28,458 But I'm looking at it right now, and I see tuple in there three times. 25 00:01:30,266 --> 00:01:32,123 Is the method wrong? 26 00:01:32,123 --> 00:01:35,965 Nope, all of these operators and methods are looking for exact matches. 27 00:01:35,965 --> 00:01:39,090 When it comes to strings, that means everything is case sensitive. 28 00:01:39,090 --> 00:01:43,890 This excerpt only includes the string tuple with a lower case t one time. 29 00:01:43,890 --> 00:01:46,440 Both other times, tuple begins with a capital t, and 30 00:01:46,440 --> 00:01:47,720 thus aren't considered a match. 31 00:01:48,730 --> 00:01:50,260 See the following instruction step for 32 00:01:50,260 --> 00:01:54,100 more examples on how to use count with different sequence types. 33 00:01:54,100 --> 00:01:58,360 Python also gives us a way to find the index of the first occurrence of an object 34 00:01:58,360 --> 00:01:59,720 inside a sequence. 35 00:01:59,720 --> 00:02:01,810 This is done using the index method. 36 00:02:01,810 --> 00:02:04,410 The syntax is very similar to the count method. 37 00:02:04,410 --> 00:02:05,730 And just like the count method, 38 00:02:05,730 --> 00:02:09,550 the index method takes a single argument, the object we want to find. 39 00:02:09,550 --> 00:02:12,322 So in our example here, I'll just change count to index. 40 00:02:15,526 --> 00:02:16,921 Then I'll save it and run it. 41 00:02:20,887 --> 00:02:22,824 Cool, it printed out 105. 42 00:02:22,824 --> 00:02:24,620 What does that mean? 43 00:02:24,620 --> 00:02:28,982 It means that the lowercase string tuple is first found at the 105 index in 44 00:02:28,982 --> 00:02:30,017 the docs string. 45 00:02:30,017 --> 00:02:35,289 Or in other words, our matched tuple string starts at the 106th character. 46 00:02:35,289 --> 00:02:36,513 I'm gonna erase all this and 47 00:02:36,513 --> 00:02:39,067 show another example of the index method with a short list. 48 00:02:43,200 --> 00:02:46,586 Here, I'm going to make an alphabetized list of a few Treehouse teachers. 49 00:03:00,356 --> 00:03:03,228 I want to find the index that Nicole appears in this list. 50 00:03:03,228 --> 00:03:08,038 So to do that, I'll do teachers.index, 51 00:03:08,038 --> 00:03:12,857 and then I'll pass Nicole as my argument. 52 00:03:15,422 --> 00:03:17,002 I'll wrap this in a print statement. 53 00:03:20,623 --> 00:03:21,947 And then now we'll run. 54 00:03:28,266 --> 00:03:30,932 Okay, this code printed out the number 2, 55 00:03:30,932 --> 00:03:34,617 meaning that the index of the item that matches Nicole is 2. 56 00:03:34,617 --> 00:03:38,740 That makes sense, we can see that Nicole is the third element in the list. 57 00:03:38,740 --> 00:03:41,631 So what happens if we add Nicole's name to the list a second time? 58 00:03:47,213 --> 00:03:48,233 Let's run this again. 59 00:03:51,436 --> 00:03:53,070 It's still printing out 2. 60 00:03:53,070 --> 00:03:56,870 That's because the index method only returns the index of the very first 61 00:03:56,870 --> 00:04:00,160 occurrence of an object, no matter how many times its present in a sequence. 62 00:04:01,330 --> 00:04:04,763 And now what happens if we delete the Nicole items from the list altogether? 63 00:04:16,291 --> 00:04:18,168 Okay, we're getting a ValueError. 64 00:04:18,168 --> 00:04:21,800 It says, 'Nicole' does not exist in list. 65 00:04:21,800 --> 00:04:22,922 This is important to remember. 66 00:04:22,922 --> 00:04:25,219 If you're using this method, always account for 67 00:04:25,219 --> 00:04:29,063 the fact that if the object doesn't exist, you'll get an error, not an integer. 68 00:04:29,063 --> 00:04:31,405 And your code won't run. 69 00:04:31,405 --> 00:04:34,911 All right, when you're ready, join me in the final video of Python Sequences 70 00:04:34,911 --> 00:04:37,260 to learn about concatenation and multiplication.