Python学习第三天!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45652666/article/details/102711986

掌握 列表与元组

列表

列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现。
列表的数据项不需要具有相同的类型
创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。
用索引来访问list中每一个位置的元素,记得索引是从0开始的
使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下:

list1 = ['喜羊羊', '灰太狼', 2019, 2020];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
 
print ("list1[0]: ", list1[0])
print ("list2[1:5]: ", list2[1:5])

结果如下:
list1[0]: 喜羊羊
list2[1:5]: [2, 3, 4, 5]

append()

append() 方法用于在列表末尾添加新的对象。
如下:

list1 = ['喜羊羊', '灰太狼', 2019, 2020];
list1.append('懒羊羊')
print ("更新后的列表 : ", list1)

结果:
更新后的列表 : [‘喜羊羊’, ‘灰太狼’, 2019, 2020, ‘懒羊羊’]

extend()

extend() 函数用于在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。

list1 = ['喜羊羊', '灰太狼', 2019, 2020];
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2)  # 扩展列表
print ("扩展后的列表:", list1)

运行结果如下:
扩展后的列表: [‘喜羊羊’, ‘灰太狼’, 2019, 2020, 0, 1, 2, 3, 4]

insert()

insert() 函数用于将指定对象插入列表的指定位置。

list1 = ['喜羊羊', '灰太狼', 2019, 2020];
list1.insert(1, '熊二')
print ('列表插入元素后为 : ', list1)

结果如下:
列表插入元素后为 : [‘喜羊羊’, ‘熊二’, ‘灰太狼’, 2019, 2020]

pop()

pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。

list1 = ['喜羊羊', '熊二', '灰太狼']
list1.pop()
print ("列表现在为 : ", list1)
list1.pop(1)
print ("列表现在为 : ", list1)

结果如下:
列表现在为 : [‘喜羊羊’, ‘熊二’]
列表现在为 : [‘喜羊羊’]

remove()

remove() 函数用于移除列表中某个值的第一个匹配项。

list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.remove('Taobao')
print ("列表现在为 : ", list1)
list1.remove('Baidu')
print ("列表现在为 : ", list1)

如下:
列表现在为 : [‘Google’, ‘Runoob’, ‘Baidu’]
列表现在为 : [‘Google’, ‘Runoob’]
还有以下几种函数

list.reverse()

反向列表中元素

list1 = ['喜羊羊', '熊二', '灰太狼']
list1.reverse()
print ("列表反转后: ", list1)

结果如下:
列表反转后: [‘灰太狼’, ‘熊二’, ‘喜羊羊’]

list.clear()

清空列表

list1 = ['喜羊羊', '熊二', '灰太狼']
list1.clear()
print ("列表清空后 : ", list1)

结果如下:
列表清空后 : []

list.copy()

复制列表

list1 = ['喜羊羊', '熊二', '灰太狼']
list2 = list1.copy()
print ("list2 列表: ", list2)

结果如下:
list2 列表: [‘喜羊羊’, ‘熊二’, ‘灰太狼’]

元组

Python 的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。


tup1 = ('Google', 'Runoob', 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";  
type(tup3)

创建空元组

tup1 = ();

元组可以使用下标索引来访问元组中的值,如下:

tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
 
print ("tup1[0]: ", tup1[0])
print ("tup2[1:5]: ", tup2[1:5])

如下:
tup1[0]: Google
tup2[1:5]: (2, 3, 4, 5)

修改元组

元组中的元素值是不允许修改的,但我们可以对元组进行连接组合,如下:

tup1 = (12, 34.56);
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2;
print (tup3)

如下:
(12, 34.56, ‘abc’, ‘xyz’)

删除元组

元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组,如下:

tup = ('Google', 'Runoob', 1997, 2000)
 
print (tup)
del tup;
print ("删除后的元组 tup : ")
print (tup)

结果:
以上实例元组被删除后,输出变量会有异常信息,输出如下所示:
删除后的元组 tup :
Traceback (most recent call last):
File “test.py”, line 8, in
print (tup)
NameError: name ‘tup’ is not defined

len(tuple)

计算元组元素个数。

tuple1 = ('Google', 'Runoob', 'Taobao')
len(tuple1)

结果如下:
3

max(tuple)

返回元组中元素最大值。

tuple2 = ('5', '4', '8')
max(tuple2)

结果如下:‘8’

min(tuple)

返回元组中元素最小值。

tuple2 = ('5', '4', '8')
min(tuple2)

结果如下:‘4’

tuple(seq)

将列表转换为元组。

list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
tuple1=tuple(list1)
tuple1

结果如下
(‘Google’, ‘Taobao’, ‘Runoob’, ‘Baidu’)
今天的学习就到这里了,一起加油!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45652666/article/details/102711986
今日推荐