📜  isnotin python (1)

📅  最后修改于: 2023-12-03 14:42:10.975000             🧑  作者: Mango

Introduction to 'isnotin' in Python

The 'isnotin' is a built-in function in Python that allows you to check if an element is not present in a given sequence or iterable.

Syntax:

The basic syntax of the 'isnotin' function is as follows:

isnotin(element, sequence)
  • 'element': The element to be checked for presence in the sequence.
  • 'sequence': The sequence or iterable in which to check for the element.
Return Value:

The 'isnotin' function returns True if the element is not present in the sequence, and False otherwise.

Examples:

Let's see some examples to understand how 'isnotin' can be used:

Example 1: String in List
people = ["Alice", "Bob", "Charlie"]
if isnotin("David", people):
    print("David is not in the list.")
else:
    print("David is in the list.")

Output:

David is not in the list.
Example 2: Integer in Tuple
numbers = (1, 2, 3, 4, 5)
if isnotin(6, numbers):
    print("6 is not in the tuple.")
else:
    print("6 is in the tuple.")

Output:

6 is not in the tuple.
Example 3: Character in String
word = "Hello"
if isnotin("b", word):
    print("The character 'b' is not in the string.")
else:
    print("The character 'b' is in the string.")

Output:

The character 'b' is not in the string.
Example 4: Dictionary Key
person = {"name": "John", "age": 30, "city": "New York"}
if isnotin("age", person.keys()):
    print("The key 'age' is not present in the dictionary.")
else:
    print("The key 'age' is present in the dictionary.")

Output:

The key 'age' is not present in the dictionary.
Conclusion:

The 'isnotin' function is a useful tool in Python for checking the absence of an element in a sequence or iterable. It helps in writing concise and readable code when you need to perform such checks.