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

Nested loops

In Python, it is possible to implement a loop within a loop. For example, let's say we have to print x and y coordinates of a map. We can use nested loops to implement this:

for x in range(0,3): 
for y in range(0,3):
print(x,y)

The expected output is:

Be careful about code indentation in nested loops as it may throw errors. Consider the following example:

for x in range(0,10): 
for y in range(0,10):
print(x,y)

The Python interpreter would throw the following error:

    SyntaxError: expected an indented block

This is visible in the following screenshot:

Hence, it is important to pay attention to indentation in Python (especially nested loops) to successfully execute the code. IDLE's text editor automatically indents code as you write them. This should aid with understanding indentation in Python.

主站蜘蛛池模板: 潼关县| 利川市| 县级市| 安塞县| 曲阜市| 丽水市| 炉霍县| 罗源县| 阿图什市| 德庆县| 新巴尔虎右旗| 彰化县| 崇仁县| 高州市| 揭西县| 兰州市| 商都县| 洛宁县| 若尔盖县| 靖宇县| 古交市| 万年县| 龙南县| 如皋市| 淮南市| 兴和县| 潞西市| 岑溪市| 龙游县| 双流县| 永兴县| 锡林郭勒盟| 和平区| 普定县| 武川县| 陕西省| 榆树市| 玛纳斯县| 郴州市| 右玉县| 旌德县|