Previous Lesson: Lesson 11: Escape Sequences in Python

In this lesson, we’ll talk about lists in Python. You’ll know how you can apply and use in and not in and operators on lists in Python.

Let’s create a new file: ex11.py. Then, type these in:

This is an example of a list. The name of our list is dogs. To create a list, we use brackets ([]). An item inside a list is called an element. And we use a commas (,) to separate these elements inside a list. To get our list printed, we just do what we have done on Line 3. That is, putting in the name of the list to print the items.

Now when we run this, it will look like this:

Our list just got printed. Simple enough, eh? Notice that our list contained str. But we can also include different types of values inside a list. For example: an int, a float, or a Boolean. We’ll get back to these in a second. In the meantime, add this to your code, too:

Let’s try it:

Nice! All of them got printed out.

So a str, is what we’ve already talked about and used a lot of times in the previous lessons, is a piece of text that has single or double-quotes surrounding them. An int is an integer, or a number (e.g. 4, 1, 6, 8, etc.).

A float, if you’ve clicked on the link on it, are integers that have decimal points (e.g. 4.0, 1.032, 6.34, 3.14, etc.). And a Boolean is where we can find the True and False. We’ll talk about booleans on the next lessons.

Anyway, did you know that we can also do some operations in lists, the same way that we’re doing on strings? Let’s try it!

Operators on Lists

Let’s do something fun. Let’s have operators on lists in Python:

This is a little mixed up. So be sure that you copy these exactly on your text editor. Let’s run it:

As what we have seen, lists can be joined or added to another list. That’s what we did on Line 3. In addition, they can also be repeated according to a number of your choice, just like what we did on Line 5.

That thing on Line 4 is what we’ve learned from the previous lesson. Be sure that you’ve been there. Anyhow, I put a \n on Line 4 so that the result would be easier to read on our Terminal. Now that’s all about operators on lists in Python.

By the way, did you know that we can check whether an item or something is written on our list?

‘in’ on Lists

We use in to check whether an item is really on our list. This is an example. Type it:

Note: Our Terminal will return a Boolean value (that’s right, you remembered. The True or False). If the item is really listed on our list, then our Terminal will return a value of True. On the other hand, if that item is not in our list, then it will return a False. Let’s see the results:

That’s right–“Max” is really inside our dogs list; there is no “Jane” in our list; “Zedd” is not a dog, I assure you; and “Milo” can be found inside our list. Now, what if we want to check our list using the other way around? “Woah! Are you telling me that there is another way to check a list?” You’ll see.

‘not in’ on Lists

Just as you have guessed, not in is just the opposite of in (of course). When we say that something is not in our list, and we know for sure that there is no such item in our list, then our Terminal will return a True value. But, if we say that something is not in our list, and yet it is in our list, then the Terminal will return a False.

Before we run this–before you run this–I want you to comment out your answers beside these lines of code. You remember how to do that. Just use the octothorpe:

Be honest. And then run it:

Did you get the same answers? Nevertheless, don’t worry if you didn’t. Try and try; have many experiments on this one. If you have any questions, post them down below on the Comments section. See you in the next lesson! We’re still gonna talk about lists on the next lesson!

Next Lesson: Lesson 13: (Part I) Accessing and Nesting on Lists in Python

4 Replies to “Lesson 12. In, Not In, and Operators on Lists in Python”

Leave a Reply

Your email address will not be published. Required fields are marked *