This page (revision-17) was last changed on 2025-08-01 12:57 by Murray Altheim

This page was created on 2025-07-30 02:51 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
17 2025-08-01 12:57 2 KB Murray Altheim to previous
16 2025-07-30 12:48 2 KB Murray Altheim to previous | to last
15 2025-07-30 12:48 3 KB Murray Altheim to previous | to last
14 2025-07-30 12:48 3 KB Murray Altheim to previous | to last
13 2025-07-30 07:19 3 KB Murray Altheim to previous | to last
12 2025-07-30 07:18 2 KB Murray Altheim to previous | to last
11 2025-07-30 07:13 2 KB Murray Altheim to previous | to last
10 2025-07-30 07:12 2 KB Murray Altheim to previous | to last
9 2025-07-30 06:58 1 KB Murray Altheim to previous | to last
8 2025-07-30 06:56 1 KB Murray Altheim to previous | to last
7 2025-07-30 06:56 1 KB Murray Altheim to previous | to last
6 2025-07-30 06:55 1 KB Murray Altheim to previous | to last
5 2025-07-30 06:22 718 bytes Murray Altheim to previous | to last
4 2025-07-30 06:18 646 bytes Murray Altheim to previous | to last
3 2025-07-30 03:56 463 bytes Murray Altheim to previous | to last
2 2025-07-30 03:55 458 bytes Murray Altheim to previous | to last
1 2025-07-30 02:51 410 bytes Murray Altheim to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 1 changed one line
!!! Blocks
!!! Functions
At line 3 removed one line
%%info
At line 5 removed one line
%%
At line 7 changed one line
In Python, a block is a group of statements that belong together, forming the body of a function, loop, class or other structure. A block is defined by an initial line followed by the contents of the block indented four characters.
We can define __functions__ in Python. Functions are defined globally, within the file/module as they begin in column 0.
At line 9 removed 23 lines
As an example, a {{for}} block sets up a loop:
%%filename main.py %% %%python
for i in range(0,5):
print(i)
%%
%%warning
Notice that the contents of the {{for}} block are indented four characters. This causes the contents of the block to be executed five times, printing the value of the index variable ("i") each time.
%%
!! The try-except-finally Block
There is a {{try-except-finally}} structure called a __compound statement__, consisting of three separate __blocks__. This is ideal for a main.py file, and is also a very common and useful structure. It is composed of three parts:
* {{try}}: this block contains is the code we want to ''try'' to execute
* {{except}}: this block catches any errors or exceptions that occur and executes the block, handling the exception
* {{finally}}: no matter what happens, this block will be executed after the {{try}} (and possibly {{except}}) blocks are executed
In the below example, there is a {{try}} block, followed by an {{except}} block, followed by a {{finally}} block. A {{try}} block must be followed by an {{except}} block and/or a {{finally}} block (you must have one of them).
%%filename main.py %%
At line 33 removed one line
try:
At line 35 changed one line
print('hello world.') # remember to indent the insides of your blocks by 4 characters!
def display_message(message):
print(message)
raise Exception('we did something wrong.')
At line 37 removed 12 lines
except Exception:
# this block is executed if there is an exception (error)
print('an error occurred.')
finally:
# this is always executed, no matter what
print('complete.')
%%
If you want to use the exception to print out an error message you need to assign it to a variable (in this case, we've used {{ex}}).
%%filename main.py %%
%%python
At line 50 changed 21 lines
print('hello world.')
except Exception as ex: # we assign the Exception to the variable 'ex'
print('an error occurred: {}'.format(ex))
finally:
print('complete.')
%%
!! Optional Blocks
As mentioned above, in the {{try-except-finally}} compound statement, the {{except}} or {{finally}} block is optional.
If you don't need to do anything after exiting the {{try}} block you can leave off the {{finally}} block:
%%filename main.py %%
%%python
try:
print('hello world.')
display_message('hello world.')
At line 72 changed 11 lines
print('an error occurred.')
%%
or, just using {{finally}}:
%%filename main.py %%
%%python
try:
print('hello world.')
print('an error occurred: {}'.format(e))
At line 86 removed 7 lines
This would be useful when you want the Exception to be passed on and handled somewhere else.
----
__Previous:__ [MicroPython Tutorial Exercise 1|MicroPythonTutorialExercise01]: main.py \\
__Next:__ [MicroPython Tutorial Exercise 3|MicroPythonTutorialExercise03]: functions