The glory of python lists
Start Here: Jump to a section
wb_incandescentLearning goals
wb_incandescentList basics
wb_incandescentMini-project: Checklist manager
Learning goals
- Create lists in python and manipulate those lists by appending to them, removing items, and displaying their contents
- Demonstrate how to create and read tuples and demonstrate their immutability
- Use while looping to create a menu-based program for manipulating a list whose order conveys a ranking in a context relevant to the student
Essentials of lists
Lists are a way of storing data values in an ordered sequence and provide a backbone data structure for many software components. Lists are mutable, meaning we can create them and then edit their contents through the use of methods (i.e. functions) such as append(x) and remove(x).
Code along with the following sample python where we create lists literally and with append() and then display their contents:
official documentation |
python.org provides a comprehensive page summarizing the data structures essentials in python, starting with lists and running through dictionaries! Dedicate a few hours to carefully creating code that demonstrates the operations on lists described in the documentation. |
List indexes start at zero! 0
A critical behavior of list operations in python (and many other languages) is that list items are numbered starting at an index of 0. The second item in a list has an index of 1, and so forth. When accessing list items, therefore, our code must be careful not to try to access the index of an element that is not in the list. This often happens because we start counting at 1 and ask for what we think is the last item in a list, but in reality the requested item doesn't exist.
Study the following example and output by creating your own list and trying to access its items by index. Try accessing an item that is not in the list and see that you, too, can get an index out of range error.
arrow_upward back up to contents
Checklist manager mini-project
To exercise your list prowess, create a program that allows users to create and manage a checklist of items to use in conducting some task that's interesting to you. The checklist could also be more like a list of steps to make something such as cooking recipe or a craft.
Specifications
Your checklist manager should meet these specifications:
- Display a program menu with numbers corresponding to the following actions the user can undertake on their checklist: View the list, add to the end of the checklist, remove an item from the checklist by name, remove an item by index, and end the program
- Display each item in the checklist preceded by its index
- Prompt the user to enter a new item which then gets appended to the end of the checklist. Program displays a confirmation message
- Prompt the user to enter the exact text of an item to remove from the checklist. Program should confirm when it is done. You may optionally gracefully fail if the user enters an item that is not in the list using try and except
- Prompt the user to enter the index of an item to remove and then confirm that the item was removed. The program should fail gracefully if the user enters an index out of bounds of the list's index range.
- When the exit code is given, exit the program with a message
Extension exercises
The following features can be implemented in any order or only partially as a way to continue your list learning.
- Implement reversing: Add a reversal function to your program that allows the user to invert the order of their checklist
- Swap items: Add a feature that asks the user for two items by their index and swaps their positions in the list
- Clear list: Add a feature that allows the user to wipe their checklist and start over.
- Delete a range: Study the syntax of deleting by slicing in python's documentation. Then, adjust your program to allow the user to specify a starting to ending index to delete multiple checklist items in one operation. Carefully check the given start and ending index such that your delete operation will succeed, i.e. that the indexes are within bounds.
- Use counting numbers for list items: Non programmery users don't like items getting a number of zero, which is the starting index for python lists (and lists in many other languages). Adjust your code to display the checklist items with numbers starting with 1. Then adjust your delete operation so that the user can enter the counting number for the item to delete (instead of the index). This will require careful adjusting of the user's input back into index range instead of counting range before use then del operation.
- Duplicate warning: Implement a feature that warns the user if they attempt to enter a checklist item that is already in the list. After warning them, ask if they want to proceed or to abort the add operation.
- Empty list warning: Implement a feature that warns the user if they try to remove the last item in the checklist. If they confirm, go ahead and empty the list entirely.
arrow_upward back up to contents
(c)2023 technologyrediscogvery.net | content can freely reproduced according to the site's content use agreement.