python中列表类型

        列表是指一系列的按特定顺序排列的元素组成;是python中内置的可变序列;在python中使用[ ]定义列表,元素与元素之间使用英文的逗号分隔。

        列表中的元素可以是任意的数据类型。

        列表的创建方式有两种:

1.使用[]直接创建列表

如:列表名=[element1,element2,element3,.....,elementN]

2.使用内置函数list()创建列表

如:列表名=list(序列)

3.列表的删除

del 列表名

#1.使用[]直接创建列表
lst = ['hello','world',98,100.5]
print(lst)

#可以使用内置函数list()创建列表
lst2=list('helloworld')
lst3=list(range(1,10,2))#从来开始到时候0结束,步长为什么,不包含10
print(lst2)
print(lst3)

#列表是序列的一种,对序列的操作符,运算符,函数均可以使用
print(lst+lst2+lst3)#相加操作
print(lst*3)#序列相乘操作
print(len(lst))
print(max(lst3))
print(min(lst3))

print(lst2.count('o'))#统计o的个数
print(lst2.index('o'))#o在列表lst2中第一次出现的位置

#列表的删除操作
lst4= [10,20,30]
print(lst4)
#删除列表
del lst4
#print(lst4) NameError: name 'lst4' is not defined. Did you mean: 'lst'?

4.列表的遍历操作

enumerate函数的使用语法结构:

for index ,item in enumerate(lst):

输出index和item

lst= ['hello','world','python','li']
#使用遍历循环for遍历列表元素
for item in lst:
    print(item)

#使用for循环,range()函数,len()函数,根据索引进行遍历
for i in range(0,len(lst)):
    print(i,'---> ',lst[i])

#第三种遍历方式enumerate()函数
for index,item in enumerate(lst):
    print(index,item)#index是序号,不是索引
#手动修改序号的起始值
for index,item in enumerate(lst,start=1):
    print(index,item)
    
for index,item in enumerate(lst,1):
    print(index,item)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值