Guide: How to say “not in” in Python

4 1 vote
Article Rating

Greetings! In the world of programming, expressing negation is a vital aspect of writing clear and concise code. In Python, when you want to check if a value is not present in a collection or sequence, you can use the “not in” keyword. Sit back, relax, and let’s dive into this guide where we’ll explore formal and informal ways to express “not in” in Python, along with tips, examples, and common use cases.

Formal Expressions

When it comes to formal expressions in Python, the “not in” keyword is the recommended way to check for the absence of a value in a collection. Here’s how you can use it:

Syntax: value not in collection

This syntax allows you to evaluate whether a value is not present in a given collection. Let’s see some examples to illustrate its usage:

Example 1:

fruits = ['apple', 'banana', 'orange'] if 'kiwi' not in fruits: print("Kiwi is not present in the list.")

Output:
“Kiwi is not present in the list.”

Example 2:

numbers = (1, 2, 3, 4, 5) if 6 not in numbers: print("The number 6 is not in the tuple.")

Output:
“The number 6 is not in the tuple.”

As you can see from the examples, the “not in” keyword allows you to check for the absence of a value within a collection with ease.

Informal or Alternate Ways

While the formal “not in” expression is preferred in Python, it’s worth mentioning some informal or alternate ways that can achieve the same result. However, remember that using the formal “not in” syntax is highly recommended for improved code readability and maintainability.

Using the “in” keyword with the logical not operator

An alternate way to express “not in” is by using the “in” keyword along with the logical not operator (“!”) to negate the result. Here’s an example:

Example:

fruits = ['apple', 'banana', 'orange'] if not 'kiwi' in fruits: print("Kiwi is not present in the list.")

Output:
“Kiwi is not present in the list.”

While this achieves the same outcome, it introduces an extra negation symbol, which can make the code slightly less readable. Hence, using the “not in” keyword is more preferred and recommended.

Common Use Cases

The usage of the “not in” keyword and its alternatives can be diverse. Here are some common scenarios where it comes in handy:

  • Checking for the absence of a specific element: By using “not in,” you can easily determine if a particular element is missing in a list, tuple, dictionary, or any other collection.
  • Filtering unwanted values: If you have a dataset or collection and want to exclude specific values, the “not in” keyword can help you achieve this filtering.
  • Conditional control flow: The “not in” expression can be used as a condition within control flow statements, such as if-else or while loops, to perform specific actions based on the absence of a certain value.

These are just a few examples, but the applications of the “not in” keyword are vast and can be tailored to various programming scenarios.

Now that you’ve learned how to express “not in” in Python using formal and informal methods, you’re ready to wield this powerful keyword in your code with ease. Remember to opt for the formal syntax whenever possible, as it contributes to better code readability and maintainability. Happy coding!

4 1 vote
Article Rating
⭐Share⭐ to appreciate human effort 🙏
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top