004: 基本数据类型-List

List 也就是数组,但是这个数组是动态的,用中括号与逗号创建。

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

1) Like strings (and all other built-in sequence type), lists can be indexed and sliced.

2) All slice operations return a new list containing the requested elements.

3) Lists also support operations like concatenation.用 + 进行连接

4) Unlike strings, which are immutable, lists are a mutable type.

5) You can also add new items at the end of the list, by using the append() method.

6Assignment to slices is also possible, and this can even change the size of the list or clear it entirely.

7) The built-in function len() also applies to lists.

8) It is possible to nest lists (create lists containing other lists).

9) There is a way to remove an item from a list given its index instead of its value: the del statement.

del list_example[index]
del list_example[start:stop]

以下是今天做的一些小练习

关于list的slice:

1,一共可以接受三个参数,start, stop, step. 其中start与stop是必选参数, step是可选参数, 默认为1。

start可以接受负数,如果为负数,则意思为从倒数第几个开始。

stop也可以接受负数,如果为负数,则意思为倒数几个结束。

如果step为正数,需要 stop>start 才有意义, 如果step为负数, 需要stop<start才有意义。

a = [1,2,3,4,5]
a[:] # [1, 2, 3, 4, 5]
a[0:3] # [1, 2, 3]
a[2:5] # [3, 4, 5]
a[-3:-1] # [3, 4]

a[::-1] # [5, 4, 3, 2, 1]
a[-1:-5:-1] # [5, 4, 3, 2] 
原文地址:https://www.cnblogs.com/jcsz/p/5089276.html