python基础语法 第3关_Python基础语法 第3节课 (列表)

[ ]列表

一、列表可以存放的内容

1.列表可以存放哪些类型?

列表是一个大仓库,百宝箱,所学过的所有类型,都可以放在列表里面。

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

print(my_list)

结果:[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

2.列表的长度len()

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]print(len(my_list)) #7

3.获取列表中的元素---索引(类似字符串)

列表索引得到的结果还是列表(该元素是什么数据类型,索引得到的结果就是什么类型,不会发生变化)

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]print(my_list[-1][1]) # python

4.列表的切片,还是个列表

二、创建列表

空列表:

number =[]

print(number) #[]

普通列表:

number = [1,2,3]

print(number)

混合列表:

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

print(list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

列表的常用操作:增删改

三、向列表中添加元素

1.append() :默认在列表末尾追加新的对象。即将参数作为一个元素增加到列表尾部。(append的参数只有一个,append一次只能添加一个元素)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

list_1.append(9)

print(list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 9]

❤append()是对列表有作用,但是其返回的结果是None,这个是python的规则。(remove() 同理.)

my_list = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

new_elem= my_list.append(888)print(my_list)print(new_elem)

结果:

[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 888]

None

2.extend():在列表的尾部一次性追加另一个序列中的多个值,即将参数作为一个列表去扩展原列表,列表的合并。

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

list_1.extend([2,4,5,6])

print(list_1) #[1, 2, 3, (2, 'polo'), 6, 'hello', ['lemon', 'python'], 2, 4, 5, 6]

3.insert():有2个参数,第一个参数是待插入的位置,第二个参数是在该待插入位置,插入的值。

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

list_1.insert(3,'hello world')

print(list_1) #[1, 2, 3, 'hello world', (2, 'polo'), 6, 'hello', ['lemon', 'python']]

四、删除列表中的元素

1.删除指定的内容(不知道索引的位置,只知道列表中有这个具体的内容,一定是 有 才可以删除,列表中不存在,是无法操作的)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

list_1.remove(['lemon','python'])print(list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello']

remove()只能移除一个元素,列表中有多个相同元素,默认是移除第一个

list_1 = [1,'hello',2,'hello',3,(2,'polo'),6,'hello',['lemon','python']]

list_1.remove('hello')

print(list_1) #[1, 2, 'hello', 3, (2, 'polo'), 6, 'hello', ['lemon', 'python']]

2.删除指定的索引位置pop(),返回的结果是删除的元素值

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]#pop()不带索引值,默认移除列表中最后一个元素

list_1.pop()print(list_1) # [1, 2, 3, (2, 'polo'), 6, 'hello']

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]#pop()中带索引值,删掉对应索引值的内容

list_1.pop(3)print(list_1)# [1, 2, 3, 6, 'hello', ['lemon', 'python']]

❤查看pop()的结果(返回值)

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

elem=list_1.pop()print(elem)print(list_1)

结果:

['lemon', 'python']

[1, 2, 3, (2, 'polo'), 6, 'hello']

五、修改列表中的元素

list_1 = [1,2,3,(2,'polo'),6,'hello',['lemon','python']]

list_1[2] = 'hahhahahha'

print(list_1) #[1, 2, 'hahhahahha', (2, 'polo'), 6, 'hello', ['lemon', 'python']]

六、列表的函数方法

1.clear() 清楚列表元素

❤a = my_list.clear()的返回值是None

my_list = [2,1,3]

my_list.clear()

print(my_list) # []

print(my_list.clear()) #None

2.sort() 排序 数字排序,文字不需要排序

my_list = [2,1,3,4,6,5]

my_list.sort()print(my_list) #[1,2,3,4,5,6]

print(my_list.sort()) #None

my_list = [1,2,3,['3',6]]

my_list.sort()#报错,不同类型是不能比较的

3.reverse()降序

my_list = [2,1,3,4,6,5]

my_list.sort(reverse=True)print(my_list) #[6,5,4,3,2,1]

print(my_list.sort(reverse = True)) #None

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值