Wednesday, July 27, 2022

What is in/not in operator?

The in operator is the boolean operator that takes two strings and compares them. The output will be True if the first string appears as a substring in the second string. The output will be False if the string does not appear as a substring in the second string.
Example 1 Check if the string "and" is a subset of a strings "Sand", "Candy", "Land", and "Lent".
Solution: First assign each string to a specific variable name.
string1 = "and"
stringCheck1 = "Sand"
stringCheck2 = "Candy"
stringCheck3 = "Land"
stringCheck4 = "Lent"
Now we will check if string1 is a substring of the string assigned to variable names stringCheck1, stringCheck2, stringCheck3, and stringChekc4.
print(string1 in stringCheck1)
print(string1 in stringCheck2)
print(string1 in stringCheck3)
print(string1 in stringCheck4)
The output of the previous block of code will be True for all cases except the last. In the last case, we want to know if the string "and" is a substring of a string "Lent". Since the "and" is not a substring of the aforementioned string the Python will return False as the result.
True
True
True
False
The in operator works with iterables i.e. lists or strings. This operator is useful in case you want to check if the element is found in the string or list. The result of in operator is True if the element is found in a list or a string. The result of in operator is False if the element is not found in the list.
Example 2 Check if word "phone" is in the list ["Computer", "tablet", "phone"].
Solution Assign the phone to a variable name var1 and list to a variable name list1.
var1 = "phone"
list1 = ["Computer", "tablet", "phone"]
To check if the "phone" is in the list use the "in" operator.
print("var1 in list1 = {}".format(var1 in list1))
Output:
var1 in list1 = True
The statement var1 in list1 is True since the string "phone" is in the list. So, the result True is expected. Now we will apply the if-else statement to check if the "phone" is in the list1. If the "phone" is in the list the output will be "Word 'phone' is found in the list1." On the other hand, if the "phone" is not found in the list1 the program will print the string "Word 'phone' is not found in the list1."
if var1 in list1:
print("Word 'phone' is found in the list1")
else:
print("Word 'phone' is not found in the list1.")
The entire program code is given below.
var1 = "phone"
list1 = ['Computer', 'tablet', 'phone']
if var1 in list1:
print("Word 'phone' is found in the list1")
else:
print("Word 'phone' is not found in the list1.")
The output of the program will always be "World 'phone' is found in the list1." since the word "phone" is actually in the list.
Output:
World 'phone' is found in the list1.
To test the else statement we will create a USER_INPUT variable that will ask the user to type in the string that will be searched in the list1. To do this we will need the input built-in Python function. After the user enters a string using in operator Python will check if the string defined by the user is in the list1.
USER_INPUT = input("Enter a string:> ")
list1 = ['Computer', 'tablet', 'phone']
if USER_INPUT in list1:
print("Word '{}' is found in the list1.".format(USER_INPUT))
else:
print("Word '{}' is not found in the list1.".format(USER_INPUT))
Output_1:
Enter a string:> Smart Watch
Word 'Smart Watch' is not found in the list1.
Output_2:
Enter a string:> tablet
Word 'tablet' is found in the list1.

The "not in" operator.

The "not in" operator is used to check if a string is not in the list or not the substring element of another string. The result of not in operator is True if the string is not part of iterable i.e. list or a substring of another string. The result of not in operator is "False" if the string is part of an iterable i.e. list or a substring of another string.
Example 3 Create a program that will investigate if a string entered by a user is/is not in the list ["Computer", "game console", "smartwatch"] using the not in operator.
Solution: The USER_INPUT variable will contain the string entered by the user. The list will be assigned to a variable name list2
USER_INPUT = input("Enter a string:> ")
list2 = ["Computer", "game console", "smartwatch"]
Now we have to check if the string is or is not in the list2 using the not in operator.
if USER_INPUT not in list2:
print("The {} is not list2 element.".format(USER_INPUT))
else:
print("The {} is list2 element.".format(USER_INPUT))
Output_1:
Enter a string:> GPU
The GPU is not list2 element.
Output_2:
Enter a string:> game console
The game console is list2 element.

No comments:

Post a Comment