1 00:00:08,000 --> 00:00:13,590 Most sequences have a few useful methods that you can do on any sequence. 2 00:00:13,590 --> 00:00:18,450 The way to call that method is to save the sequence name and then the method name. 3 00:00:18,450 --> 00:00:24,460 So, I'm going to go over two methods on sequences, count and index. 4 00:00:24,860 --> 00:00:27,540 So, suppose that we have a, 5 00:00:27,540 --> 00:00:30,750 a string and we want to find out how many times 6 00:00:30,750 --> 00:00:35,380 a given character or a given sequence of characters appears in the string a? 7 00:00:35,380 --> 00:00:38,770 The way that we would do that is using the count method. 8 00:00:38,770 --> 00:00:43,880 The way to call count is we say the name of the sequence whether it's a string, 9 00:00:43,880 --> 00:00:46,945 integer, or tuple that we want to count on. 10 00:00:46,945 --> 00:00:50,395 So here, we want to do a count on a. 11 00:00:50,395 --> 00:00:54,765 So, I say a, and then the dot or period, 12 00:00:54,765 --> 00:00:57,230 and then the method name that we want to call, 13 00:00:57,230 --> 00:01:00,845 dot count, open parentheses. 14 00:01:00,845 --> 00:01:02,525 In the case of count, 15 00:01:02,525 --> 00:01:07,340 we pass in a string which is the sequence of characters that we actually want to count. 16 00:01:07,340 --> 00:01:08,750 So, on line two, 17 00:01:08,750 --> 00:01:12,900 we're counting the number of characters e in a, 18 00:01:12,900 --> 00:01:17,185 and then we close parenthesis in order to finish that method call. 19 00:01:17,185 --> 00:01:20,725 One thing to note here is that count is case sensitive. 20 00:01:20,725 --> 00:01:24,985 Meaning that we don't count capital e's if there are any capital e's in a. 21 00:01:24,985 --> 00:01:27,280 We would only count lowercase e's. 22 00:01:27,280 --> 00:01:30,905 So here, when we say printout a.count("e"), 23 00:01:30,905 --> 00:01:33,840 then we look for the number of e's that are in the string a. 24 00:01:33,840 --> 00:01:36,390 So, in this case, we should get one, 25 00:01:36,390 --> 00:01:42,280 two, three, four, five. 26 00:01:42,830 --> 00:01:46,115 In addition to counting individual characters, 27 00:01:46,115 --> 00:01:47,960 we can also count substrings. 28 00:01:47,960 --> 00:01:52,475 So here, we're counting the number of times the substring ha appears. 29 00:01:52,475 --> 00:01:59,220 So, we should get one, two. 30 00:01:59,220 --> 00:02:01,835 So, let's run our code just to double check. 31 00:02:01,835 --> 00:02:10,260 So, we can see that we get five e's and two times that ha appears in this string a. 32 00:02:10,520 --> 00:02:14,430 Just like strings, count also works on lists. 33 00:02:14,430 --> 00:02:22,470 So here, we have a list z that has 1,2,3,4,5,6,7,8,9,10 items. 34 00:02:22,470 --> 00:02:24,835 So, on line two, 35 00:02:24,835 --> 00:02:27,530 if I comment out the rest of these lines, 36 00:02:27,530 --> 00:02:29,690 on line two, we're finding out 37 00:02:29,690 --> 00:02:34,490 how many times does the string four appear as an item in z. 38 00:02:34,490 --> 00:02:37,250 One thing to note here is that the string 39 00:02:37,250 --> 00:02:41,810 four is an entirely different thing than the integer four, 40 00:02:41,810 --> 00:02:46,070 which we have here, here, and here. 41 00:02:46,070 --> 00:02:50,225 So, when we say print out z.count the string four, 42 00:02:50,225 --> 00:02:56,200 then we're going to actually get zero because no item in z is the string four. 43 00:02:58,670 --> 00:03:02,270 If we instead counted for the integer four, 44 00:03:02,270 --> 00:03:03,965 then we would get what we might expect. 45 00:03:03,965 --> 00:03:06,420 So, we'd get one, two, 46 00:03:06,420 --> 00:03:11,075 three because there are three integers four in our list z. 47 00:03:11,075 --> 00:03:14,030 So, if I hit save and run again, 48 00:03:14,030 --> 00:03:19,920 then I'll see that I get three when I print out z.count the integer four. 49 00:03:20,440 --> 00:03:23,210 Here's something that's a bit trickier. 50 00:03:23,210 --> 00:03:28,160 So, let's suppose that I say z.count the string a. 51 00:03:28,160 --> 00:03:30,925 Now, we can see that in our list z, 52 00:03:30,925 --> 00:03:33,945 the letter a appears a few times. 53 00:03:33,945 --> 00:03:35,985 So, in the first item of z, 54 00:03:35,985 --> 00:03:37,635 a appears in atoms, 55 00:03:37,635 --> 00:03:42,060 and then it also appears in the last item. 56 00:03:42,060 --> 00:03:45,145 So, we might almost expect the result to be two. 57 00:03:45,145 --> 00:03:46,950 But if we actually run our code, 58 00:03:46,950 --> 00:03:48,735 then we'll see the result is zero. 59 00:03:48,735 --> 00:03:50,610 Now, why is that? 60 00:03:50,610 --> 00:03:54,240 Well, when we call z.count the character a, 61 00:03:54,240 --> 00:04:00,220 what we're looking for our items in z where the item itself is the string a. 62 00:04:00,220 --> 00:04:02,870 So, the fact that a just happens to be 63 00:04:02,870 --> 00:04:06,230 a character in this larger string doesn't matter to Python. 64 00:04:06,230 --> 00:04:08,240 What Python would care about is, 65 00:04:08,240 --> 00:04:11,585 is the value actually the string a? 66 00:04:11,585 --> 00:04:16,315 So, if I had one item here that was the string a, 67 00:04:16,315 --> 00:04:18,955 then I would get count one. 68 00:04:18,955 --> 00:04:23,915 But because this instead is the string atoms, 69 00:04:23,915 --> 00:04:27,570 then I get zero when I look for the number of a's. 70 00:04:29,630 --> 00:04:34,200 On line five, we do z.count("electron"), 71 00:04:34,200 --> 00:04:37,545 and when I look, I see that there is one, 72 00:04:37,545 --> 00:04:40,095 two electrons in z. 73 00:04:40,095 --> 00:04:44,040 So, I should expect line five to print out two. 74 00:04:44,570 --> 00:04:49,415 Now, let's go on to another useful method named index. 75 00:04:49,415 --> 00:04:52,535 Index finds where an item is. 76 00:04:52,535 --> 00:04:54,020 In the case of index, 77 00:04:54,020 --> 00:04:59,555 it searches for the first time that a given string or item appears. 78 00:04:59,555 --> 00:05:01,835 So, let's suppose that on line one, 79 00:05:01,835 --> 00:05:03,485 we're creating this string, 80 00:05:03,485 --> 00:05:05,705 "Pull out your music and dancing can begin". 81 00:05:05,705 --> 00:05:14,270 Now on line two, we create this list that has 1,2,3,4,5,6,7,8,9,10 items. 82 00:05:14,270 --> 00:05:19,705 Now, when we print out the value of music.index of m, 83 00:05:19,705 --> 00:05:25,279 then we're printing out the first or leftmost index where lowercase m appears. 84 00:05:25,279 --> 00:05:28,950 So, if I comment out the rest of these lines, 85 00:05:29,240 --> 00:05:33,685 and only print out the value of music.index of m, 86 00:05:33,685 --> 00:05:38,645 then we should get the index of the first m. Just to find out where that is, 87 00:05:38,645 --> 00:05:40,620 we should get it at 88 00:05:42,140 --> 00:05:50,610 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14. 89 00:05:50,610 --> 00:05:55,895 So, this m, which is the first term leftmost m that appears is at index 14. 90 00:05:55,895 --> 00:06:00,200 What that means is that the value of this overall expression, 91 00:06:00,200 --> 00:06:05,015 music.index of m is going to be 14. 92 00:06:05,015 --> 00:06:08,120 Let's run our code and make sure that that's the case. 93 00:06:08,120 --> 00:06:10,660 So, we see that we get 14. 94 00:06:10,660 --> 00:06:16,325 On line five, we print out the value of music.index of your. 95 00:06:16,325 --> 00:06:18,920 So, what this is going to do is it's going to search for 96 00:06:18,920 --> 00:06:22,145 the first time that this substring 'your' appears, 97 00:06:22,145 --> 00:06:24,760 and it's going to print out the index where it starts. 98 00:06:24,760 --> 00:06:28,140 So, we should expect that to be zero, one, two, three, 99 00:06:28,140 --> 00:06:32,280 four, five, six, seven, eight, nine. 100 00:06:32,280 --> 00:06:38,890 So, when we run this and we get nine as a result because 'your' starts at index nine. 101 00:06:38,890 --> 00:06:43,685 Now, index works with lists as well as it does with strings. 102 00:06:43,685 --> 00:06:47,570 So, if we print out bio.index of metatarsal, 103 00:06:47,570 --> 00:06:50,935 then we get the first index where metatarsal appears. 104 00:06:50,935 --> 00:06:52,755 So here, on bio, 105 00:06:52,755 --> 00:06:54,945 that should be index zero. 106 00:06:54,945 --> 00:06:58,820 Even though this appears multiple times at index zero and one, 107 00:06:58,820 --> 00:07:03,505 index always gives us the first or leftmost index where it appears. 108 00:07:03,505 --> 00:07:07,600 So, nine, seven should print out zero. 109 00:07:08,510 --> 00:07:13,185 On line eight, we print out bio.index of an empty list. 110 00:07:13,185 --> 00:07:17,165 This is going to search for the first empty list in this list bio. 111 00:07:17,165 --> 00:07:19,845 So, the index should be zero, 112 00:07:19,845 --> 00:07:22,995 one, two, and we find it at three. 113 00:07:22,995 --> 00:07:24,210 So, when we run this, 114 00:07:24,210 --> 00:07:26,260 we should expect three. 115 00:07:26,390 --> 00:07:28,800 Then finally on line nine, 116 00:07:28,800 --> 00:07:31,920 we print out bio.index of 43. 117 00:07:31,920 --> 00:07:34,260 So, that's going to be zero, one, two, 118 00:07:34,260 --> 00:07:37,660 three, four, five, six. 119 00:07:39,140 --> 00:07:44,365 Now, one thing to note for index is that if something is actually not in the sequence, 120 00:07:44,365 --> 00:07:46,310 then we get a runtime error. 121 00:07:46,310 --> 00:07:48,000 So, if we create this list, 122 00:07:48,000 --> 00:07:51,225 seasons and set its values to be winter, 123 00:07:51,225 --> 00:07:52,950 spring, summer, and fall. 124 00:07:52,950 --> 00:07:54,720 Then we search for autumn, 125 00:07:54,720 --> 00:07:58,510 note that autumn is not in here because we instead wrote fall. 126 00:07:58,510 --> 00:08:02,175 So, if we print out seasons.index of autumn, 127 00:08:02,175 --> 00:08:04,740 then we're going to get a runtime error, 128 00:08:04,740 --> 00:08:08,450 and that runtime error is going to print out that the item that we searched for, 129 00:08:08,450 --> 00:08:13,110 in this case, autumn was not in our list, seasons. 130 00:08:13,610 --> 00:08:18,410 Okay. So, let's answer some questions about count and index. 131 00:08:18,410 --> 00:08:20,750 So, this question ask, 132 00:08:20,750 --> 00:08:23,405 what will be stored in the variable ty below? 133 00:08:23,405 --> 00:08:26,160 So, we say ty is qu, 134 00:08:26,160 --> 00:08:29,445 which is this string dot index of we. 135 00:08:29,445 --> 00:08:33,995 So, this is searching for the first time that we find the string we. 136 00:08:33,995 --> 00:08:36,260 So, that's going to give us zero, one, 137 00:08:36,260 --> 00:08:39,160 two, three, four, five. 138 00:08:39,160 --> 00:08:41,190 Again, when we search for a substring, 139 00:08:41,190 --> 00:08:42,810 we search for where it starts. 140 00:08:42,810 --> 00:08:44,610 So, that's going to give us five. 141 00:08:44,610 --> 00:08:52,070 Next, what will be stored in the variable ty now? 142 00:08:52,070 --> 00:08:58,335 So, we say ty is the string qu.count of we. 143 00:08:58,335 --> 00:09:01,730 So, here we're searching for how many times does 144 00:09:01,730 --> 00:09:05,705 this substring we appear in this string qu? 145 00:09:05,705 --> 00:09:09,905 So, we see we right here at the start of welcome, 146 00:09:09,905 --> 00:09:12,470 here at the start of week. 147 00:09:12,470 --> 00:09:17,030 We might think that we actually see it here, 148 00:09:17,030 --> 00:09:19,314 but if you look closely, 149 00:09:19,314 --> 00:09:23,690 this is actually a capital w and not a lowercase w. Now, 150 00:09:23,690 --> 00:09:25,760 remember that Python is case sensitive. 151 00:09:25,760 --> 00:09:30,950 So, capital w is a completely different thing than a lowercase w. So, 152 00:09:30,950 --> 00:09:33,845 Python isn't actually going to count this. 153 00:09:33,845 --> 00:09:39,710 So, Python isn't actually going to count this we because it contains a capital w. So, 154 00:09:39,710 --> 00:09:43,230 the end result here is that we get two. 155 00:09:47,690 --> 00:09:52,200 So here, we're asked, what will be stored in the variable ht below. 156 00:09:52,200 --> 00:09:56,060 So, we have room set to a list with one, two, three, four, 157 00:09:56,060 --> 00:10:00,940 five, six Items, and we asked for rooms.index of garden. 158 00:10:00,940 --> 00:10:05,385 So, we're going to search for is garden in this list rooms. 159 00:10:05,385 --> 00:10:07,065 So, we see it's not here, 160 00:10:07,065 --> 00:10:08,220 nor here, nor here, 161 00:10:08,220 --> 00:10:10,005 nor here, nor here, nor here. 162 00:10:10,005 --> 00:10:12,510 Garden isn't anywhere in this list. 163 00:10:12,510 --> 00:10:17,750 Remember that when we tried to do index on something that's actually not in the list, 164 00:10:17,750 --> 00:10:19,145 then we get an error. 165 00:10:19,145 --> 00:10:22,500 So, the answer here should be an error. 166 00:10:23,020 --> 00:10:26,760 That's all for now, until next time.