SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
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
):
(val)
else
:
(
"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
(val)
else
:
(
"Finished"
)
>>> %Run -c $EDITOR_CONTENT
0
1
3.5.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.
Create a Function
def function_nameparameters)
"""docstring"""
statement(s)
• A function is defined using the
def
keyword
• A function name to uniquely identify the function. Function naming is the same as variable naming, and both
follow the following rules.
–
Can only contain numbers, letters, and underscores.
–
The first character must be a letter or underscore.
3.5. MicroPython Basic Syntax
121
Summary of Contents for Thales Kit
Page 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Page 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Page 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...