Python in 关键字

实例

检查列表中是否存在 "banana":

  1. fruits = ["apple", "banana", "cherry"]
  2.  
  3. if "banana" in fruits:
  4. print("yes")

定义和用法

in 关键字有两种作用:

in 关键字用于检查序列(列表、范围、字符串等)中是否存在值。

in 关键字还用于在 for 循环中迭代序列:

实例

遍历列表并打印项目:

  1. fruits = ["apple", "banana", "cherry"]
  2.  
  3. for x in fruits:
  4. print(x)