Python学习之[if]分支

为什么要用 if 分支:

因为在生活中,在不同的条件下生成的结果是不同的.这种关系需要通过分支来体现:

[if]   [if   elif  else]  [if else]

if语句产生的值是布尔值,布尔值若为真则执行if 下面的代码块

一.if condition:

代码块

如:

a=10
if a=10:
print("a")
显示结果为a
二. if else:
if conditions:
代码块
else:
代码块

若果if 后面条件成立为真则执行 if 代码块内容
若if 条件不成立则执行else后的代码块
a=10
if a>10:
print("a")
else:
print("b")
显示结果为 b 因为if 后的条件不成立

三.if elif else:
if conditions:
代码块
elif:
代码块
else:
代码块

elif 相当于否则
如:
type=input("请输入一个分数:")
type=int(type)
if type >=90:
print("A")
elif type>=80:
print("B")
elif type>=70:
print("C")
elif type>=60:
print("D")
else:
print("E")




原文地址:https://www.cnblogs.com/charles-lin/p/9579079.html