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

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.

主站蜘蛛池模板: 通河县| 泰顺县| 永城市| 固镇县| 榆中县| 阿拉善右旗| 焉耆| 乐安县| 马龙县| 涞水县| 义乌市| 镇康县| 南皮县| 襄垣县| 开原市| 新密市| 邵东县| 兰考县| 慈溪市| 祁门县| 新余市| 徐汇区| 突泉县| 洱源县| 珠海市| 高安市| 宿松县| 习水县| 云阳县| 远安县| 丰台区| 攀枝花市| 湘潭县| 固安县| 深圳市| 石景山区| 贵州省| 天气| 扎兰屯市| 泽普县| 浙江省|