诏安县城乡规划建设局网站,公司网站购买主机,住房和城乡建设部网站进不去,药物研发网站怎么做基本概念
if语句为条件判断语句#xff0c;用来判断if后面的语句是真是假。if的用途有很多#xff0c;比如作为条件测试可以判断两数是否相等与不等、进行数值笔记等等。例子如下#xff1a;
Lego_price (599, 799, 898)
if Lego_price[0] 599:print(Correct!用来判断if后面的语句是真是假。if的用途有很多比如作为条件测试可以判断两数是否相等与不等、进行数值笔记等等。例子如下
Lego_price (599, 799, 898)
if Lego_price[0] 599:print(Correct!)输出结果
Correct!if语句经常用于检查列表中是否存在某个数
Lego [LegoCity, LegoMarvel, LegoHouse]
lego LegoHouse
if lego not in Lego:print(You forgot f{lego})由于LegoHouse是在列表中所以运行结果不显示任何语句。 当if后面需要判断的内容不止一个时我们可以用and和or进行连接 andand连接的所有项全为真最终结果才是真 oror连接的所有项至少有一个为真结果就是真 if语句
除了最简单的if语句之外还有一些稍微复杂一些的if语句帮助我们进行条件判断
if-else语句
Lego [LegoCity, LegoMarvel, LegoHouse]
lego LegoHouse
if lego not in Lego:print(You forgot f{lego})
else:print(Youve already buy it!)运行结果
Youve already buy it!if-elif-else语句
elif可以看做else if的简写利用该语句可以实现多重条件的判断
Lego_price (599, 799, 898)
if Lego_price[0] 100:print(Too cheap!)
elif Lego_price[0] 300:print(Its OK)
elif Lego_price[0] 500:print(A little expensive)
else:print(Too expensive!)输出结果
Too expensive!上面的else模块可以省去 if语句与for循环的结合
两者的结合可以很方便的处理列表中所有的元素
Lego_price (599, 799, 898)
for legoprice in Lego_price:if legoprice 100:print(Too cheap!)elif legoprice 300:print(Its OK)elif legoprice 500:print(A little expensive)else:print(Too expensive!)输出结果
Too expensive!
Too expensive!
Too expensive!