python基础课程系列(四)

5.3. if 语句
5.3.1.简单的 if  语句
最简单的 if 语句只有一个测试和一个操作:
if conditional_test:
    do something

在第 1 行中,可包含任何条件测试,而在紧跟在测试后面的缩进代码块中,可执行任何操作。如果条件测试的结果为 True , Python 就会执行紧跟在 if 语句后面的代码;否则
Python 将忽略这些代码。

5.3.2. if-else  语句
经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作;在这种情况下,可使用 Python 提供的 if-else 语句。 if-else 语句块类似于简单的 if 语句,但
其中的 else 语句让你能够指定条件测试未通过时要执行的操作。
>>>age = 17
>>>if age >= 18:
        print('You are old enough to vote!')
        print('Have you registered to vote yet?')
    else:
        print('Sorry, you are too young to vote.')
        print('Please register to vote as soon as you turn 18!')

Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

5.3.3.if-elif-else  结构
经常需要检查超过两个的情形,为此可使用 Python 提供的 if-elif-else 结构。 Python 只执行 if-elif-else 结构中的一个代码块,它依次检查每个条件测试,直到遇到通过
了的条件测试。测试通过后, Python 将执行紧跟在它后面的代码,并跳过余下的测试。
--------------------------------------------------
age = 12

if age < 4:
    print('Your admission cost is $0.')
elif age < 18:
    print('Your admission cost is $5.')
else:
    print('Your admission cost is $10.')
--------------------------------------------------
Your admission cost is $5.


5.3.4.使用多个 elif  代码块

age = 55

if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else:
    price = 5

print('Your admission cost is $' + str(price) + '.')
--------------------------------------------------
Your admission cost is $10.

5.3.6.测试多个条件
有时候必须检查你关心的所有条件。在这种情况下,应使用一系列不包含 elif 和 else 代码块的简单 if 语句。在可能有多个条件为 True ,且你需要在每个条件为 True
时都采取相应措施时,适合使用这种方法。

requested_toppings = ['mushrooms', 'extra cheese']

if 'mushrooms' in requested_toppings:
    print('Adding mushrooms.')
if 'pepperoni' in requested_toppings:
    print('Adding prpperoni.')
if 'extra cheese' in requested_toppings:
    print('Adding extra cheese.')

print('\nFinished making your pizza!')

-----------------------------------------------------
Adding mushrooms.
Adding extra cheese.

Finished making your pizza!

5.4.使用 if 语句处理列表
如果比萨店的青椒用完了,该如何处理呢?为妥善地处理这种情况,可在 for 循环中包含一条 if 语句:

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping == 'green peppers':
        print('Sorry, we are out of green peppers right now.')
    else:
        print('Adding ' + requested_topping + '.')

print('\nFinished making your pizza!')
------------------------------------------------------
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.

Finished making your pizza!

5.4.2.确定列表不是空的
在这里,我们首先创建了一个空列表,其中不包含任何配料。进行了简单检查,而不是直接执行 for 循环。在 if 语句中将列表名用在条件表达式中时, Python 将在列表至少包含一个元素时返回 True ,并在列表为空时返回 False 。如果 requested_toppings 不为空,就运行与前一个示例相同的 for 循环;否则,就打印一条消息,询问顾客是否确实要点不加任何配料的普通比萨

requested_toppings = []

if requested_toppings:
    for requested_topping in requested_toppings:
        print('Adding ' + requested_topping + '.')
    print('\nFinished making your pizza!')
else:
    print('Are you sure you want a plain pizza?')
------------------------------------------------------
Are you sure you want a plain pizza?

5.4.3.使用多个列表
我们定义了一个列表,其中包含比萨店供应的配料。请注意,如果比萨店供应的配料是固定的,也可使用一个元组来存储它们。又创建了一个列表,其中包含顾客点的配料,请注意那个不同寻常的配料 —— 'french fries' 。我们遍历顾客点的配料列表。在这个循环中,对于顾客点的每种配料,我们都检查它是否包含在供应的配料列表中;如果答案是肯定的,就将其加入到比萨中,否则将运行 else 代码块:打印一条消息,告诉顾客不供应这种配料。
available_toppings = ['mushrooms', 'olives', 'green peppers','pepproni', 'pineapple', 'extra cheese']

requested_toppings = ['mushrooms', 'french fries', 'extra cheese']

for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print('Adding ' + requested_topping + '.')
    else:
        print('Sorry, we don\'t have ' + requested_topping + '.')

print('\nFinished making your pizza!')
------------------------------------------------------
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.

Finished making your pizza!

第6章 字典
6.1.一个简单的字典
>>> alien_0 = {'color':'green', 'points':5}
>>> print(alien_0['color'])
green
>>> print(alien_0['points'])
5

6.2.使用字典
字典 是一系列 键 — 值对 。每个 键 都与一个值相关联,你可以使用键来访问与之相关联的值。与键相关联的值可以是数字、字符串、列表乃至字典。
6.2.1.访问字典中的值
>>> new_point = alien_0['points']
>>> print('You just earned ' + str(new_point) + ' points!')
You just earned 5 points!

6.2.2.添加键 — 值对
字典是一种动态结构,可随时在其中添加键 — 值对。要添加键 — 值对,可依次指定字典名、用方括号括起的键和相关联的值。
>>> print(alien_0)
{'color': 'green', 'points': 5}
>>> alien_0['x_position'] = 0
>>> alien_0['y_position'] = 25
>>> print(alien_0)
{'x_position': 0, 'color': 'green', 'y_position': 25, 'points': 5}

6.2.4.修改字典中的值
要修改字典中的值,可依次指定字典名、用方括号括起的键以及与该键相关联的新值。
>>> alien_1 = {'color':'green'}
>>> print(alien_1)
{'color': 'green'}
>>> alien_1['color'] = 'yellow'
>>> print(alien_1)
{'color': 'yellow'}
-------------------------------------------------------
alien_0 = {'x_position':0, 'y_position':25, 'speed':'medium'}
print('Original x-position: ' + str(alien_0['x_position']))

#向右移动外星人
#根据外星人当前速度决定将其移动多远

if alien_0['speed'] == 'slow':
    x_increment = 1
elif alien_0['speed'] == 'medium':
    x_increment = 2
else:
    x_increment = 3

#新位置等于老位置加上增量
alien_0['x_position'] = alien_0['x_position'] + x_increment

print('New x-position: ' + str(alien_0['x_position']))

-------------------------------------------------------
Original x-position: 0
New x-position: 2

6.2.5.删除键 — 值对
对于字典中不再需要的信息,可使用 del 语句将相应的键 — 值对彻底删除。使用 del 语句时,必须指定字典名和要删除的键。
>>> print(alien_0)
{'color': 'green', 'points': 5}
>>> del alien_0['points']
>>> print(alien_0)
{'color': 'green'}
 

猜你喜欢

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