# Python 清单

> 原文： [https://pythonspot.com/python-lists/](https://pythonspot.com/python-lists/)

列表是一个序列和一个基本的数据结构。 列表可以包含字符串（文本）和数字。 列表类似于其他编程语言中的数组，但具有其他功能。

## Python 清单

We define lists with brackets []. To access the data, these same brackets are used.
Example list usage:

```py
#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l)     # prints all elements
print(l[0])  # print first element
print(l[1])  # prints second element

```

## 添加/删除

We can use the functions append() and remove() to manipulate the list.

```py
#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l)                # prints all elements
l.append("Victoria")   # add element.
print(l)                # print all elements
l.remove("Derp")       # remove element.
l.remove("Drake")      # remove element.
print(l)               # print all elements.

```

## 排序清单

We can sort the list using the sort() function.

```py
#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l)     # prints all elements
l.sort()    # sorts the list in alphabetical order
print(l)     # prints all elements

```

如果要按降序排列列表，只需使用 reverse（）函数。

```py
#!/usr/bin/python

l = [ "Drake", "Derp", "Derek", "Dominique" ]

print(l)     # prints all elements
l.sort()    # sorts the list in alphabetical order
l.reverse() # reverse order.
print(l)     # prints all elements

```

[下载 Python 练习](https://pythonspot.com/download-python-exercises/)