Python语法基础刻意练习:Task03(列表与元组)day2

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

操作列表

前一天主要介绍了关于列表的基础知识,包括简单处理列表的方法、函数等。今天继续来说一下关于操作列表的相关知识。
遍历整个列表
在某些情况下,我们可能需要遍历整个列表来完成一些操作,要完成此操作,可以使用for循环,我们来看一个例子:

bicycles=['trek','cannondale','redline']      
for bicycle in bicycles: 
	print(bicycle)        
#trek
#cannondale
#redline

在这段代码中,我们定义了一个for循环,这个循环每次从列表中取出一个元素,并将其存储在变量bicycle中,最后打印出来。

bicycles=['trek','cannondale','redline']      
for bicycle in bicycles:
	print(bicycle+' is very good.')        
#trek is very good.
#cannondale is very good.
#redline is very good.

避免缩进错误
Python判断代码行与前一个代码行的关系,在定义循环之后缩进的代码是需要循环执行的。有一些常见的缩进错误,值得我们注意:忘记缩进、不必要的缩进,另外定义for语句之后不要遗漏了冒号

创建数值列表
首先来介绍一下range()函数,它可以轻松的生成一系列的数字,如:

for value in range(1,5):
	print(value)
#1
#2
#3
#4

上面的代码看似会打印1~5,但实际只打印到了4。要打印1,2,3,4,5,可以使用range(1,6)。
下面使用range()创建数字列表:

numbers=list(range(1,6))
print(numbers)     #[1,2,3,4,5]

使用函数range()时,还要指定步长。例如,打印1~10内的偶数:

even_numbers=list(range(2,11,2))
print(even_numbers)     #[2,4,6,8,10]

创建一个列表,包含前十个整数的平方:

squares=[]
for value in range(1,11):
	square=value**2
	squares.append(square)
print(squares)   #[1,4,9,16,25,36,49,64,81,100]

对数字列表执行简单的统计运算

digits=[1,2,3,4,5,6,7,8,9,0]
print(min(digits))           #0  最小值
print(max(digits))           #9  最大值
print(sum(digits))           #45 总和

列表解析
前面介绍的生成列表的方式包含三四行代码,而列表解析让你只需编写一行代码就能生成这样的列表。列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素。

squares=[value**2 for value in range(1,11)]
print(squares)          #[1,4,9,16,25,36,49,64,81,100]

使用列表的一部分
切片:

players=['charles','martina','michael','florence','eli']
print(players[0:3])   #['charles','martina','michael']
players=['charles','martina','michael','florence','eli']
print(players[1:4])   #['martina','michael','florence']
players=['charles','martina','michael','florence','eli']
print(players[:4])   #['charles','martina','michael','florence'] 没有指定起始索引的从头开始
players=['charles','martina','michael','florence','eli']
print(players[1:])   #['martina','michael','florence','eli'] 没有指定终止索引的终止于末尾

遍历切片

players=['charles','martina','michael','florence','eli']
print('here are the first three players on my team:')
for player in players[:3]:
	print(player.title())
#here are the first three players on my team:
#Charles
#Martina
#Michael

复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引:

my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
print(my_foods)                        #['pizza','falafel','carrot cake']
print(friend_foods)                    #['pizza','falafel','carrot cake']

元组

定义元组
元组看起来很像列表,但它使用圆括号而不是方括号来标识。定义元组后,可以使用索引来访问元素:

dimensions=(200,50) 
print(dimensions[0])     #200
print(dimensions[1])     #50

下面我们来尝试修改第一个元素:

dimensions=(200,50) 
dimensions[0]=250

Traceback (most recent call last):
  File "质数.py", line 2, in <module>
    dimensions[0]=250
TypeError: 'tuple' object does not support item assignment


------------------
(program exited with code: 1)

请按任意键继续. . .

结果会报错,这说明元组中的元素是不能被修改的。
虽然不能修改元素的值,但可以给存储元组的变量赋值:

dimensions=(200,50) 
print(dimensions)             #(200,50)
dimensions=(400,100) 
print(dimensions)             #(400,100)

关于列表的知识点较多,花了两天时间复习巩固。

猜你喜欢

转载自blog.csdn.net/weixin_45138411/article/details/102700917