列表
列表:可以存储任意数据类型的集合
>>> name = ['tom','Tony','coco']
>>> name
['tom', 'Tony', 'coco']
>>> type(name)
<class 'list'>
#列表里:可以存储不同的数据类型
>>> list = [1,1.2,True,'hello']
>>> print(list)
[1, 1.2, True, 'hello']
>>> print(type(list))
<class 'list'>
#列表里也可以嵌套列表(列表:本身也是一种数据类型)
>>> list1 = [1,1.2,True,'hello',[1,2,3,4,5]]
>>> print(list1)
[1, 1.2, True, 'hello', [1, 2, 3, 4, 5]]
>>> print(type(list1))
<class 'list'>
>>> service = ['http','ssh','ftp']
# 索引
>>> print(service[0])
http
>>> print(service[-1])
ftp
# 切片
>>> print(service[1:])
['ssh', 'ftp']
>>> print(service[:-1])
['http', 'ssh']
>>> print(service[::-1])
['ftp', 'ssh', 'http']
# 重复
>>> print(service * 3)
['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']
# 连接
>>> service1 = ['mysql','firewalld']
>>> print(service + service1)
# 成员操作符
['http', 'ssh', 'ftp', 'mysql', 'firewalld']
>>> print('firewalld' in service)
False
>>> print('firewalld' in service1)
True
# for循环(迭代)
>>> for se in service:
... print(se)
...
http
ssh
ftp
# 列表里嵌套列表
>>> service2 = [['http',80],['ssh',22],['ftp',21]]
# 索引
>>> print(service2[1][1])
22
>>> print(service2[-1][1])
21
# 切片
>>> print(service2[:][1])
['ssh', 22]
>>> print(service2[:-1][0])
['http', 80]
>>> print(service2[0][:-1])
['http']
>>> service = ['http','ssh','ftp']
# append:追加一个元素到列表中
>>> service.append('firewalld')
>>> print(service)
['http', 'ssh', 'ftp', 'firewalld']
# extend:拉伸 追加多个元素到列表中
>>> service.extend(['mysql','firewalld'])
>>> print(service)
['http', 'ssh', 'ftp', 'firewalld', 'mysql', 'firewalld']
# insert:在指定索引位置插入元素
>>> service.insert(1,'samba')
>>> print(service)
['http', 'samba', 'ssh', 'ftp',