Advanced Python road in and not in operators

Disclaimer: This tutorial learning exchanges only, not for commercial use. https://blog.csdn.net/weixin_45086637/article/details/91427847

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 ❹.

Guess you like

Origin blog.csdn.net/weixin_45086637/article/details/91427847