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

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.

主站蜘蛛池模板: 磴口县| 伊金霍洛旗| 河北区| 沁阳市| 台山市| 闽侯县| 丹阳市| 江西省| 曲松县| 安乡县| 龙泉市| 南皮县| 泰顺县| 出国| 汤阴县| 乌兰县| 隆回县| 永年县| 仪征市| 张掖市| 黑山县| 南宁市| 上杭县| 田林县| 崇信县| 清原| 宣城市| 汝城县| 新乡市| 张家川| 会昌县| 会理县| 巴彦淖尔市| 林西县| 郎溪县| 申扎县| 义乌市| 平阴县| 边坝县| 武汉市| 兴化市|