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

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.

主站蜘蛛池模板: 安顺市| 饶阳县| 夹江县| 东山县| 佛山市| 西城区| 洞头县| 丽水市| 盘锦市| 阳曲县| 马鞍山市| 镇康县| 乐至县| 隆安县| 中方县| 平定县| 关岭| 隆子县| 滦南县| 开原市| 沙洋县| 石门县| 灵宝市| 鸡东县| 甘德县| 天门市| 巩义市| 扎赉特旗| 天津市| 黄龙县| 平顺县| 洛川县| 汝城县| 竹北市| 文山县| 桦川县| 凤山县| 宣武区| 枝江市| 浪卡子县| 鄂托克前旗|