Lists and Functions
Lesson 1 of 2
  1. 1
    This exercise goes over just pulling information from a list, which we’ve covered in a previous section!
  2. 2
    You’ve already learned how to modify elements of a list in a previous section. This exercise is just a recap of that!
  3. 3
    Here, we’ll quickly recap how to .append() elements to the end of a list.
  4. 4
    This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called n: 1. n.pop(index) will remove the item at index from the list and return it to y…
  5. 5
    In this exercise, you will just be making a minor change to a function to change what that function does.
  6. 6
    This exercise will recap how to use more than one argument in a function.
  7. 7
    This is a basic recap on using strings in functions.
  8. 8
    You pass a list to a function the same way you pass any other argument to a function.
  9. 9
    Passing a list to a function will store it in the argument (just like with a string or a number!) def first_item(items): print items[0] numbers = [2, 7, 9] first_item(numbers) 1. In the examp…
  10. 10
    Modifying an element in a list in a function is the same as if you were just modifying an element of a list outside a function. def double_first(n): n[0] = n[0] * 2 numbers = [1, 2, 3, 4] doubl…
  11. 11
    You can also append or delete items of a list inside a function just as if you were manipulating the list outside a function. my_list = [1, 2, 3] my_list.append(4) print my_list # prints [1, 2, 3,…
  12. 12
    This exercise will go over how to utilize every element in a list in a function. You can use the existing code to complete the exercise and see how running this operation inside a function isn’t mu…
  13. 13
    This exercise shows how to modify each element in a list. It is useful to do so in a function as you can easily put in a list of any length and get the same functionality. As you can see, len(n) is…
  14. 14
    Okay! Range time. The Python range() function is just a shortcut for generating a list, so you can use ranges in all the same places you can use lists. range(6) # => [0, 1, 2, 3, 4, 5] range(1, 6)…
  15. 15
    Now that we’ve learned about range, we have two ways of iterating through a list. Method 1 - for item in list: for item in list: print item Method 2 - iterate through indexes: for i …
  16. 16
    Now let’s try working with strings! for item in list: print item for i in range(len(list)): print list[i] The example above is just a reminder of the two methods for iterating over a list.
  17. 17
    Using multiple lists in a function is no different from just using multiple arguments in a function! a = [1, 2, 3] b = [4, 5, 6] print a + b # prints [1, 2, 3, 4, 5, 6] The example above is just…
  18. 18
    Finally, this exercise shows how to make use of a single list that contains multiple lists and how to use them in a function. list_of_lists = [[1, 2, 3], [4, 5, 6]] for lst in list_of_lists: fo…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo