operators in and not in
In use and not in operators, can determine whether a value in the list. Like other operators, like, in and not in use in the expression, connecting the two values: To find a value in the list, and the list to be looking for. These expressions will evaluate to a Boolean value.
>>> 'howdy' in ['hello','hi','howdy','heyas']
True
>>> spam = ['hello','hi','howdy','heyas']
>>> 'cat' in spam
False
>>> 'howdy' not in spam
False
>>> 'cat' not in spam
True
For example, the following program allows the user to enter the name of a pet, then check whether the name in the pet list. Enter the following code:
mypets = ['zophie','pooka','fattail']❶
print('Enter a pet name:')
name = input()
if name not in mypets:❷
print('I do not have a pet named ' + name)❸
else:
print(name + ' is my pet.')❹
First, create a list ❶, and then save the name to the string entered by the user via variable, by determining whether the user input string ❷ exist in the list, if the string entered by the user does not exist in mypets list, then output ❸. If the user entered character string is present in the list, then the output ❹.