Python第三天(列表+元组+集合)

列表

1.创建列表

数组:存储同一数据类型的集合 score = [10,20,30]

列表:可以存储任意数据类型的集合

1.创建一个列表name并给里面储存数据

In [16]: name = ['tom','bob','coco','alice']
In [17]: name                                                           
Out[17]: ['tom', 'bob', 'coco', 'alice']

In [18]: type(name)                    	##这里查看列类型为列表                                 
Out[18]: list

2.列表里可以存储不同的数据类型

li = [1,1.2,'hello',True]
print(li)
print(type(li))

[1, 1.2, 'hello', True]
<class 'list'>

3.列表嵌套

li1 = [1,1.2,'hello',True,[1,2,3,4,5]]
print(li1)
print(type(li1))

[1, 1.2, 'hello', True, [1, 2, 3, 4, 5]]
<class 'list'>

2.列表的特性

service = ['http','ssh','ftp','dns']

1.索引

 print(service[0])
 print(service[-1])

http
dns

2.切片

print(service[1:])
print(service[:-1])
print(service[::-1])

['ssh', 'ftp', 'dns']
['http', 'ssh', 'ftp']
['dns', 'ftp', 'ssh', 'http']

3.重复

print(service * 3)

['http', 'ssh', 'ftp', 'dns', 'http', 'ssh', 'ftp', 'dns', 'http', 'ssh', 'ftp', 'dns']

4.连接

service1 = ['mysql','firewalld']
print(service + service1)

['http', 'ssh', 'ftp', 'dns', 'mysql', 'firewalld']

5.成员操作符

print('mysql' in service)
print('mysql' in service1)

False
True

6.迭代

print('显示所有服务'.center(50,'*'))
for se in service:
    print(se)

**********************显示所有服务**********************
http
ssh
ftp
dns

7.列表里嵌套列表

service2 = [['http',80],['ssh',22],['ftp',21]]
1.索引
print(service2[0][1])
print(service2[-1][1])

80
21

2.切片
print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])

['ssh', 22]
['http', 80]
['http']

3.列表的增加

service = ['http','ssh','ftp','dns']
1.普通方法
print(service + ['firew
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值