python中in是关系运算符吗
2023-09-24 03:08:41
是的,in是Python中的关系运算符之一。它用于判断一个值是否存在于一个序列(如字符串、列表、元组等)中。如果存在,则返回True;如果不存在,则返回False。以下是in关系运算符的使用示例:
str1 = "Hello, world!"
if "Hello" in str1:
print("Hello is in str1.") # 输出:Hello is in str1.
else:
print("Hello is not in str1.")
list1 = [1, 2, 3, 4, 5]
if 3 in list1:
print("3 is in list1.") # 输出:3 is in list1.
else:
print("3 is not in list1.")
tuple1 = (6, 7, 8, 9, 10)
if 11 in tuple1:
print("11 is in tuple1.")
else:
print("11 is not in tuple1.") # 输出:11 is not in tuple1.
通过使用in关系运算符,可以方便地判断一个值是否存在于一个序列中。