前言:继续学习python,接下来学习操作列表等知识
第四章、操作列表
messages=['a','b','c','d']
for message in messages:
print(message)
#输出结果:
a
b
c
d
格式:for xxx in xxxx :
利用for循环,遍历整个列表。使用单数和复数式名称,可帮助判断代码段处理的是单个列表元素还是整个列表
也可以在for循环中执行更多的操作,例如:
messages=['a','b','c','d']
for message in messages:
print(message.title() + "we you he")
#输出结果:
Awe you he
Bwe you he
Cwe you he
Dwe you he
在python中,for循环没有花括号,区分是否为for循环的语句就是通过
缩进
来区分。在for循环下没有缩进的只执行一次。
messages=['a','b','c','d']
for message in messages:
print(message.title()+"we")
print("you"+message.title()+".\n")
print("he!")
#输出结果:
Awe
youA.
Bwe
youB.
Cwe
youC.
Dwe
youD.
he!
创建数值列表
使用函数range()能够轻松地生成一系列的数字
for value in range(1,5):
print(value)
#输出结果:
1
2
3
4
函数range()从你指定的第一个值开始数,并在到达你指定的第二个值 后停止,因此输出不包含第二个值
使用
range()
函数和list()
函数创建数字列表,将range() 作为list() 的参数,输出将为一个数字列表。
numbers=list(range(1,6))
print(numbers)
#输出结果:
[1, 2, 3, 4, 5]
使用 range()函数还可以指定步长
#求1-10的偶数
numbers=list(range(2,11,2))
print(numbers)
#输出结果:
[2, 4, 6, 8, 10]
实现创建一个列表,其中包含前10个整数(即1~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]
#为使代码更为简介,还可以不使用临时变量
squares = []
for value in range(1,11):
squares.append(value**2)
print(squares)
对数字列表执行简单的统计计算
对数字列表执行简单的统计计算,
min(),max(),sum()
numbers = [1,2,3,4,5,6,7,8,9]
print(min(numbers))
print(max(numbers))
print(sum(numbers))
#输出结果:
1
9
45
列表解析
列表解析 将for 循环和创建新元素的代码合并成一行,并自动 附加新元素。
squares = [value**2 for value in range(1,11)]
print(squares)
#输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
使用列表的一部分
切片(可以处理列表的部分元素)
创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range() 一样。
players = ['a','d','c','d']
print(players[0:3])
#输出结果:
['a', 'd', 'c']
没有指定第一个索引,将从列表开头开始
players = ['a','d','c','d']
print(players[:4])
#输出结果:
['a', 'd', 'c', 'd']
#要让切片终止于列表末尾,也可使用类似的语法。
例如:
players = ['a','d','c','d']
print(players[2:])
#输出结果:
['c', 'd']
#负数索引返回离列表末尾相应距离的元素
players = ['a','d','c','d']
print(players[-3:])
#输出结果:
['d', 'c', 'd']
遍历切片
players = ['a','d','c','d']
for player in players[:3]:
print(player.title())
#输出结果:
A
D
C
复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引
([:] )
my = ['a','b','c','d']
you = my[:]
print(my)
print("\n")
print(you)
#输出结果:
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
元组
定义元组后,可以使用索引来访问其元素,就像访问列表元素一样。而且是用
圆括号
括起来的
修改元组的操作是被禁止的,不能给元组的元素赋值.
例如:
my=(200,50)
my[0]=210
print(my[0])
#输出结果:
TypeError: 'tuple' object does not support item assignment
遍历元组中的所有值
与列表一样,也可以使用for 循环来遍历元组中的所有值
mys=(200,50)
for my in mys:
print(my)
#输出结果:
200
50
修改元组变量
不能修改元组的元素,但可以给存储元组的变量赋值
mys = (200,50)
for my in mys:
print(my)
print("\n")
mys = (210,60)
for my in mys:
print(my)
#输出结果:
200
50
210
60
如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组.
第五章、if语句
mys = ['a','b','c','d']
for my in mys:
if my == 'a':
print(my.title())
else:
print(my)
#输出结果:
A
b
c
d
条件测试
每条if 语句的核心都是一个值为True 或False 的表达式,这种表达式被称为条件测试 条件测试 。
==
相等运算符!=
判断两个值是否不等
检查特定值是否包含在列表中
判断特定的值是否已包含在列表中,可使用
关键字in
>>> mys = ['a','b','c']
>>> 'a' in mys
True
检查特定值是否不包含在列表中
使用关键字not in
mys = ['a','b','d','f']
you = 'l'
if you not in mys:
print("no")
#输出结果:
no
if-elif-else结构
age = 12
if age < 4:
print("a")
elif age < 18:
print("b")
else:
print("c")
#输出结果:
b
使用多个elif结构
age=12
if age<4:
price = 0
elif age <18:
price = 5
elif age <40:
price = 10
else:
price = 5
print(price)
#输出结果:
5
确定列表不是空的
messages=[]
if messages:
for message in messages:
print(message + ".")
print("yes")
else:
print("no")
#输出结果:
no
使用多个列表
people_message = ['a','b','c','d']
messages = ['a','b']
for message in messages:
if message in people_message:
print("yes")
else:
print("no")
print("over")
#输出结果:
yes
yes
over
这次就先学习到这里,下次继续往后学习。