官术网_书友最值得收藏!

The if-else loop

if 1 is 3:
print "How did that happen?"
elif 1 > 3:
print ("Yikes")
else:
print ("All is well with the world")

The output of the above code is as follows:

All is well with the world

We can also do if-else and else-if blocks here too. Let's do something a little bit more complicated here. If 1 is 3, I would print How did that happen? But of course 1 is not 3, so we will fall back down to the else-if block, otherwise, if 1 is not 3, we'll test if 1 > 3. Well that's not true either, but if it did, we print Yikes, and we will finally fall into this catch-all else clause that will print All is well with the world.

In fact, 1 is not 3, nor is 1 greater than 3, and sure enough, All is well with the world. So, you know, other languages have very similar syntax, but these are the peculiarities of Python and how to do an if-else or else-if block. So again, feel free to keep this notebook around. It might be a good reference later on.

主站蜘蛛池模板: 兴义市| 徐州市| 泰兴市| 萨嘎县| 保德县| 深州市| 屏东县| 蒙自县| 绵阳市| 蒙阴县| 巢湖市| 黑龙江省| 连云港市| 甘孜| 桑日县| 焉耆| 徐州市| 嘉祥县| 温州市| 漾濞| 渑池县| 轮台县| 镇赉县| 巍山| 海林市| 通城县| 南宫市| 随州市| 镇坪县| 桐梓县| 科技| 巢湖市| 遵义市| 西昌市| 九台市| 宜丰县| 枣庄市| 清新县| 日土县| 兴仁县| 开江县|