Python for beginners [Lists]

Python for beginners [Lists]

Introduction

In this part of the series, we will go over lists and what we can do. We will go over how to create a list, change a list, append something to a list, and some other valuable things that you will need to know to start. Without beating a dead horse, let's just get right to it.

What is a list?

A list is what it sounds like, a list of data stored in a Python variable. They appear in a specific order; they are mutable (you can change them), and they don't have to be unique. Just like when you go to the grocery store to purchase items for your dinner, you can make one of these in Python.

Lets say 'Old Gregg' is going to go get his dinner and he needs to log it in a computer for keeping until later (typically you would want to use a database for this, but for the sake of this tutorial we will use a list.)

With lists, you can have strings and integers, floats, or even more lists. Super cool, right? Let's try that out with Gregg's list:

To verify what data type we have in the list, we can use type() that we learned in the string lesson here.

Getting data from your list

Getting information from your list is easy; all you have to do is call the number associated with the value you are searching. Let's say we wanted to get some of that data and print it out in a sentence. We need to call the variable in which the list is assigned, then wrap the number (remember, computers start counting with '0') in brackets. We will go back to the original example of Old Gregg's grocery list.

Pretty cool, right? What if Old Gregg got something off his list?

Deleting an entry from your list

Well, it looks like Old Gregg made it to the market and picked up his Baileys as well as a shoe to drink it in. We need to drop these two data points in our list. How can we do this without modifying the original list? That's as easy as casting your line and catching a scaly man-fish.

Adding to your list

We need to add another item for Old Gregg to pick up when he goes to the market. Old Gregg likes art, and he needs more watercolors. We need to add those to the list. What we use for that is the append() method. Keep in mind, using append() will only add in at the end of your list.

Slicing lists

Old Gregg doesn't want Asparagus or Fish this evening; he will just be drinking Bailey's. He also forgot that he had already picked up the disco lights for the Cave, and he had a backup set of Watercolors. Let's slice up this list and save it to a new variable. We covered this before in the deleting something from your list section.

Let's see a different example of this that is slightly more complicated. We will want to keep Fish and Disco lights for the Cave in this example. How can we do that?

Finally, we will go over how to change something in a list.

As always, I am still going to point you to the docs. These are super important, and this is where you should be searching for information if you have a question about something. Python has good documentation, and you should get accustomed to looking there for answers before seeking help online; it will make you a better programmer.

I'd like to hear from you: What part of using lists do you think you will use the most? Did I cover everything about what a beginner would use to dive into understanding lists? If I didn't, please let me know by leaving a comment below, and I will add it to the post! My goal is to help more people love Python like I do!

5mr5s7.jpg