Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we'll create a program that accepts 5 numbers from the user and calculates their average. Can anyone tell me how we could start this?
Maybe we could use a list to store the numbers?
Correct! We can use a list to store the user inputs. Who remembers how to get input from a user?
We can use the input() function in Python.
Exactly! After collecting all entries, we'll need to calculate the average. Does anyone recall how to get the average from a list?
We can sum the list and then divide by the length of the list.
Right again! Let’s go through the code together step-by-step. Here’s how your final code structure could look:
Start with an empty list, numbers = []. Then loop five times with for _ in range(5):, turn each answer into a number with float(input('Enter a number: ')) and append() it to the list. Finally, work out average = sum(numbers) / len(numbers) and show it with print('The average is:', average).
Can you all see how we effectively used a list to make our program dynamic? Make sure to practice this on your own!
Next, we’re going to work on a shopping list application. How would you start this?
We might need a list to hold the items.
Yes, a list will be perfect! What methods could we use to add or remove items from the list?
We can use append() for adding and remove() for removing items.
Excellent! How about printing the entire list afterward?
We can directly print the list after each operation!
Absolutely! Here's an example structure for your shopping list application:
Start with an empty list, shopping_list = [], and use a while True: loop that asks the user to type add, remove, view or quit. For add, call shopping_list.append(item); for remove, check if item in shopping_list: first and then call shopping_list.remove(item), so a missing item doesn't crash the program; for view, print(shopping_list); and for quit, break out of the loop. Try building it on your own!
This section presents exercises aimed at applying the concepts of lists in Python through practical programming challenges.
In this section, learners will tackle exercises focused on utilizing lists to complete tasks such as calculating averages, managing a shopping list, and creating a tic-tac-toe board in a matrix format, which reinforces their understanding of list operations and methods.
This section engages learners with practical programming exercises using lists in Python. The exercises are designed to solidify the understanding of list creation, manipulation, and nested lists through real-world applications. By accepting user input for numbers and implementing a shopping list with add/remove functionalities, students practice key concepts like indexing, list methods, and more. Moreover, creating a tic-tac-toe board challenges students to work with nested lists, thus enhancing their comprehension of this powerful data structure.