Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:scriptingprogramming:pythonfundamentals [2023/09/06 08:31] – ekontoturbo | en:iot-open:scriptingprogramming:pythonfundamentals [2023/11/23 10:25] (current) – pczekalski |
---|
~~NOTOC~~ | ====== Python Fundamentals for IoT ====== |
==== ==== | {{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ |
<box #5374d5></box> | |
<box #5374d5></box> | |
==== Introduction to the Python programming for IoT ==== | |
<box #5374d5></box> | |
<box #5374d5></box>\\ | |
A program in Python is stored in text files on the device's file system, as Python's source code is interpreted, not compiled, opposite to C++. A typical file extension for programs in Python is ''.py''. | A program in Python is stored in text files on the device's file system, as Python's source code is interpreted, not compiled, opposite to C++. A typical file extension for programs in Python is ''.py''. |
In the context of IoT programming, both Python and Micropython share the same syntax and mostly the same libraries, so source code, in many cases, is portable. General hardware-related libraries like GPIO handling or timers are shared between those two Python worlds, and hardware-specific differences are minor compared to the Arduino framework.\\ | In the context of IoT programming, both Python and Micropython share the same syntax and mostly the same libraries, so source code, in many cases, is portable. General hardware-related libraries like GPIO handling or timers are shared between those two Python worlds, and hardware-specific differences are minor compared to the Arduino framework.\\ |
Python is simple and efficient in programming the not-so-complex IoT algorithms but does not offer the level of control needed in real-time applications. It can be easily used for prototyping, testing hardware and implementing simple tasks. | Python is simple and efficient in programming the not-so-complex IoT algorithms but does not offer the level of control needed in real-time applications. It can be easily used for prototyping, testing hardware and implementing simple tasks. |
| |
| <note important>Python is the language of the first choice when it comes to AI applications. Most autonomous devices (such as cars) run, in fact, Python code along with C++, hardware accelerated, on IoT fog class devices such as the NVIDIA Jetson family.</note> |
| |
<note> Obviously, Micropython does not contain nor allow the use of very complex libraries and frameworks that sometimes are provided to the developer with only binary backend (that is CPU or MCU specific) such as Tensorflow for AI applications.</note> | <note> Obviously, Micropython does not contain nor allow the use of very complex libraries and frameworks that sometimes are provided to the developer with only binary backend (that is CPU or MCU specific) such as Tensorflow for AI applications.</note> |
| |
<note tip>Note: Python script must be marked as "executable" to run it directly.</note> | <note tip>Note: Python script must be marked as "executable" to run it directly.</note> |
| |
In the following chapters, we present Python coding elements that are specific to the microcontrollers. A full Python course is beyond the scope of this publication, but it can be easily obtained online (links presented by the end of the chapter). | The following chapters present Python coding elements specific to the microcontrollers. A complete Python course is beyond the scope of this publication, but it can be easily obtained online (links presented by the end of the chapter). |
| |
=== IDEs for Python === | ==== IDEs for Python ==== |
A dozen of IDEs can be used to program in Python. The most common are: | A dozen of IDEs can be used to program in Python. The most common are: |
* IDLE Editor, formerly delivered with Raspbian OS in the bundle, requires GUI. It is currently obsolete but still popular among hobbyists. | * IDLE Editor, formerly delivered with Raspbian OS in the bundle, requires GUI. It is currently obsolete but still popular among hobbyists. |
* Thonny Python IDE, which comes with Raspbian OS, recently took over IDLE. | * Thonny Python IDE, which comes with Raspbian OS, recently took over IDLE. |
* Visual Studio Code with plugins for Python, common with Arduino framework and easily integrates remote Python development - it provides two development scenarios: local on the IoT device (Raspberry Pi, requires GUI) and remote from the PC to the IoT device, that works with headless Raspberry Pi OSes installations. | * Visual Studio Code with plugins for Python, standard with Arduino framework, that also easily integrates remote Python development - it provides two development scenarios: local on the IoT device (Raspberry Pi, requires GUI) and remote from the PC to the IoT device, that works with headless Raspberry Pi OSes installations. |
* PyCharm Community Edition requires additional installation ((https://raspberrytips.com/install-pycharm-raspberry-pi/)) and requires GUI. | * PyCharm Community Edition requires additional installation ((https://raspberrytips.com/install-pycharm-raspberry-pi/)) and requires GUI. |
* Simple code can be authored in the terminal using any text editor (i.e. Nano), as Python source files do not require compilation and are plain text ones. This is not very convenient, but it can help if no dedicated IDE and GUI are available, e.g., for rapid work remotely. | * Simple code can be authored in the terminal using any text editor (e.g. Nano), as Python source files do not require compilation and are plain text ones. This is not very convenient, but it can help if no dedicated IDE and GUI are available, e.g., for rapid work remotely. |
| |
The following subchapters present some IoT and embedded systems-specific Python programming and a very basic introduction: | <WRAP excludefrompdf> |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:building_your_first_project_python|First Program in Python]] | The following subchapters present some IoT and embedded systems-specific Python programming and an elementary introduction: |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:data_types_and_variable_python|Python Types and Variables]] | * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:building_your_first_project_python]], |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:program_control_structures_python|Python Flow Control Structures]] | * [[en:iot-open:scriptingprogramming:micropythonfundamentals]], |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:looping_python|Python Loops]] | * [[en:iot-open:scriptingprogramming:remotedevelopment]], |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:interrupts_and_sub-programs_python|Python Structural Programming]] | * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:data_types_and_variable_python]], |
* [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:hardware_specific_python|Hardware Specific Python]] | * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:program_control_structures_python]], |
| * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:looping_python]], |
=== Additional Resources for Python programming for beginners === | * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:interrupts_and_sub-programs_python]], |
| * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:hardware_specific_python]], |
| * [[en:iot-open:getting_familiar_with_your_hardware_rtu_itmo_sut:raspberrypi_rpi:networking_python]]. |
| </WRAP> |
| ==== Additional Resources for Python programming for beginners ==== |
For in-depth Python courses and more, follow the links: | For in-depth Python courses and more, follow the links: |
- The Python syntax and semantics: [[https://en.wikipedia.org/wiki/Python_syntax_and_semantics| Python Semantics]]. | - The Python syntax and semantics: [[https://en.wikipedia.org/wiki/Python_syntax_and_semantics| Python Semantics]]. |
- The Python Standard Library:[[https://docs.python.org/3/library/|PSL]]. | - The Python Standard Library:[[https://docs.python.org/3/library/|PSL]]. |
- Free online Python course: [[https://www.learnpython.org/|learnpython.org]]. | - Free online Python course: [[https://www.learnpython.org/|learnpython.org]]. |
| |
<todo @pczekalski #pczekalski:2023-08-26> Port info from the old manuals subpages in the namespace</todo> | |
| |
| |
| |