|
| 1 | +--- |
| 2 | +featured: micropython-101 |
| 3 | +title: '2. Micropython Environment - Editor' |
| 4 | +description: 'Learn the basics for loops on MicroPython.' |
| 5 | +author: 'Pedro Lima' |
| 6 | +hero_image: "./hero-banner.png" |
| 7 | +--- |
| 8 | + |
| 9 | +Arduino Labs for MicroPython includes a user-friendly code editor that helps us write, format, and run MicroPython code with ease. In this article, we’ll explore how the code editor works, key formatting rules for MicroPython, and some useful tips to keep our code clean and error-free. |
| 10 | + |
| 11 | +## The Code Editor in Arduino Labs for MicroPython |
| 12 | + |
| 13 | +The code editor in Arduino Labs is designed to streamline our coding experience, providing tools to: |
| 14 | + |
| 15 | +- Write and format MicroPython code. |
| 16 | +- View and manage indentation easily. |
| 17 | +- Run our code and view results directly in the REPL. |
| 18 | + |
| 19 | +As we get familiar with the editor, remember that MicroPython has a few syntax rules that differ from other languages we might know. |
| 20 | + |
| 21 | +## Indentation |
| 22 | + |
| 23 | +In MicroPython, indentation (using spaces or tabs) is essential for defining the structure of our code. Unlike many other languages, MicroPython relies on indentation instead of symbols like `{ }` to define code blocks. |
| 24 | + |
| 25 | +- **Indent with Consistency**: Typically, each block of code (such as inside a function, loop, or conditional) is indented by four spaces. Make sure all lines in the same block are indented with the same number of spaces. |
| 26 | +- **Nested Blocks**: For nested code, simply increase the indentation by another four spaces or one tab. |
| 27 | + |
| 28 | +**Example of Proper Indentation:** |
| 29 | + |
| 30 | +```python |
| 31 | +def greet(name): |
| 32 | + if name: |
| 33 | + print(f"Hello, {name}!") |
| 34 | + else: |
| 35 | + print("Hello, world!") |
| 36 | +``` |
| 37 | + |
| 38 | +In this example, each level of code indentation clearly defines which lines belong to which block. The `print` statements align under the `if` and `else` statements, showing they’re part of those blocks. |
| 39 | + |
| 40 | +### Avoiding Common Indentation Mistakes |
| 41 | + |
| 42 | +- **Mixing Tabs and Spaces**: Use either spaces or tabs consistently. Mixing them can lead to indentation errors. Tabs are generally preferred as these are easier to keep consistency. |
| 43 | +- **Missing Indentation**: Every line within a block (like the lines inside the `if` statement) must be indented correctly. If not, we’ll encounter syntax errors. |
| 44 | + |
| 45 | +## Comments: Inline and Multiline |
| 46 | + |
| 47 | +Comments are essential for making our code understandable and documenting complex logic. In MicroPython: |
| 48 | + |
| 49 | +- **Inline Comments**: Use `#` to add a comment on a single line. Anything following the `#` symbol will be ignored by the interpreter. |
| 50 | + |
| 51 | + ```python |
| 52 | + print("Hello, world!") # This prints a greeting |
| 53 | + ``` |
| 54 | + |
| 55 | +- **Multiline Comments**: While MicroPython doesn’t have a dedicated multiline comment syntax, we can use multiple inline comments to achieve the same effect, or use a multiline string with triple quotes (`""" ... """`) as a workaround. |
| 56 | + |
| 57 | + ```python |
| 58 | + # This is a comment |
| 59 | + # that spans multiple lines. |
| 60 | + |
| 61 | + """ |
| 62 | + Alternatively, we can use |
| 63 | + triple quotes to comment on |
| 64 | + multiple lines. |
| 65 | + """ |
| 66 | + ``` |
| 67 | + |
| 68 | +Using comments effectively helps both us and others understand the code’s purpose and logic. |
| 69 | + |
| 70 | +## MicroPython Syntax: No Semicolons Needed! |
| 71 | + |
| 72 | +In MicroPython, each statement ends automatically at the end of a line, so there’s no need to use semicolons (`;`) as in languages like C++ or Java. This simplifies code and makes it cleaner and easier to read. |
| 73 | + |
| 74 | +```python |
| 75 | +# In C++: |
| 76 | +# int a = 5; |
| 77 | +# int b = 10; |
| 78 | + |
| 79 | +# In MicroPython: |
| 80 | +a = 5 |
| 81 | +b = 10 |
| 82 | +``` |
| 83 | + |
| 84 | +If we’d like to place two commands on the same line, however, we can use a semicolon: |
| 85 | + |
| 86 | +```python |
| 87 | +a = 5; b = 10 |
| 88 | +``` |
| 89 | + |
| 90 | +But this is generally discouraged, as it can make the code harder to read. |
| 91 | + |
| 92 | +## Running and Compiling Code |
| 93 | + |
| 94 | +In MicroPython, code isn’t “compiled” like in traditional Arduino sketches. Instead, it’s **interpreted** line by line, making MicroPython highly interactive and flexible for rapid testing and development. |
| 95 | + |
| 96 | +- **Writing Code**: We write our code in the editor as usual. |
| 97 | +- **Running Code**: When we run our code, Arduino Labs for MicroPython sends it to the REPL, which reads and executes each line in real-time. |
| 98 | +- **Seeing Results**: The output (or any errors) appears immediately, allowing us to test code quickly and see immediate feedback. |
| 99 | + |
| 100 | +## Best Practices for Using the Editor |
| 101 | + |
| 102 | +Here are a few quick tips to make the most of our time in the code editor: |
| 103 | + |
| 104 | +- **Use Descriptive Names**: Name variables and functions clearly to make the code readable. |
| 105 | +- **Keep It Modular**: Split complex code into functions for easier maintenance. |
| 106 | +- **Comment as We Go**: Leave helpful comments explaining why we wrote certain sections of code. |
| 107 | +- **Indent Carefully**: Pay attention to indentation for clean, error-free code. |
| 108 | + |
| 109 | +## Try It Out |
| 110 | + |
| 111 | +- **Experiment with Indentation**: Try writing a few functions with nested loops or conditionals to get comfortable with indentation. |
| 112 | +- **Add Comments**: Document a small code snippet with comments explaining each part. |
0 commit comments