English subtitles

← List Addition and Length - Intro to Computer Science

Get Embed Code
6 Languages

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

  1. [Narrator] That's what append does.
  2. We're going to introduce 2 other list operations.
  3. The next one is plus.
  4. Plus behaves very similar to the concatenation operation for strings.
  5. If we have the list 0, 1 + the list 2, 3
  6. the result is the list 0, 1, 2, 3.
  7. What plus does like concatenation for strings is it produces a new list.
  8. It doesn't mutate either of the input lists.
  9. I want to introduce 1 other operator that works on lists, and that's the len operator.
  10. Len is short for length, and we use len
  11. like this: it looks like a procedure call.
  12. We pass into len the object that we want to know the length of that can be a list.
  13. Len actually works for many things other than lists.
  14. It also works for strings.
  15. It works for any object that's a collection of things,
  16. and the output from len is the number of elements in the input.
  17. For example, the result of len applied to the list 0, 1 is 2,
  18. since there are 2 elements in the list.
  19. The result of applying len to this list is also 2.
  20. It looks like there are many more elements here,
  21. but len is only counting the outer elements.
  22. If 1 of the elements of a list is a list, it doesn't matter how many elements that list contains.
  23. It only contributes 1 to the length of the original list,
  24. so the result of this call would also be 2.
  25. We can also use len on a string, and the
  26. output will be the number of characters in the string.
  27. In this case the string Udacity has 7 characters, so the output is 7.
  28. Now we are ready for some quizzes to see how well you understand
  29. the 3 list operations we've introduced.
  30. The append operation which is invoked on a list
  31. and takes an element as a parameter and adds that element to the end of the list.
  32. The plus operator which operates on 2 lists as its operands
  33. and produces a new list that consists of all the elements in the 2 lists put together.
  34. And finally the len operator that takes a list as its input and produces
  35. an output number that is the length of the list.
  36. The number of elements in the input list.