首页 /编程语言和算法/Python
 Python 条件语句if-elif-else和变量写一排
2025年4月23日 14:25
if 判断条件:
    执行语句……
else:
    执行语句……

举例1:

# 设定一个变量 i
i = 4

# 检查 i 是否能被 2 整除
if i % 2 == 0:
    print("The value of i is even.")
else:
    print("The value of i is odd.")

# 继续执行此后的代码
print("This is executed no matter the value of i.")

变量写一排,举例2:

a=1
b=2
c,d=1,4

print(a, b, c, d)

if a == c:
    print("a==c")
elif a != b:
    print("a != b")
else:
    if a <= d:
        print("a <= d")
    else:
        print("a > d")

input("Press Enter to exit...")

运行结果:

1 2 1 4
a==c
Press Enter to exit...


 
全部回复(0)
首页 | 电脑版 |