- Python Programming with Raspberry Pi
- Sai Yamanoor Srihari Yamanoor
- 133字
- 2021-07-02 23:48:45
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.
推薦閱讀
- 工業(yè)機器人虛擬仿真實例教程:KUKA.Sim Pro(全彩版)
- Excel 2007函數(shù)與公式自學寶典
- ServiceNow Cookbook
- AWS Certified SysOps Administrator:Associate Guide
- 計算機網(wǎng)絡安全
- Containers in OpenStack
- Mastering GitLab 12
- 統(tǒng)計挖掘與機器學習:大數(shù)據(jù)預測建模和分析技術(原書第3版)
- 在實戰(zhàn)中成長:C++開發(fā)之路
- 自適應學習:人工智能時代的教育革命
- Flash CS5二維動畫設計與制作
- 大話數(shù)據(jù)科學:大數(shù)據(jù)與機器學習實戰(zhàn)(基于R語言)
- 工業(yè)機器人與自控系統(tǒng)的集成應用
- Arduino創(chuàng)意機器人入門:基于Mind+
- 圖像傳感器應用技術