# 1.一个例子
#下面的代码遍历一个列表,并以首字母大写的方式打印其中的汽车名,
# 但对于汽车名'bmw',以全大写的方式打印
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
# 2.条件测试
# 2.1检查条件是否相等 ==
car = 'bmw'
print(car == 'bmw')
print(car == 'audi')
# 2.2检查是否相等时不考虑大小写
car = 'Audi'
print(car.lower() == 'audi')
print('car=', car) # lower不会修改存储在变量car中的值
# 2.3检查是否不相等,有时更高效 !=
char = 'moon'
if char != 'sun':
print('mon is not equal with sun.')
# 2.4比较数字
age = 18
print(age == 18)
print(age != 10)
# 2.5检查多个条件
# 2.5.1使用and检查多个条件
age_0 = 22
age_1 = 18
result = age_0 >= 21 and age_1 >= 21 # 有无(),结果都一样
print(result)
#2.5.2使用or检查多个条件
result = age_0 >= 21 or age_1 >= 21
print(result)
# 2.6 检查特定值是否包含在列表中 in
pizzas = ['A', 'B', 'C']
print('A' in pizzas)
print('F' in pizzas)
# 2.7检查特定的值是否不包含在列表中 not in
print('F' not in pizzas)
print('A' not in pizzas)
# 2.8布尔表达式
game_active = True
print(game_active)
# P70动手试一试
# 5-1
car = 'Telsa'
print("Is car == 'Telsa'? I predict True.")
print(car == 'Telsa')
print('a' == 'a')
print('a' != 'a')
print('BMW'.lower() == 'bmw')
print(3 == 3)
print(3 != 4)
print(3 > 4)
print(3 < 4)
print(3 >= 4)
print(3 <= 4)
print( 3 == 4 and 4 < 3)
print(3 == 4 or 3 < 4)
print(3 in [1, 2, 3])
print(3 not in [1, 2])
# 3.if语句
# 3.1.简单的if语句
# if conditional_test:
# do something
# 3.2.if-else语句
age = 17
if age >= 18:
print('You are old enough to vote!')
else:
print('Sorry, you are too young to vote.')
# 3.3.if-elif-else语句
age = 12
if age < 4:
print('You are a baby.')
elif age < 18:
print('You are a teen')
else:
print('You are a adult')
# 3.4.使用多个elif代码块
age = 12
if age < 4:
print('You are a baby.')
elif age < 10:
print('You are a child.')
elif age < 18:
print('You are a teen')
else:
print('You are a adult')
# 3.5.省略else代码块,else有用才加,没用可以省略
if age < 4:
print('You are a baby.')
elif age < 10:
print('You are a child.')
elif age < 18:
print('You are a teen')
# 3.6.测试多个条件,使用多个不包含else的if,不管前面的if有没有通过,后面的都要测试
# P75试一试
# 5-3
alien_color = 'green'
if alien_color == 'green':
print('Player gets 5 points.')
if alien_color == 'red':
print('Player gets 5 points.')
# 5-4
alien_color = 'green'
if alien_color == 'green':
print('Player gets 5 points.')
else:
print('Player gets 10 points.')
alien_color = 'red'
if alien_color == 'green':
print('Player gets 5 points.')
else:
print('Player gets 10 points.')
# 5-5
if alien_color == 'green':
print('Player gets 5 points.')
elif alien_color == 'yellow':
print('Player gets 10 points.')
elif alien_color == 'red':
print('Player gets 15 points.')
# 5-6
age = 30
if age < 2:
print('他是婴儿')
elif age < 4:
print('他蹒跚学步')
elif age < 13:
print('他是儿童')
elif age < 20:
print('他是青少年')
elif age < 65:
print('他是成年人')
else:
print('他是老人')
# 5-7
favorites_fruits = ['apple', 'banana', 'orange']
if 'apple' in favorites_fruits:
print('You really like apple.')
if 'orange' in favorites_fruits:
print('You really like orange')
if 'pear' in favorites_fruits:
print('You really like pear')
# 4.使用if处理列表
# 4.1检查特殊元素
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 + '.')
# 4.2确定列表不是空的
requested_toppings = []
if requested_toppings:
print('list is not empty.')
else:
print('list is empty.')
# 4.3使用多个列表
available_toppings = ['mushrooms', 'olives','pepperoni', '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 + ".")
# P79动手试一试
# 5-8
users = ['Mary', 'Jack', 'Howard', 'Dean', 'admin']
for user in users:
if user == 'admin':
print('Hello admin, would you like to see a staus report?')
else:
print('Hello %s, thank you for logging in again.' % user)
# 5-9
users.pop()
users.pop()
users.pop()
users.pop()
users.pop()
if not users:
print('We need to find some user!')
# 5-10
current_users = ['Mary', 'Jack', 'Howard', 'Dean', 'admin']
new_users = ['Mary', 'Bob', 'Jack','Lecuun', 'Musk']
for new_user in new_users:
if new_user in current_users:
print('%s has been used, please input another.' % new_user)
else:
print('%s has not been used' % new_user)
# 5-11
numbers = list(range(1, 10))
for number in numbers:
if number == 1:
print('first')
elif number == 2:
print('second')
elif number == 3:
print('third')
else:
print(str(number) +'th')