This tutorial covers fundamental Python programming concepts essential for robotics and SLAM development.
| File | Topics |
|---|---|
01_basics.py |
Variables, data types, operators, control flow, loops |
02_data_structures.py |
Lists, tuples, dictionaries, sets, comprehensions |
03_functions.py |
Functions, lambdas, decorators, generators |
04_classes.py |
Classes, inheritance, magic methods, dataclasses |
05_file_io.py |
File I/O, JSON, CSV, exceptions, context managers |
06_modules_venv.py |
Modules, packages, virtual environments (venv) |
07_rustlike_python.py |
Rust-inspired patterns: type hints, dataclasses, ADTs |
# Create virtual environment
python3 -m venv .venv
# Activate (Linux/macOS)
source .venv/bin/activate
# Activate (Windows)
# .venv\Scripts\activate
# Install dependencies
pip install numpy opencv-python
# Run examples
cd examples
python3 01_basics.py
python3 02_data_structures.py
# ... etc
# Deactivate when done
deactivatecd examples
python3 01_basics.py# Numbers
x = 42 # int
y = 3.14 # float
# Strings
name = "Robot"
msg = f"Name: {name}" # f-string
# Collections
items = [1, 2, 3] # list (mutable)
point = (1.0, 2.0) # tuple (immutable)
config = {"speed": 2.0} # dict# Conditional
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
# Loop
for i in range(10):
print(i)
# List comprehension
squares = [x**2 for x in range(10)]def process_data(data: list, threshold: float = 0.5) -> list:
"""Process data with threshold filter."""
return [x for x in data if x > threshold]
# Lambda
square = lambda x: x ** 2from dataclasses import dataclass
@dataclass
class Point:
x: float
y: float
z: float = 0.0
class Robot:
def __init__(self, name: str):
self.name = name
def move(self, dx: float, dy: float):
# ...# JSON
import json
with open("config.json", "r") as f:
config = json.load(f)
# CSV
import csv
with open("data.csv", "r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)# Create
python3 -m venv .venv
# Activate
source .venv/bin/activate
# Install packages
pip install numpy scipy opencv-python
# Save dependencies
pip freeze > requirements.txt
# Install from requirements
pip install -r requirements.txt| Package | Description |
|---|---|
numpy |
Numerical computing |
scipy |
Scientific computing |
opencv-python |
Computer vision |
matplotlib |
Plotting |
rerun-sdk |
3D visualization |
transforms3d |
3D transformations |
-
Use type hints for better code documentation
def add(a: float, b: float) -> float: return a + b
-
Use virtual environments for every project
- Never install packages globally
- Keep dependencies isolated
-
Never use conda (per course guidelines)
- Use
venv+pipinstead
- Use
-
Use f-strings for formatting
print(f"Position: ({x:.2f}, {y:.2f})")
-
Use context managers for resources
with open("file.txt") as f: data = f.read()