1.使用for循环遍历整个列表
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i)
alice
david
carolina
===========================
#在for循环中执行更多的操作
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i.title() + ", that was a great trick!")
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
=====================
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i.title() + ", that was a great trick!")
print("I can't wait to see you next trick, " + i.title() + ".\n")
Alice, that was a great trick!
I can't wait to see you next trick, Alice.
David, that was a great trick!
I can't wait to see you next trick, David.
Carolina, that was a great trick!
I can't wait to see you next trick, Carolina.
2.避免缩进错误
#for循环结束后执行一些操作,在for循环后面,没有缩进的代码都只执行一次,,可以看到,第三条print语句没有缩进,只执行了一次
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i.title() + ", that was a great trick!")#print要注意缩进
print("I can't wait to see you next trick, " + i.title() + ".\n")#print要注意缩进
print("Thank you,everyone, That was a great magic show !")
Alice, that was a great trick!
I can't wait to see you next trick, Alice.
David, that was a great trick!
I can't wait to see you next trick, David.
Carolina, that was a great trick!
I can't wait to see you next trick, Carolina.
Thank you,everyone, That was a great magic show !
===================================
#当出现不必要的缩进时
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i.title() + ", that was a great trick!")#print要注意缩进
print("I can't wait to see you next trick, " + i.title() + ".\n")#print要注意缩进
print("Thank you,everyone, That was a great magic show !")
Alice, that was a great trick!
I can't wait to see you next trick, Alice.
Thank you,everyone, That was a great magic show !
David, that was a great trick!
I can't wait to see you next trick, David.
Thank you,everyone, That was a great magic show !
Carolina, that was a great trick!
I can't wait to see you next trick, Carolina.
Thank you,everyone, That was a great magic show !
========================================
#当忘记缩进时,出现错误
magicians = ['alice', 'david', 'carolina']
for i in magicians:
print(i)
File "<ipython-input-10-0849b56cda33>", line 3
print(i)
^
IndentationError: expected an indented block
========================
#遗漏冒号,for循环末尾有个冒号,如果不小心遗漏,会导致语法错误
magicians = ['alice', 'david', 'carolina']
for i in magicians
print(i)
File "<ipython-input-11-6876d8e7a14b>", line 2
for i in magicians
^
SyntaxError: invalid syntax
3.创建数值列表
#使用函数range(),,从第一个值开始,到不包括最后一个值结束
for i in range(1,5):
print(i)
1
2
3
4
================================
#使用函数range()创建数字列表
numbers = list(range(1,5))
print(numbers)
[1, 2, 3, 4]
===========================
#使用函数range()创建数字列表,指定步长
numbers = list(range(1,11,2))
print(numbers)
[1, 3, 5, 7, 9]
===============================
#练习,创建一个列表,其中包含前十个整数的平方
list = []#创建一个空列表
for i in range(1,11):
square = i**2
list.append(square)
print(list)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#更简洁做法
list = []#创建一个空列表
for i in range(1,11):
list.append(i**2)
print(list)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
=======================================
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
min(numbers)#最小值
0
max(numbers)#最大值
9
sum(numbers)#求和
45
=========================================
#列表解析,前面介绍的生成列表squares的方式有三四行代码,而列表解析只需便携一行代码就能生成这样的列表
squares = [i**2 for i in range(1,11)]
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4.使用列表的一部分,切片
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(numbers[0:3])#提取1到3个数字
print(numbers[1:4])#提取2到4个数字
print(numbers[:4])#若没有指定第一个索引,将自动从列表开头开始
print(numbers[2:])#让列表终于末尾
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]
[3, 4, 5, 6, 7, 8, 9, 0]
=======================
遍历切片
假如值遍历前三个数字
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print('numbers:')
for i in numbers[:3]:
print(i)
numbers:
1
2
3
===================================
#复制列表
my_foods = ['pizza', 'falafel', 'carrot cake']
friends_foods = my_foods[:]#相当于创建一个副本
print('my favorite foods are :')
print(my_foods)
print("my friend's favorite foods are :")
print(friends_foods)
my favorite foods are :
['pizza', 'falafel', 'carrot cake']
my friend's favorite foods are :
['pizza', 'falafel', 'carrot cake']
5.元组:将不可修改的列表称为元组
元组使用圆括号而不是方括号来标识,定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样
#定义一个元组
dimentions = (200,50)
print(dimentions[0])
print(dimentions[1])
200
50
============================
#尝试修改元组中的元素,看看效果
dimentions = (200,50)
dimentions[0] = 250
TypeError Traceback (most recent call last)
<ipython-input-30-8fb52796d9e5> in <module>
1 #尝试修改元组中的元素,看看效果
2 dimentions = (200,50)
----> 3 dimentions[0] = 250
TypeError: 'tuple' object does not support item assignment
====================================
#遍历元组中所有的值
dimentions = (200,50)
for i in dimentions:
print(i)
200
50
=========================================
#修改元组变量,虽然不能修改元组的元素,但可以给存贮元组的变量赋值,达到重新定义整个元组的目的
dimentions = (200,50)
print('original dimentions:')
for i in dimentions:
print(i)
dimentions = (400,200)
print('modified dimentions:')
for i in dimentions:
print(i)
original dimentions:
200
50
modified dimentions:
400
200
网友评论