English subtitles

← Index - Intro to Computer Science

Get Embed Code
5 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. [Evans] There are many other ways to define find_element.
  2. I'm going to show you 1 way that takes advantage of another built-in list operation
  3. that we haven't introduced yet but that makes it much easier to write find_element.
  4. The operation is called index.
  5. The index method is invoked on a list.
  6. You pass in a value, and the output in index is the position that that value exists in the list.
  7. So the built-in index almost solves find_element exactly the way we want.
  8. The difference is when we invoke index on a list that doesn't contain the value we pass in.
  9. Index doesn't return -1. What it does is gives us an error.
  10. So let's look at what happens in the Python interpreter.
  11. I'll define a variable p and give it the value of the list 0, 1, 2.
  12. If we invoke index on p, passing in 2, we get as the result 2,
  13. which is the position in p where the value 2 occurs.
  14. For the second example we'll add more 2s in the list,
  15. and we see that it always gives us the first one.
  16. So the result returned by p index is the first place in the list where that occurs.
  17. Now we'll try an element that doesn't exist in the list.
  18. What we see is instead of getting -1 as a result we get an error
  19. that the target element that we looked for doesn't appear in the list.
  20. So if we want find_element to have the behavior that's described,
  21. we can't quite do it using index directly.
  22. There are some other list operations that might be very helpful, though.
  23. So to summarize the behavior of the index method,
  24. if the value passed in is in the list, it returns the first position where the value is found.
  25. This is exactly what we wanted for find_element.
  26. The problem is if the value is not found in the list, it produces an error.
  27. There's another list operation that will be useful to allow us to use index to find_element,
  28. and it's useful for many other things, and that's the in word.
  29. We've already seen in being used in the for-loop.
  30. Here we use the same word in, but in this context it means something different.
  31. The syntax is to have a value to the left of the in word followed by the list.
  32. The syntax is a bit strange compared to the other things we've seen in Python.
  33. It doesn't look like a procedure call, but it's very natural in terms of how it corresponds
  34. to how we would say this in English.
  35. If we say something like, "Is 3 in the list?"
  36. we would write that in Python as 3 in p.
  37. So let's see how that works in the interpreter.
  38. Now I'm printing the result of 3 in p.
  39. 3 is not in the list, so it prints out False.
  40. If I change this to print the result of 2 in p,
  41. we get the value True.
  42. So to summarize the behavior of the in operator,
  43. if the value is in the list, the output is true.
  44. Otherwise the output is false.
  45. Similarly, we can use not in.
  46. Not in has the opposite meaning of in.
  47. If the value is not in the list, the result of value not in list is true.
  48. If the value is in the list, then the result of value not in list is false.
  49. The value not in list is exactly equivalent to saying not value in list.
  50. The only reason to have the not in is it's more natural to read it this way in English
  51. than having the not between the value and the in.
  52. So now to check that you understand index and in and not in operations,
  53. I want you to try to define the find_element procedure again.
  54. It should have the same exact behavior as the ones we've defined before.
  55. It should give us the position of the element we're searching for if it exists,
  56. and it should give us -1 if it doesn't exist.
  57. But this time instead of using a while loop or a for-loop to define it
  58. see if you can define it using index.