Lists and Functions

Now that you've learned about lists, let's turbo-charge them with functions.

StartPython Illustration
Chevron Left Icon
Lists and Functions
Lesson 1 of 2
Chevron Right Icon
  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 [...] 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 [...] : 1. [...] will remove the item at [...] from the list and return it t...

  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!) [...] 1. In the example above, we define a function called [...] . It has one argument called ...

  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. [...] 1. We create a list called [...] . 2. We use the [...] f...

  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. [...] The example above is just a reminder of how to append items t...

  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, [...] i...

  14. 14

    Okay! Range time. The Python [...] function is just a shortcut for generating a list, so you can use ranges in all the same places you can use lists. [...] The [...] function has three diff...

  15. 15

    Now that we've learned about [...] , we have two ways of iterating through a list. Method 1 - [...] : [...] Method 2 - iterate through indexes: [...] Method 1 is useful to loop t...

  16. 16

    Now let's try working with strings! [...] 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! [...] The example above is just a reminder of how to concatenate two lists.

  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. [...] 1. In the example above, we first create a list containing t...

  1. 1

    In this project you will build a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5x5 ...

  2. 2

    The first thing we need to do is to set up the game board.

  3. 3

    Good! Now we'll use a built-in Python function to generate our board, which we'll make into a 5 x 5 grid of all [...] s, for "ocean." [...] will print out [...] , which is the basis for a row...

  4. 4

    Great job! Now that we've built our board, let's show it off. Throughout our game, we'll want to print the game board so that the player can see which locations they have already guessed. Regular...

  5. 5

    Now we can see the contents of our list, but clearly it would be easier to play the game if we could print the board out like a grid with each row on its own line. We can use the fact that our b...

  6. 6

    We're getting pretty close to a playable board, but wouldn't it be nice to get rid of those quote marks and commas? We're storing our data as a list, but the player doesn't need to know that! [....

  7. 7

    Excellent! Now, let's hide our battleship in a random location on the board. Since we have a 2-dimensional list, we'll use two variables to store the ship's location, [...] and [...] . [......

  8. 8

    Good job! For now, let's store coordinates for the ship in the variables [...] and [...] . Now you have a hidden battleship in your ocean! Let's write the code to allow the player to guess whe...

  9. 9

    Awesome! Now we have a hidden battleship and a guess from our player. In the next few steps, we'll check the user's guess to see if they are correct. While we're writing and debugging this par...

  10. 10

    Okay—now for the fun! We have the actual location of the ship and the player's guess so we can check to see if the player guessed right. For a guess to be right, [...] should be equal to [...]...

  11. 11

    Great! Of course, the player isn't going to guess right all the time, so we also need to handle the case where the guess is wrong. [...] The example above prints out [...] , the element in th...

  12. 12

    Great job! Now we can handle both correct and incorrect guesses from the user. But now let’s think a little bit more about the "miss" condition. 1. They can enter a guess that's off the board. 2...

  13. 13

    Great! Now let's handle the second type of incorrect guess: the player guesses a location that was already guessed. How will we know that a location was previously guessed? [...] The example ...

  14. 14

    Congratulations! Now you should have a game of Battleship! that is fully functional for one guess. Make sure you play it a couple of times and try different kinds of incorrect guesses. This is...

  15. 15

    You can successfully make one guess in Battleship! But we’d like our game to allow the player to make up to 4 guesses before they lose. [...] We can use a [...] loop to iterate through a ran...

  16. 16

    If someone runs out of guesses without winning right now, the game just exits. It would be nice to let them know why. Since we only want this message to display if the user guesses wrong on their ...

  17. 17

    Almost there! We can play Battleship!, but you’ll notice that when you win, if you haven’t already guessed 4 times, the program asks you to enter another guess. What we’d rather have happen is for ...

  18. 18

    Congratulations! You have a fully functional Battleship game! Play it a couple of times and get your friends to try it out, too. (Don’t forget to go back and remove the debugging output that gives ...

  19. 19

    You can also add on to your Battleship! program to make it more complex and fun to play. Here are some ideas for enhancements—maybe you can think of some more! 1. Make multiple battleships: you'll...

How you'll master it

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

Pro Logo

Lists and Functions

StartPython Illustration