以下是Python代码示例,用于检查列表中是否存在特定的值。
def check_value_existence(value, my_list):
if value in my_list:
print("Value exists!")
else:
print("Value does not exist.")
my_list = [1, 2, 3, 4, 5]
check_value_existence(3, my_list) # Output: Value exists!
check_value_existence(6, my_list) # Output: Value does not exist.
这个示例定义了一个名为“check_value_existence”的函数,它接受两个参数:要搜索的值和要在其中搜索值的列表。如果值存在于列表中,则返回“值存在!”,否则返回“值不存在”。函数调用时需要传递两个参数:要搜索的值和包含搜索值的列表。