
上QQ阅读APP看书,第一时间看更新
1.5.5 元素检查
通过使用“in”或“not in”保留字检查某个元素是否为序列的成员,基本语法为:
value in sequence value not in sequence
其中,value表示被检查的元素,sequence表示相应的序列。
示例如下,在Spyder代码编辑区内输入以下代码并逐行运行:
list1 = [1,3,5,7,9] # 生成列表list1,其中的元素均为数字 print(3 in list1) # 检查列表list1中是否包含数字3 print(4 not in list1) # 检查列表list1中是否不包含数字4
可在IPython控制台看到如图1.42所示的运行结果。

图1.42 运行结果