python基础课程系列(三)

4.4.使用列表的一部分
你还可以处理列表的部分元素 ——Python 称之为 切片
>>> players = ['charles', 'martina', 'michael', 'florence', 'eli']
>>> print(players)
['charles', 'martina', 'michael', 'florence', 'eli']
>>> print(players[0:3])
['charles', 'martina', 'michael']
>>> print(players[1:4])
['martina', 'michael', 'florence']

如果你要输出名单上的最后三名队员,可使用切片 players[-3:] :
>>> print(players)
['charles', 'martina', 'michael', 'florence', 'eli']
>>> print(players[-3:])
['michael', 'florence', 'eli']

4.4.2.遍历切片
如果要遍历列表的部分元素,可在 for 循环中使用切片。
>>> print(players)
['charles', 'martina', 'michael', 'florence', 'eli']
>>> for player in players[:3]:
    print(player.title())
    
Charles
Martina
Michael

4.4.3.复制列表
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引( [:] )。这让 Python 创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个
列表。
>>> my_foods = ['pizza', 'falafel', 'carrot cake']
>>> friend_foods = my_foods[:]
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake']
>>> my_foods.append('cannoli')
>>> friend_foods.append('ice cream')
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake', 'cannoli']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake', 'ice cream']

4.5.元组
列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网站的用户列表或游戏中的角色列表至关重要。然而,有时候你需要创建一系列不可修
改的元素,元组可以满足这种需求。 Python 将不能修改的值称为 不可变的 ,而不可变的列表被称为 元组 。

4.5.1.定义元组
元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。
>>> dimensions = (200, 50)
>>> print(dimensions[0])
200
>>> print(dimensions[1])
50
>>> dimensions[0] = 250
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    dimensions[0] = 250
TypeError: 'tuple' object does not support item assignment

4.5.2.遍历元组中的所有值
像列表一样,也可以使用 for 循环来遍历元组中的所有值:
>>> for dimension in dimensions:
    print(dimension)
    
200
50

4.5.3.修改元组变量
虽然不能修改元组的元素,但可以给存储元组的变量赋值。
>>> print(dimensions)
(200, 50)
>>> dimensions = (400, 100)
>>> print(dimensions)
(400, 100)

第5章 if语句
5.1.一个简单示例
>>> cars = ['audi', 'bmw', 'subaru', 'toyota']
>>> for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

Audi
BMW
Subaru
Toyota

5.2.条件测试
每条 if 语句的核心都是一个值为 True 或 False 的表达式,这种表达式被称为 条件测试 。 Python 根据条件测试的值为 True 还是 False 来决定是否执行 if 语句中的代码。如果
条件测试的值为 True , Python 就执行紧跟在 if 语句后面的代码;如果为 False , Python 就忽略这些代码。

5.2.5  检查多个条件
你可能想同时检查多个条件,例如,有时候你需要在两个条件都为 True 时才执行相应的操作,而有时候你只要求一个条件为 True 时就执行相应的操作。在这些情况下,关键
字 and 和 or 可助你一臂之力。
1)使用 and 检查多个条件
要检查是否两个条件都为 True ,可使用关键字 and 将两个条件测试合而为一;如果每个测试都通过了,整个表达式就为 True ;如果至少有一个测试没有通过,整个表达式就
为 False 。
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 and age_1 >= 21
False
>>> age_1 = 22
>>> age_0 >= 21 and age_1 >=21
True

2)使用 or 检查多个条件
关键字 or 也能够让你检查多个条件,但只要至少有一个条件满足,就能通过整个测试。仅当两个测试都没有通过时,使用 or 的表达式才为 False 。
>>> age_0 = 22
>>> age_1 = 18
>>> age_0 >= 21 or age_1 >= 21
True
>>> age_0 = 18
>>> age_0 >= 21 or age_1 >= 21
False

5.2.6.检查特定值是否包含在列表中
要判断特定的值是否已包含在列表中,可使用关键字 in 
>>> requested_toppings = ['mushrooms', 'onions', 'pineapple']
>>> 'mushrooms' in requested_toppings
True
>>> 'peperoni' in requested_toppings
False

5.2.7.检查特定值是否不包含在列表中
还有些时候,确定特定的值未包含在列表中很重要;在这种情况下,可使用关键字 not in
>>> banned_users = ['adnrew', 'carolina', 'david']
>>> user = 'marie'
>>> if user not in banned_users:
    print(user.title() + ', you can post a response if you wish.')
    
Marie, you can post a response if you wish.
 

猜你喜欢

转载自blog.csdn.net/zhaocen_1230/article/details/81279096