python 判断元素在不在list中
时间: 2023-04-10 07:02:27 浏览: 216
可以使用 in 关键字来判断元素是否在 list 中,示例代码如下:
```
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 在列表中")
else:
print("3 不在列表中")
```
输出结果为:3 在列表中
相关问题
python判断元素是否在list中
可以使用 "in" 关键字来判断元素是否在列表中,例如:
```python
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 is in the list")
else:
print("3 is not in the list")
```
输出结果为:"3 is in the list"。
python判断元素在列表中
你可以使用 `in` 关键字来判断一个元素是否在列表中,例如:
```python
my_list = [1, 2, 3, 4, 5]
if 3 in my_list:
print("3 is in the list")
else:
print("3 is not in the list")
```
输出:
```
3 is in the list
```
如果要判断元素不在列表中,可以使用 `not in` 关键字:
```python
my_list = [1, 2, 3, 4, 5]
if 6 not in my_list:
print("6 is not in the list")
else:
print("6 is in the list")
```
输出:
```
6 is not in the list
```