在Python中,`in` 是一个成员操作符,用于检查一个元素是否存在于序列(如列表、元组、字典、集合或字符串)中。如果元素存在于序列中,`in` 操作符返回 `True`,否则返回 `False`。
1. 在字符串中检查字符:
a = "hello"if 'l' in a:print('l 在a 内')else:print('l 不在 a 内')
2. 在列表、元组、集合、字典中检查元素:
tup = (1, 2, 3, 4)list_ = [1, 2, 3, 4]sets = {1, 2, 3, 4}dicts = {'a': 1, 'b': 2}if 1 in tup:print('元组内有1')if 1 in list_:print('列表内有1')if 1 in sets:print('集合内有1')if 'a' in dicts:print('字典内有a')

3. 在字符串中检查子字符串:
a = "hello world"if "world" in a:print('world 在a 内')else:print('world 不在 a 内')
4. 在字典中检查键或值:
dicts = {'a': 1, 'b': 2, 'c': 3}if 'a' in dicts:print('字典内有a')if 1 in dicts.values():print('字典中值有1')
`in` 操作符也可以用于检查元素是否在集合中,集合是无序且元素唯一的集合类型。
希望这些示例能帮助你理解Python中 `in` 操作符的用法
