Python in 關鍵字

實例

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

fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
  print("yes")

運行實例

定義和用法

in 關鍵字有兩種作用:

in 關鍵字用于檢查序列(列表、范圍、字符串等)中是否存在值。

in 關鍵字還用于在 for 循環中迭代序列:

實例

遍歷列表并打印項目:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

運行實例