How to Train Your Python: Part 12, Logical and Membership Operators

Part 12, Logical and Membership Operators

Welcome back! In the last round of python training, we talked about tuples and dictionaries. We'll be diverging again and talking about logical operators as well as membership operators. These are used very similiar to booleans, so if you haven't read that, I suggest you do so first.

We'll start this discussion by explaining what a logical/member operator is and it's relation to booleans, then we'll list the logical/member operators and use them in examples! So, let's get started!

A Side Note

Keep in mind that logical and membership operators are not part of the same group, hence the name difference. But we'll be discussing them at the same time because in most cases we'll be using them together.

What Are Logical and Membership Operators?

First, we'll start by defining operator. An operator is something that can affect a true or false outcome. More specifically, there are different types of operators, but today we'll only be discussing the logical and membership types. Logical operators can be used to chain conditional statements together to make one, or to test for a false value instead of a true one. Membership operators are used to evaluate a given elements presence.

Let's move on to talking about the specific operators in the logical operator family, then we'll discuss the membership family.

The Logical Operators Family

There are three operators in the logical operators family, these operators are as follows...

  • and
  • or
  • not

The first two, 'and' and 'or', and be used to chain conditional statements together. If we wanted to test for two conditions in one statement, we would write two separate statements and put the 'and' operator between them. If both conditions are true, the result is true. But if either of the conditions are false, the answer is false. Let's take a look at this in action.

We can see here that when we use the 'and' operator and true and true, it returned true. But when we used to the 'and' operator and true and false, it returned false. Let's move on to our second logical operator, 'or'.

Since we used 'and' to see if both our conditions were true, we can use 'or' to see if either of our conditions are true. Let's take a look at this.

We can see in the screenshot above, that using or and true and true returned true, it also returned true when we used it on true and false. This is useful in situations where it's okay if one or more conditions is false. Let's take a look at our last logical operator, not.

We can use not to invert the result of a condition statement. It turns true values into false, and false values into true. Before we talk about how this is useful, let's see some examples.

We can see some good examples of not here. One is obviously greater than zero, but with the not operator, the result was inverted, and it returned false. We also have some generic examples of not true being false and not false being true. That's it for logical operators, on to the membership family!

The Membership Operators Family

The membership family of operators is much simpler than the logical family, but just as useful. We can use the membership operators to verify the presence of a given item. This may sound confusing, but it's actually quite simple! The two operators in this family are...

  • in
  • not in

We've already discussed the usage of 'not', so we're only going to be discussing 'in'.

We can use 'in' to return true or false as to the presence of a specified item. As an example, we'll be making a list with quite a few elements, and we'll be using 'in' in order to ask about the presence of our item in a conditional statement. Let's go ahead and make our list...

Alright, we have our list, and it's a good sized one. Let's see if we can find some names within this list using 'in'...

Alright. Here we see that we can used our 'in' operator to test for values inside of our list. Often times we will use these two families together to make very specific conditional statements.

Wrapping It Up

We've covered two families of operators here, logical and membership. We'll be using them together in many situations. These operators will be with us for our entire scripting career!


I'm very happy that we got these operators out of the way! Now we can move on to advanced conditional statements! Whoops, spoiler! I'll see you for you're python's next training session!

Thank you for reading!


Just updated your iPhone? You'll find new features for Podcasts, News, Books, and TV, as well as important security improvements and fresh wallpapers. Find out what's new and changed on your iPhone with the iOS 17.5 update.



out of curiosity, what is the difference between or and xor?

Xor is shirt for exclusive-or. Which returns true if one is true, but not both.

True or True is true.
True xor True is False.

Share Your Thoughts

  • Hot
  • Latest