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

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.

主站蜘蛛池模板: 洛川县| 柳林县| 陆良县| 当涂县| 靖宇县| 四会市| 宜兴市| 景泰县| 西安市| 奉化市| 大庆市| 丽江市| 威海市| 进贤县| 英山县| 丽江市| 平阴县| 和田县| 临城县| 安西县| 华蓥市| 凤城市| 上杭县| 曲阳县| 金川县| 延长县| 建始县| 巧家县| 舞钢市| 游戏| 枞阳县| 广饶县| 北海市| 石河子市| 隆子县| 三穗县| 龙井市| 丹棱县| 旬阳县| 金乡县| 政和县|