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

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.

主站蜘蛛池模板: 吴江市| 云龙县| 顺昌县| 台南市| 江城| 嘉鱼县| 垣曲县| 林周县| 永康市| 乌什县| 津市市| 油尖旺区| 远安县| 无极县| 中牟县| 米泉市| 长治市| 出国| 南康市| 瑞丽市| 二手房| 广平县| 和顺县| 霸州市| 桦川县| 嘉祥县| 汶川县| 甘洛县| 宁陕县| 锦州市| 锡林浩特市| 贺州市| 盐津县| 疏附县| 潼南县| 冕宁县| 吐鲁番市| 天津市| 达日县| 桂林市| 青阳县|