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

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.

主站蜘蛛池模板: 武强县| 富川| 财经| 合江县| 睢宁县| 桐柏县| 商洛市| 长泰县| 榆林市| 泰顺县| 泌阳县| 绥滨县| 榆树市| 泽州县| 扶余县| 舒兰市| 东城区| 连江县| 阳新县| 伊金霍洛旗| 吉木乃县| 利津县| 达拉特旗| 逊克县| 合阳县| 舞阳县| 滦南县| 南丹县| 平泉县| 黄平县| 顺平县| 登封市| 达州市| 高平市| 越西县| 蛟河市| 原平市| 自贡市| 德庆县| 蚌埠市| 金乡县|