Python学习——if语句

if语句

思维导图

在这里插入图片描述

代码

# 5-1
cars = ['bmw', 'audi', 'subaru', 'toyota']
print(cars[2] == 'subaru')
print(cars[3] == 'subaru')
print(cars[2] != 'subaru')
print(cars[3] != 'subaru')
print(cars[2] == 'subaru' and cars[3] != 'toyota')
print(cars[2] == 'subaru' or cars[3] == 'subaru')
print('bmw' in cars)
print('subaru' not in cars)
age = 22
print(age == 21)
print(age != 21)
# 5-2
print("5-2")
str1 = 'Bmw'
# print(str1 == cars[0])
# print(str1 != cars[0])
# print(str1.lower() == cars[0].lower())
# print(str1.lower() != cars[0].lower())
numbers = list(range(1, 5))
# print(numbers[1] == numbers[2])
# print(numbers[1] != numbers[2])
# print(numbers[1] > numbers[2])
# print(numbers[1] < numbers[2])
# print(numbers[1] <= numbers[2])
# print(numbers[1] >= numbers[2])
print(cars[0] == 'bmw' and cars[3] == 'subaru')
print(cars[0] == 'bmw' or cars[3] == 'subaru')
print(9 in numbers)
print(9 not in numbers)
# 5-3
print("5-3")
alien_color = 'red'
if alien_color == 'green':
    print("You got 5 points.")
# 5-4
print("5-4")
if alien_color == 'green':
    print("You got 5 points.")
else:
    print("You got 10 points.")
# 5-5
print("5-5")
if alien_color == 'green':
    print("You got 5 points.")
elif alien_color == 'yellow':
    print("You got 10 points.")
else:
    print("You got 15 points.")
# 5-6
print("5-6")
age = 65
if age < 2:
    print("It is a baby.")
elif age < 4:
    print("It is a toddler.")
elif age < 13:
    print("It is a children.")
elif age < 20:
    print("It is a teenagers.")
elif age < 65:
    print("It is a adult.")
else:
    print("It is a old people.")
# 5-7
favorite_fruits = ['apple', 'banana', 'orange']
if 'apple' in favorite_fruits:
    print("You really like apple!")
if 'banana' in favorite_fruits:
    print("You really like banana!")
if 'orange' in favorite_fruits:
    print("You really like orange!")
if 'pear' in favorite_fruits:
    print("You really like pear!")
if 'peach' in favorite_fruits:
    print("You really like peach!")
# 5-8
print("5-8")
user_names = ['admin', 'root', 'scott', 'hr', 'system']
for user_name in user_names:
    if user_name == 'admin':
        print("Hello admin,would you like to see a status report?")
    else:
        print("Hello " + user_name + ",thank you for logging in again")
# 5-9
print("5-9")
user_names = []
if user_names:
    for user_name in user_names:
        if user_name == 'admin':
            print("Hello admin,would you like to see a status report?")
        else:
            print("Hello " + user_name + ",thank you for logging in again")
else:
    print("We need to find some users!")
# 5-10
print("5-10")
current_users = ['admin', 'root', 'scott', 'hr', 'John']
new_users = ['admin', 'root', 'Python', 'hzx', 'JOHN']
current_users_copy =[]
for current_user in current_users:
    current_users_copy.append(current_user.lower())
print("It's all done.")
for new_user in new_users:
    if new_user.lower() in current_users_copy:
        print("You need to enter a different user name.")
    else:
        print("This user name is not used")
# 5-11
print("5-11")
ordinal_numbers = list(range(1, 10))
for ordinal_number in ordinal_numbers:
    if ordinal_number == 1:
        print("1st")
    elif ordinal_number == 2:
        print("2nd")
    elif ordinal_number == 3:
        print("3rd")
    elif ordinal_number == 4:
        print("4th")
    elif ordinal_number == 5:
        print("5th")
    elif ordinal_number == 6:
        print("6th")
    elif ordinal_number == 7:
        print("7th")
    elif ordinal_number == 8:
        print("8th")
    else:
        print("9th")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值