Python学习笔列表之in和not in操作

随笔记录方便自己和同路人查阅。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  利用in和not in操作符,可以确定一个值是否在列表中。像其他操作符一样,in和not in用在表达式中,

连接两个值:一个要在列表中查找的值,以及待查找的列表。这些表达式将求值为布尔值。

  in和not in结果相反,看下面的示例代码。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  1、in操作符,检查值在列表中示例代码:

list_cat =['Zophie', 'Pooka', 'Simon', 'Lady Macbeth']#定义一个列表
if 'Zophie' in list_cat:#使用in检查'Zophie'是否在列表中,在为True,在为False
    print('True')
else:
    print('False')

  运行结果:

  检查值不在列表内的值

list_cat =['Zophie', 'Pooka', 'Simon', 'Lady Macbeth']#定义一个列表
if '1' in list_cat:#使用in检查'Zophie'是否在列表中,在为True,在为False
    print('True')
else:
    print('False')

  与性能运行结果:

  2、not in操作符,检查值在列表中示例代码:

list_cat =['Zophie', 'Pooka', 'Simon', 'Lady Macbeth']#定义一个列表
if 'Zophie' not in list_cat:#使用in检查'Zophie'是否在列表中,不在为True,在为False
    print('True')
else:
    print('False')

  运行结果:

  检查值不在列表中示例代码:

list_cat =['Zophie', 'Pooka', 'Simon', 'Lady Macbeth']#定义一个列表
if '1' not in list_cat:#使用in检查'Zophie'是否在列表中,不在为True,在为False
    print('True')
else:
    print('False')

  运行结果:

原文地址:https://www.cnblogs.com/lirongyang/p/9535405.html