1 00:00:00,390 --> 00:00:01,290 Instructor: Welcome back. 2 00:00:01,290 --> 00:00:04,320 Let's keep going with some more string methods. 3 00:00:04,320 --> 00:00:06,660 Now we've learned about adding and removing ones. 4 00:00:06,660 --> 00:00:09,810 We learned about append, insert, and extend. 5 00:00:09,810 --> 00:00:10,860 And then in removing, 6 00:00:10,860 --> 00:00:14,190 we learned about pop, remove, and clear. 7 00:00:14,190 --> 00:00:16,680 There's a few other important ones. 8 00:00:16,680 --> 00:00:19,083 Let's go with the index. 9 00:00:20,280 --> 00:00:23,200 So instead of clearing here, let's clean this up 10 00:00:24,990 --> 00:00:28,283 and we'll do basket.index. 11 00:00:31,410 --> 00:00:33,960 And index, as you can see, 12 00:00:33,960 --> 00:00:36,720 we give it a value, a start and a stop. 13 00:00:36,720 --> 00:00:37,860 What does that mean? 14 00:00:37,860 --> 00:00:42,513 Well, we wanna say, Hey, what index is number two? 15 00:00:44,040 --> 00:00:47,050 If we do index of two and I click run 16 00:00:48,630 --> 00:00:50,130 it's going to give me one. 17 00:00:50,130 --> 00:00:51,960 I know this is a little confusing 18 00:00:51,960 --> 00:00:54,240 but it's asking for the index. 19 00:00:54,240 --> 00:00:55,710 Where is number two? 20 00:00:55,710 --> 00:00:58,920 Number two is at index of one. 21 00:00:58,920 --> 00:01:02,310 Let's change these into letters just so it's more clear. 22 00:01:02,310 --> 00:01:07,310 So we have A, B, C, D, 23 00:01:07,350 --> 00:01:09,033 and then finally E. 24 00:01:10,350 --> 00:01:14,500 And again, if I do D here and I click run 25 00:01:15,540 --> 00:01:19,620 I get an index of three because 0, 1, 2, 3 26 00:01:19,620 --> 00:01:22,620 it's on shelf three in our memory. 27 00:01:22,620 --> 00:01:26,790 And again, also do a optional parameter here 28 00:01:26,790 --> 00:01:29,460 which is where should we start looking? 29 00:01:29,460 --> 00:01:31,080 So I want you to start looking 30 00:01:31,080 --> 00:01:36,080 at index of zero and then finish looking at index of two. 31 00:01:36,150 --> 00:01:38,850 If I do run here, I get an error. 32 00:01:38,850 --> 00:01:42,210 It says, Hey, D is not in list because, well 33 00:01:42,210 --> 00:01:45,930 we started at index of zero and stopped at index of two. 34 00:01:45,930 --> 00:01:48,033 If I do three and I click run, 35 00:01:48,960 --> 00:01:49,793 nope. 36 00:01:49,793 --> 00:01:51,540 It stops right before three. 37 00:01:51,540 --> 00:01:55,443 So if I do four and I click run, 38 00:01:56,790 --> 00:01:57,623 there you go. 39 00:01:57,623 --> 00:01:58,563 I get three. 40 00:01:59,640 --> 00:02:02,460 Now, errors are not good in our programs. 41 00:02:02,460 --> 00:02:05,700 Ideally, we don't wanna get errors. 42 00:02:05,700 --> 00:02:07,080 So how can we avoid this? 43 00:02:07,080 --> 00:02:08,490 What if we're looking for something 44 00:02:08,490 --> 00:02:11,220 and we're not sure if it's in the list or not? 45 00:02:11,220 --> 00:02:14,910 There's another nifty way of looking for things in a list 46 00:02:14,910 --> 00:02:19,770 and this involves what we call a Python key word. 47 00:02:19,770 --> 00:02:22,230 A key word is something that you use 48 00:02:22,230 --> 00:02:25,143 in Python that will -- already has some meaning. 49 00:02:26,370 --> 00:02:31,370 For example, a key word here if I go to Python keywords 50 00:02:31,500 --> 00:02:35,040 are all these words that already mean something. 51 00:02:35,040 --> 00:02:38,070 Now we're gonna go through them and we haven't seen a lot 52 00:02:38,070 --> 00:02:42,270 of them yet, but you we might have seen true, right? 53 00:02:42,270 --> 00:02:44,730 In boolean and values, true means something. 54 00:02:44,730 --> 00:02:49,140 So in Python, we can't really use the word true 55 00:02:49,140 --> 00:02:51,363 because well, it already means something. 56 00:02:52,410 --> 00:02:54,730 There's a word called in 57 00:02:56,100 --> 00:02:58,800 that as you can see, as soon as we type means something, 58 00:02:58,800 --> 00:03:00,480 is a keyword in Python. 59 00:03:00,480 --> 00:03:02,387 And this is quite nice. 60 00:03:02,387 --> 00:03:05,610 We can say, Hey, is D 61 00:03:05,610 --> 00:03:07,173 in basket? 62 00:03:08,130 --> 00:03:09,483 And if I run this, 63 00:03:10,620 --> 00:03:12,030 I get true. 64 00:03:12,030 --> 00:03:15,123 Okay, is X in basket? 65 00:03:16,800 --> 00:03:21,800 I get false because, well, it doesn't exist. 66 00:03:22,380 --> 00:03:24,210 And this is actually quite useful. 67 00:03:24,210 --> 00:03:26,460 And we can even do it for strings. 68 00:03:26,460 --> 00:03:28,710 For example, if I'm looking for 69 00:03:28,710 --> 00:03:33,710 let's say the letter I in a word, and let's say it's I in 70 00:03:34,687 --> 00:03:39,510 "Hi, my name is the end." 71 00:03:39,510 --> 00:03:40,863 If I click run, 72 00:03:42,060 --> 00:03:42,893 I get true 73 00:03:42,893 --> 00:03:47,280 because the letter I does exist in this string. 74 00:03:47,280 --> 00:03:49,200 All right, let's learn another one. 75 00:03:49,200 --> 00:03:52,773 Another one is basket.count. 76 00:03:54,060 --> 00:03:57,630 And we can count how many times an item occurs. 77 00:03:57,630 --> 00:04:01,330 So for example, if I do D and I click run 78 00:04:02,250 --> 00:04:07,250 I get one because D only exists once in the list. 79 00:04:07,410 --> 00:04:12,410 But if I add another D in here and I click run, we get two. 80 00:04:12,660 --> 00:04:17,040 It counts how many time this value occurs in our list. 81 00:04:17,040 --> 00:04:19,260 Very, very useful. 82 00:04:19,260 --> 00:04:21,240 All right, a few more still to go. 83 00:04:21,240 --> 00:04:24,090 Let's do some exercises and I'll see you in the next one.