SunFounder ESP32 Starter Kit
>>> %Run -c $EDITOR_CONTENT
I like pear
I like apple
I like grape
Else in For Loop
The for loop can also have an optional else block. If the items in the sequence used for the loop are exhausted, the else
part is executed.
The break keyword can be used to stop the for loop. In this case, the else part will be ignored.
Therefore, if no interruption occurs, the else part of the for loop will run.
for val in range(5):
print(val)
else:
print("Finished")
>>> %Run -c $EDITOR_CONTENT
0
1
2
3
4
Finished
The else block will NOT be executed if the loop is stopped by a break statement.
for val in range(5):
if val == 2: break
print(val)
else:
print("Finished")
>>> %Run -c $EDITOR_CONTENT
0
1
3.6.8 Functions
In MicroPython, a function is a group of related statements that perform a specific task.
Functions help break our program into smaller modular blocks. As our plan becomes larger and larger, functions make
it more organized and manageable.
In addition, it avoids duplication and makes the code reusable.
292 Chapter 3. For MicroPython User