Stage 02 · Lesson 02/15

Programming & Python · Lesson 02

Python Fundamentals

Est. 30–40 min

Try it · Python in your browser

Run this lesson's example (hello_lab.py), change a number, run it again.

Why

Why the grammar of Python matters before joint commands

By the end you will run a small script, explain why indentation is syntax (not fashion), use comments, and fix a broken indent by eye.

NoteWe assume Python 3.10+ on your laptop. No robot hardware required for this lesson.

Concept

The big idea: statements, indentation, comments, scripts

A script is a .py file the interpreter runs from top to bottom.

  • Statement: one instruction (print("hi"), x = 3).
  • Comment: # ... — for humans; Python skips it.
  • Indentation: spaces at the start of a line define blocks (if, for, def). Prefer 4 spaces.
  • Run: python3 hello_lab.py from a terminal in that folder.

Why robots care

A missing indent can skip a safety if and send a motion you never meant. Treat indentation like wiring: wrong connection, wrong behavior.

How it works

How it actually works, step by step

  1. Create a file

    Save code as hello_lab.py in a folder you can find.

  2. Write statements

    Assignments and prints. One sensible idea per line.

  3. Open a block

    Lines ending with : start a block. Next lines indent.

  4. Run it

    python3 hello_lab.py — read stdout; fix errors from the top message down.

Visual

Visualise it: command console

Issue simple textual commands. Notice order matters — same as script order.

Interactive · Robot command console

Commands (Python-like):
  drive_forward(cm=20)
  turn_left(deg=90)
  turn_right(deg=90)
  stop()
  status()
  • # robot console ready — type a command

Worked example

Worked example: ready vs not-ready

Battery flag True → print ready. False → print charge first. The if block must be indented; the final “Script finished.” stays at the left margin so it always runs.

Code lab

Code lab: hello lab script

Copy and run. Match the output exactly.

hello_lab.py · Python
#!/usr/bin/env python3
"""First lab script — statements, comments, indentation."""

# Comments start with # — the robot ignores them
lab_name = "AIHUB Robotics Lab"
print("Hello,", lab_name)

# Indentation matters: this block belongs to if
battery_ok = True
if battery_ok:
    print("Status: ready for mission")
else:
    print("Status: charge battery first")

print("Script finished.")

Expected output

Hello, AIHUB Robotics Lab
Status: ready for mission
Script finished.

Real world

Where fundamentals show up in the lab

  • Bring-up scripts: print “driver up” before motion.
  • CI tests: tiny scripts that must exit 0.
  • Notebooks vs scripts: notebooks are great for explore; ship behavior as .py files.

Mistakes

Common mistakes and confusions

Hardware & safety

Hardware lab and safety notes

Set up a tidy desk “lab bench” for coding: laptop or Raspberry Pi, a terminal, a text editor, and one project folder. Good habits now save hours later.

  • Make a folder ~/aihub-lab/stage-02/ and save every script there.
  • Check your version with python3 --version (you want 3.10 or newer).
  • On a Raspberry Pi, Python 3 is already installed with Raspberry Pi OS.
Raspberry Pi 4 Model B single-board computer, top view
Raspberry Pi 4 — a small Linux computer that runs the same Python scripts as your laptop.Photo: Laserlicht · CC BY-SA 4.0 · Source: Wikimedia Commons · resized, converted to WebP

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
SyntaxError: invalid syntax at print "hi"Python 2 style printUse print("hi") and run with python3
IndentationError: expected an indented blockNothing indented after :Indent the block 4 spaces
No such file or directoryTerminal is in a different foldercd into the folder, or use ls to check
Script runs but prints nothingFile not savedSave (Ctrl+S) before running
NoteRead the last line of a Python error first — it names the problem — then the line number above it. Change one thing at a time and re-run.

Think

Think about it

Think

Why is IndentationError a feature for robots, not a nuisance?

Experiment

Try it in the lab: indent audit

Break and fix indentation on purpose.

Experiment

Indent audit

You will need

  • Terminal
  • hello_lab.py

Steps

  1. Run the script; confirm output matches the lesson.
  2. Remove indent from the print under if; run; read the error.
  3. Fix indent; run again.
  4. Add a second comment line describing your robot goal in one sentence.

Interactive · Robot command console

Commands (Python-like):
  drive_forward(cm=20)
  turn_left(deg=90)
  turn_right(deg=90)
  stop()
  status()
  • # robot console ready — type a command

You should see IndentationError when broken and clean output when fixed.

Knowledge check

Check your understanding

Knowledge check

1. In Python, indentation…

2. Best command to run a script on most lab laptops?

3. Comments starting with # are…

4. A line ending with : usually means…

5. Mixing tabs and spaces often causes…

Practice quiz

Loading practice quiz…

Coding practice

Loading coding exercises…

Mini assignment

Hands-on assignment

Mini assignment

Hello lab script

Write a 15–25 line script that prints your name, a robot goal, and a ready/not-ready branch.

Deliverables

  • hello_lab_custom.py
  • Screenshot or paste of terminal output

Do this, step by step

  1. Create hello_lab.py, copy the lab code and run python3 hello_lab.py.
  2. Change battery_ok to False and run again. Which line changed?
  3. Remove the indent before one print inside the if and read the error message from the top.
  4. Write hello_lab_custom.py (15–25 lines) with your name, a robot goal and a ready/not-ready branch.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: With battery_ok = False the output says “charge battery first”.
  2. Hint 2: Python reports IndentationError and gives the line number — read that number first.
  3. Hint 3: Use the rubric: runs with python3, uses if/else, has at least two comments.

Challenges (pick by level)

  • 🟢 Easy: Print the robot goal in capital letters with .upper().
  • 🟡 Medium: Add a battery_pct number and print ready only if it is 20 or more.
  • 🟠 Hard: Ask for the battery value with input() and convert it with int().

Next step

Next, Lesson 3: Variables, Types & Operators. Related: using the terminal well in Stage 3 — Linux & Dev Environment.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in