Python in 关键字
Python基础 2022-06-06 16:26:31小码哥的IT人生shichen
Python in 关键字
实例
检查列表中是否存在 "banana":
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("yes")
完整实例:
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("yes")
定义和用法
in 关键字有两种作用:
in 关键字用于检查序列(列表、范围、字符串等)中是否存在值。
in 关键字还用于在 for 循环中迭代序列:
实例
遍历列表并打印项目:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
完整实例:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)