5-1条件测试
cars=['bmw','audi','toyota']
car='bmw'
#True
print(car == 'bmw')
#False
print(car != 'bmw')
#True
print(car == car)
#False
print(car == car.title())
#True
print(car == 'bmw' or car == 'audi')
#False
print(car == 'bmw' and car == 'audi')
#True
print(int(len(car)) == int(len('bmw')))
#False
print(int(len(car)) == int(len('audi')))
#True
print('bmw' in cars)
#False
print('audi' not in cars)
5-2更多的条件测试
name_1 = 'louie'
name_3 = 'Louie'
# 字符串比较
print(name_1 == name_3)
print(name_1 == name_3.lower())
num_1 = 5
num_2 = 9
# 数字比较
print(num_1 == num_2)
print(num_1 != num_2)
print(num_1 > num_2)
print(num_1 < num_2)
# 使用 and 和 or 来比较
if num_1 > 5 and num_2 > 5:
print("This is true")
else:
print("This is False")
if num_1 > 5 or num_2 > 5:
print("This is true")
else:
print("This is False")
names = ['louie', 'link', 'lisa', 'park']
name = 'links'
if name in names:
print(name + " in the list names")
else:
print(name + " not in the list names")
5-3外星人颜色1
aline_color='green'
if aline_color=='green':
print('you got 5 points.')
else:
print('no')
alien_color = 'red'
if alien_color == 'green':
print("You got 5 point!~")
else:
print("no")
5-4外星人颜色2
alien_color = "green"
if alien_color == "green":
print("You get 5 points.")
else:
print("You get 10 points.")
5-5外星人颜色3
alien_color='green'
if alien_color =='green':
print("You get 5 points.")
elif alien_color == 'red':
print("You get 10 points.")
else:
print("You get 15 points.")
5-6人生中的不同阶段
age=12
if age < 2:
print("You are a baby")
elif 2 <= age < 4:
print("He is making a toddler.")
elif 4 <= age < 13:
print("You are a child.")
elif 13 <= age < 20:
print("You are a young man.")
elif 20 <= age < 65:
print("You are an adult.")
else:
print("You are an old man.")
age可以更改
不过可以把第一句更改成输入判断,鉴于第五章还未学,第一种答案给的是随意赋值的
可以改为
age = input("输入年龄:")
5-7喜欢的水果
favorite_fruits = ['banana', 'apple', 'orange']
if "apple" in favorite_fruits:
print("You really like apple!")
if "orange" in favorite_fruits:
print("You really like orange!")
if "orange" in favorite_fruits:
print("You really like orange!")
if "milk" in favorite_fruits:
print("You really like milk!")
else:
print("The pears not list")
5-8以特殊方式跟管理员打招呼
name_list = ["bob", "jane", "jack", "admin", "tom"]
for i in name_list:
if i == "admin":
print("Hello "+i+", would you like to see a status report?" )
else:
print("Hello "+i+", thank you for logging in again" )
5-9处理没有用户的情形
name_list = []
if len(name_list) == 0:
print("We need to find some users!")
else:
for i in name_list:
if i == "admin":
print("Hello %s, would you like to see a status report?" % i)
else:
print("Hello %s, thank you for logging in again" % i)
5-10检查用户名
current_users = ["bob", "jane", "jack", "admin", "tom"]
new_users = ["Bob", "james", "NIck", "JACK", "name"]
for i in new_users:
if i.lower() in current_users:
print("%s 已经被使用,请更换用户名" % i)
else:
print("%s 没被注册,可以使用" % i)
5-11序数
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for num in nums:
if num == "1":
print("1st")
elif num == "2":
print("2nd")
elif num == "3":
print("3rd")
else:
prin
5-12设置if语句的格式
略
5-13自己的想法
略
学python机器学习欢迎关注公众号