Month 3 Box - AI Deep Dive

Lesson 1.5: Python Programming Vs Arduino Programming

Python Programming Versus Microcontroller Programming

The rest of this month is written in Python, not the C++ you used on the microcontroller. This lesson bridges the two: it shows where to run Python, why it is so widely used for AI, and exactly how it differs from C++, no semicolons, no type declarations, and indentation that actually defines your code blocks. If you know the C++ fundamentals, Python will feel familiar and simpler.

Where you write and run Python

You have options. Online, tools like Google Colab or PythonAnywhere run code in the browser with no installation, which is handy for quick tests. Locally, the popular choice is VS Code, a free editor; installing its Python extension lets you run code from the editor or the terminal with python3 yourfile.py. You will also see two file types: a plain .py script, which is the standard clean, reusable form, and a Jupyter notebook, which runs code cell by cell and is easier for beginners and debugging. They run the same Python; only the workflow differs.

Why Python for AI

Python is known for being simple and readable, and above all for its enormous ecosystem of open-source libraries. There are mature, well-documented libraries for AI, machine learning, computer vision, numerical math, and plotting, so you rarely start from scratch. That is exactly why this AI-focused month uses Python: the hard parts are already written and you get to assemble them.

No semicolons, no type declarations

Two immediate differences from C++. First, lines do not end in a semicolon. Second, you do not declare a variable's type; Python figures it out automatically, so a = 10.5 makes a float and b = 3 makes an integer without you saying so. The built-in type function will report what it inferred. Lists work the same easy way: no type, no fixed size, and you can even mix types in one list. Printing is a single built-in call, print, with no headers or main function required.

Indentation defines your blocks

This is the biggest change to internalize. C++ groups code with curly braces, and indentation is just for looks. Python has no braces and no semicolons, so it uses indentation itself to decide which lines belong to an if, a loop, or a function. After the condition you write a colon, then indent the lines inside. Get the indentation wrong and the meaning changes: a line indented under an else runs only in the else, while the same line at the outer level always runs. It feels strict at first, but it forces clean, readable code.

Loops and functions, the Python way

A for loop uses range: for i in range(5) counts 0 through 4, and range can take a start, stop, and step. A while loop is a keyword, a condition, a colon, and an indented body, just like C++ minus the braces and semicolons. Functions use the def keyword with no return type and no parameter types: def add(a, b): then an indented return a + b. Because there are no fixed types, the same add works for two integers or an integer and a float without change. Comments start with a hash symbol.

Libraries: import and go

Python's real power is its libraries. After installing one (with pip), you bring it in with import, often under a short alias: import numpy as np gives you fast numerical arrays and math, and import matplotlib.pyplot as plt gives you publication-quality plots. These two appear constantly in AI and data work. The pattern is always the same: install once, import, then call the library's functions instead of writing that machinery yourself.

Working through it

Set up your environment. Install VS Code and its Python extension, or open an online notebook. Confirm you can run a file with python3 or the editor's run button.

Print and inspect types. Write print statements and use type() on a few variables to watch Python assign int, float, and str automatically.

Write a conditional with correct indentation. Build an if/else that reacts to a temperature value, paying attention to which lines are indented under which branch.

Loop with range and define a function. Use a for loop over range with different start, stop, and step values, then write a def function that adds two numbers and call it.

Import a library. Import NumPy to make an array and do vectorized math, or matplotlib to plot a simple curve, to feel how libraries extend Python.

The core differences in one script