Python中的IF条件语句

2022-05-03 00:00:00 python 语句 条件
"""
皮蛋编程(https://www.pidancode.com)
创建日期:2022/4/25
功能描述:Python中的IF条件语句
"""

# bool表达式
x = 69
print(x > 50)
print(x == 50)
print(x != 50)

my_age = 19
print(my_age > 21)
if my_age >= 21:
    print("Old enough.")
else:
    print("Not old enough.")
    print("Maybe next year.")

score = 72
if score > 90:
    print('Grade: A')
elif score > 80:
    print('Grade: B')
elif score > 70:
    print('Grade: C')
elif score > 60:
    print('Grade: D')
else:
    print('Grade: F')

# 如果有多个条件, 可以使用 and/or
my_age = 19
grade = 'C'
if my_age > 18 and grade == 'A':
    print('I can go to the party!')

# nested if statements -- both conditions must be True
my_age = 19
grade = 'C'
if my_age > 18:
    if grade == 'A':
        print('I can go to the party!')

# if 三元表达式
x = 10
y = 20
#  action/ if condition true/ else condfition false
z = x + y if x > y else y - x
print(z)
# result 10

相关文章