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

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.

主站蜘蛛池模板: 冀州市| 汉中市| 寻乌县| 喀喇沁旗| 苏尼特左旗| 万安县| 韶关市| 教育| 横峰县| 潼关县| 横峰县| 抚州市| 洛扎县| 蓝山县| 井陉县| 乐清市| 图木舒克市| 扶沟县| 连州市| 宝鸡市| 盱眙县| 札达县| 阳东县| 达拉特旗| 屏东市| 万安县| 博白县| 松原市| 尼木县| 南城县| 唐山市| 清流县| 南开区| 辽阳县| 鄱阳县| 织金县| 依安县| 墨竹工卡县| 盘山县| 咸丰县| 仁化县|