Python 串列(List) in 和 not in 運算子可以用來檢查元素是否在串列中,它們是Python中常用的運算子,可以用來檢查元素是否存在於串列中。

Python 串列 in 運算子

Python 串列 in 運算子可以用來檢查元素是否存在於串列中,它會回傳True或False,如果元素存在於串列中,則回傳True,反之則回傳False。

list1 = [1, 2, 3, 4, 5]

# 檢查元素3是否存在於list1中
print(3 in list1)  # 回傳True

# 檢查元素6是否存在於list1中
print(6 in list1)  # 回傳False

Python 串列 not in 運算子

Python 串列 not in 運算子可以用來檢查元素是否不存在於串列中,它會回傳True或False,如果元素不存在於串列中,則回傳True,反之則回傳False。

list1 = [1, 2, 3, 4, 5]

# 檢查元素6是否不存在於list1中
print(6 not in list1)  # 回傳True

# 檢查元素3是否不存在於list1中
print(3 not in list1)  # 回傳False

總結來說,Python 串列 in 和 not in 運算子可以用來檢查元素是否存在於串列中,它們是Python中常用的運算子,可以用來檢查元素是否存在於串列中,並且可以回傳True或False。

Categorized in: