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

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.

主站蜘蛛池模板: 稻城县| 台中市| 论坛| 大方县| 陕西省| 淳化县| 石河子市| 平罗县| 福泉市| 铜鼓县| 临泽县| 凯里市| 双辽市| 墨玉县| 兰溪市| 搜索| 尉犁县| 新巴尔虎左旗| 孝昌县| 寻乌县| 锡林郭勒盟| 区。| 通州区| 宝丰县| 沾益县| 北川| 盱眙县| 宁津县| 即墨市| 新巴尔虎左旗| 贡觉县| 哈密市| 施甸县| 绥阳县| 勐海县| 噶尔县| 湟源县| 科技| 乌拉特中旗| 金乡县| 海兴县|