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

Flow control statements

Python has several flow control statements that will be familiar to people who know another language in the C family. For example, Python has loops and if, elif, and else branches (shown in the following code example):

selector = 5 
 
if selector < 3: 
    print("less than three") 
elif selector < 6: 
    print("less than six") 
else: 
    print("six or more") 
while selector > 0" 
    print('selector is {}' .format(selector)) 
    selector -=1 
 
for x in ['a', 'b', 'c', 'd']: 
    print(x) 
for x in range(5): 
    print(x) 

Python also has a for loop statement, but it's not like the for loops in C, C++, or Java. Instead of counting through numbers, the for loop iterates through the values. If we actually want to count through numbers with a for loop, that's easily done using a range iterator, as shown in the following screenshot in the output of the preceding code example:

Before we wrap-up this section, there's one last thing I should comment on and that's Python's views on indentation to signify the block structure.

主站蜘蛛池模板: 海丰县| 库尔勒市| 铁岭市| 建阳市| 鄂州市| 县级市| 静海县| 兰溪市| 梨树县| 化德县| 孟连| 洱源县| 徐闻县| 包头市| 西畴县| 慈利县| 丰顺县| 长阳| 贺州市| 西乌珠穆沁旗| 阳高县| 收藏| 高唐县| 乌审旗| 邢台市| 灵山县| 邢台市| 响水县| 绥江县| 海门市| 天门市| 大足县| 马公市| 彭泽县| 长沙县| 岑溪市| 吉木乃县| 库尔勒市| 丰台区| 博白县| 华蓥市|