Python Fundamentals for Robotics

macbook-laptop

I have mentioned before that C++ and Python are the most popular languages in Robotics. C++ is the best language if you want great performance. For example, if you want to write a robot vision algorithm that needs to run in real time, use C++. But if you are looking to quickly prototype something and don’t want the headaches of having errors fire all over your program because you forgot a semicolon, you should use Python. While Python uses more computing resources than C++, it is much simpler and far more intuitive.

In this tutorial, we will learn the fundamentals of Python for programming robots. Getting your head around these fundamentals will help you immensely when you learn ROS.

Table of Contents

You Will Need

In order to complete this tutorial, you will need:

Directions

How to Install Python in Ubuntu Linux

To install Python, open a new terminal window and type:

sudo apt-get install python python3

Find out what the default version of Python is on your system by typing:

python
1-python-versions

The default is versions 2.7.15 and higher.

2-default-python-version

C++ is a compiled language. You write source code, compile it into object/machine code, and then run that code. Python skips the compilation step, and instead runs (i.e. interprets) the source code directly without any compilation step. To find out where the Python interpreter is located, type this command.

which python
3-which-python

You can also type the following command to get the location of the interpreter as well as the documentation and other Python-associated files.

whereis python
4-whereis-python

Return to Table of Contents

Write Your First Program in Python

For our first program in Python, we will write a program that prints “Hello World” to the screen. Open up a terminal window and type:

python

Now type:

print(“Hello World”)
5-hello-world-terminal

What I showed you above is how to write a program directly inside the Python interpreter. You can keep writing different Python commands as you please. You write a line of code, and the interpreter executes that code, one line at a time.

The more common way to write Python programs is to create Python files (called scripts) containing lots of lines of code. You then run those files using the Python interpreter. Python files end with the .py extension. Let’s write a Python script now.

Open a terminal window and type gedit to open up the Linux text editor. Type the following program, and save it as hello_world.py. I am following the Google Python Style Guide.

6-hello-world-program

The program above might look a little intimidating at first. In the first line I told the program where to find the Python interpreter (#! /usr/bin/env python). That line is known as Shebang.

Then I wrote some comments to tell what the program does and what command to type in the terminal window to run the code (python hello_world.py).

Then I defined a method named main. This is where we implement our code.

The final block of code prints the comments and runs the main method.

Now, let’s exit the terminal.

Open a terminal window and move to the directory where hello_world.py is located.

Run the program.

python hello_world.py
7-run-hello-world

Another way is to run a program is to make a Python script executable, similar to what we do in C++. Open a new terminal window, move to where your hello_world.py program is located and type:

chmod +x hello_world.py
./hello_world.py
8-run-chmod

To rerun the program, you can press the up arrow on your keyboard to find the command again. Then press Enter.

9-run-chmod-again

Return to Table of Contents

Python Crash Course

Now that you know how to create Python programs and run them, check out the tutorial below to get a good foundation in the basics of Python. Do each tutorial in the “Learn the Basics” section. The tutorials are designed to be interactive. They cover the Python fundamentals that you will encounter repeatedly as you use this language to program robots. Proceed through each tutorial carefully so that you understand what is going on. 

Once you have done the basics section of the tutorial above, you will have a firm foundation in Python to begin the fun stuff…programming robots!

Return to Table of Contents