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

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.

主站蜘蛛池模板: 山东省| 临澧县| 通化市| 潜山县| 临澧县| 松滋市| 新乡市| 崇左市| 乐山市| 西昌市| 千阳县| 通道| 汽车| 德江县| 梁河县| 澄江县| 丹巴县| 清流县| 新丰县| 抚远县| 肃北| 岗巴县| 宣城市| 邯郸市| 延川县| 江油市| 彭泽县| 达州市| 周宁县| 都兰县| 饶平县| 南康市| 安溪县| 马山县| 筠连县| 山丹县| 衡山县| 三台县| 留坝县| 麻栗坡县| 海林市|