top of page

Learning Python Lists with Ant-Man! 🐜


Hello, young superhero coders! Today, we're going to learn about Python lists. You can think of lists like Ant-Man's backpack. He can put lots of different things in it, like his suit, some snacks, or even his tiny friends! Just like Ant-Man's backpack, a Python list can hold lots of different things. Let's dive in!




What is a List?


In Python, a list is a collection of items. You can put as many things as you like into your list, just like how Ant-Man can put many things in his backpack. Here's how you make a list in Python:


Here, we've made a list called `backpack`. It has three items in it: `'suit'`, `'snacks'`, and `'friends'`.


Accessing Items in a List


What if Ant-Man wants to get something from his backpack? He would have to reach inside and pull it out. In Python, we do that by accessing an item in the list. Each item in a list has an "address", which we call an index. This is how we can access items in a list:


This will output `suit`. Notice that we start counting from 0, not 1. So the first item is at index 0, the second item is at index 1, and so on.


Adding Items to a List


What if Ant-Man finds a cool gadget and wants to add it to his backpack? In Python, we can also add items to our list. Here's how:


Now, the output will be `['suit', 'snacks', 'friends', 'cool gadget']`. Notice that `'cool gadget'` was added to the end of the list.


Removing Items from a List


Sometimes, Ant-Man might want to eat his snacks, so he needs to remove them from his backpack. In Python, we can remove things from our list. Here's how:



Now, the output will be `['suit', 'friends', 'cool gadget']`. Notice that `'snacks'` is no longer in the list.


Practice Time! 🥳


Let's put your new knowledge to the test with some exercises!


Exercise 1: Create a list called `superheroes`. Add the names of your favourite superheroes to the list. Then print your list.


Exercise 2: Oh no! A villain has snuck into your list of superheroes. Remove the villain from your list, then print your list again to make sure they're gone.


Remember, learning to code is like becoming a superhero. It might be a little challenging at first, but keep practicing and you'll be saving the world with your code in no time! Happy coding, mini-Ant-Men and mini-Wasps! 🐜🐝💻🌟

Comments


bottom of page