读书笔记 | Python学习之旅 Day14

2018-03-29  本文已影响0人  公子老马

Python学习之旅 读书笔记系列

Day 14

《Python编程从入门到实践》

复盘:第一部分基础知识(第1章~11章)

今天继续动手做练习题,动手的感觉不一样,对知识掌握有更具体的体会。

练习题

第5章 if语句

if条件判断以及常用的条件测试

1.test_study.py

做一系列条件测试,明白其原理

car = "subaru"
print("Is car == 'subaru'?I predict True.")
print(car == "subaru")

#测试条件
print("\nIs car == 'audi'? I predict False.")
print(car == "audi")
print(car != "audi")
"""true,确实不等于 """
print(car == "Audi")
"""false,需区分大小写"""

#使用函数lower()的测试
car = "Audi"
print(car.lower() == "audi")
print(car.lower() != "audi")

print("#两个数字判断测试")
number1 = 100
number2 = 118
print(number1 == number2)
"""需留意判断是否等于为双等号"""
print(number1 != number2)
print(number1 <= number2)
print(number1 >= number2)
print("#使用关键字and/or")
print(number1 < number2 and number2 > 120)
print(number1 < number2 or number2 < 120)
"""多个条件时,用 and或or连接"""

print("#测试特定值是否包含/不包含在列表里面")
test_table = [1,2,3,4,6]
print(1 in test_table)
print(5 in test_table)

输出结果如下:


day14_01.png

2.alien_color.py

if语句的不同用法,外星人颜色后面的项目有提到

alien_colors = ["green","yellow","red"]
alien_color1 = alien_colors[0]
"""列表中取数字要留意格式,不需要点"""
if alien_color1 == "green":
    """不要忘记if后面的冒号"""
    print("You get 5 points!")
alien_color1 = alien_colors[1]
"""列表中取数字要留意格式,不需要点"""
if alien_color1 == "green":
    """不要忘记if后面的冒号"""
    print("You get 5 points!")
else:
    print("...")

#利用if-elif-else结构
alien_color2 = alien_colors[2]
if alien_color2 == "green":
    print("You get 5 points!")
elif alien_color2 == "yellow":
    print("You get 10 points")
else:
    print("You get 15 points")

age = 70
if age < 2:
    print("He/She is a baby!")
elif age < 4:
    print("He/She is learning walk!")
elif age < 13:
    print("He/She is a child!")
elif age < 20:
    print("He/She is a teenage!")
elif age < 65:
    print("He/She is a major!")
else:
    print("He/She is an old people!")

favorite_fruits = ["apple","orange","banana","cherry","leomen"]
if favorite_fruits:
    print("You love so much fruits!")
if "apple" in favorite_fruits:
    print("You really like bananas!")
if "Cherry" in favorite_fruits:
    print("You really like bananas!")

输出结果如下:


day14_02.png
其他
上一篇下一篇

猜你喜欢

热点阅读