小码哥的IT人生

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)

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024