247个python案例
1. 访问列表元素
```python
my_list = [1, 2, 3]
print(my_list[0]) # Output: 1
```
2. 修改列表元素
```python
my_list = [1, 2, 3]
my_list[0] = 4
print(my_list) # Output: [4, 2, 3]
```
3. 遍历列表
```python
my_list = [1, 2, 3]
for item in my_list:
print(item) # Output: 1, 2, 3
```
4. 检查列表成员
```python
my_list = [1, 2, 3]
if 2 in my_list:
print("Yes, 2 is in the list") # Output: Yes, 2 is in the list
```
5. 查列表索引
```python
my_list = [1, 2, 3]
index = my_list.index(2)
print(index) # Output: 1
```
6. 添加元素到列表
```python
my_list = [1, 2, 3]
my_list.append(4)
print(my_list) # Output: [1, 2, 3, 4]
```
7. 从列表中删除元素
```python
my_list = [1, 2, 3]
sortedlist ve(2)
print(my_list) # Output: [1, 3]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论