数据类型
列表
1.列表的内容可以是字符串,浮点数,整数,列表等
2.如果在初始化时两个中括号[]中间是空的将会产生一个不带任何元素的空列表
3.列表的特效
1.集合性的数据类型
2.集合是有序的(所以可以用下标来访问列表里的元素)
x=[1,3.14,'hello',['a','b']]
print(x[0])
#输出将会输出第一个元素 1
>>>1
#为什么输出的是1而不是3.14或其他,因为下标是从0开始算起的
#[索引]访问元素列表
##[索引][索引]双重索引
#例子
print(x[2][2])
#将会输出'hello'中的'l'
3.列表可更新
例子
```python
x=[1,3.14,'hello',['a','b']]
x[0]=100
print(x)
#将会输出[100,3.14,'hello',['a','b']]
```
4.append()函数
例子
```python
x=[1,3.14,'hello',['a','b']]
#append()函数 在列表末尾增加元素
x.append("Cole")
print(x)
#将会输出[100,3.14,'hello',['a','b'],"Cole"]
```
5.字符串类型变量无法修改
6.len() 函数 查看字符串长度
```python
x=[1,3.14,'hello',['a','b']]
print(len(x))
#输出的结果是4
```
7.insert()函数 在指定位置插入元素
```python
x=[1,3.14,'hello',['a','b']]
x.insert(1,'book')
print(x)
#insert(下标,要插入的内容)
#将会输出[1, 'book', 3.14, 'hello', ['a', 'b']]
```
8.extend() 函数 扩展列表
extend()函数会把列表中的每一个元素依次加入另一个列
表中
```python
x=[1,3.14,'hello',['a','b']]
y=['Tom','Jerry']
x.extend(y)
print(x)
#运行结果为[1, 3.14, 'hello', ['a', 'b'], 'Tom', 'Jerry']
```
9.从列表中删除元素
1.pop() 函数 从列表尾部删除元素
```python
x=[1,3.14,'hello',['a','b']]
b=x.pop()
print(b)
#将会输出['a', 'b']
print(x)
#因此再输出x就会得到[1, 3.14, 'hello']
```
2.remove() 函数 删除列表特定元素
如果列表中存在要删除的值将会删除
如果不存在将会报错
```python
x=[1,3.14,'hello',['a','b']]
x.remove(3.14)
print(x)
#运行结果是[1, 'hello', ['a', 'b']]
x.remove(3.14)
print(x)
#运行完上边的代码再运行结果会报错
```
3.del 关键字 指定位置删除元素
```python
x=[1,3.14,'hello',['a','b']]
del x[1]
print(x)
#结果为[1, 'hello', ['a', 'b']]删除了下标为1的元素
```
10.index() 函数 获取函数索引
x = [1, 3.14, 'hello', ['a', 'b']]
y=x.index(3.14)
print(y)
#输出结果为1