Python Fundamentals for Robotics

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. And, for this reason, if you search for “robotics software engineer” on the job site Indeed or on LinkedIn, you will find that the majority of job listings require you to know Python.

In this tutorial, we will learn the fundamentals of Python. The fundamentals aren’t the most exciting stuff…that’s why they are called fundamentals.

The fundamentals never change (or only change ever so slightly). Getting your head around these fundamentals will help you immensely when you learn ROS, the most popular robot programming platform. 

We will go over the pieces of the language you will use again and again as you program robots. This post covers the fundamental building blocks of every Python program you will ever write.

Python is a really big language, but you don’t have to know every tiny little detail to get started building powerful robots. In fact, there are really only a handful of things that you will use repeatedly. 

If you have been following me for a while, you know that I’m a firm believer in the Pareto Principle (also known as the 80/20 rule). Don’t waste your time memorizing the language. I tried doing that early on in my career, only to realize later on that I wasted a lot of time learning stuff I never used again. 

My advice is to get the basics of Python down, and then go out and build robots. Any time you see something you don’t understand, just look it up on Google. That’s how we learn in robotics.

This post has a lot of steps, but be patient and take your time as you work all the way through it. By the end, you will have a firm grounding in the most important pieces of Python and will be ready to get out there and start coding up real robots in the wild.

Let’s get started!

Table of Contents

You Will Need


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:


The default is version 2.7.15 and higher.


C++ is a “compiled” language. A compiled language is one in which you write code (i.e. source code), translate (i.e. compile) that code into code that your machine can understand, and then run that code. Python skips the compilation step, and instead runs (i.e. interprets) the source code directly without that compilation step.

To find out where the Python interpreter is located, type this command.

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

Return to Table of Contents

Install Pip

Let’s begin by installing pip. Pip is a tool that will help us manage software packages for Python.

Software packages are bundles of code written by someone else that are designed to solve a specific problem. Why write code to solve a specific problem from scratch, when someone else has already written code to solve that exact same problem? That is where software packages come into play. They prevent you from having to reinvent the wheel.

Open up a fresh Linux terminal window.


Type the following command to update the list of available packages that can be installed on your system.

sudo apt-get update

Type your password.


Upgrade all the packages. The -y flag in the following command is used to confirm to our computer that we want to upgrade all the packages.

sudo apt-get -y upgrade

Type the following command to check the version of Python you have installed.

python3 --version

My version is 3.6.9. Your version might be different. That’s fine.

Now, let’s install pip

sudo apt-get install -y python3-pip

If at any point in the future you want to install a Python-related package using pip, you can use the following command:

pip3 install package_name

For example, to install the scientific computing package called Numpy, you would type:

pip3 install numpy

Return to Table of Contents

Create a Virtual Environment

In this section, we will set up a virtual environment. You can think of a virtual environment as an independent workspace with its own set of libraries, settings, packages, and programming language versions installed. 

For example, you might have a project that needs to run using an older version of Python, like Python 2.7. You might have another project that requires Python 3.7. Setting up separate virtual environments for each project will make sure that the projects stay isolated from one another.

Let’s install the virtual environment package. 

sudo apt-get install -y python3-venv

With the software installed, we can now create the virtual environment using the following command. The dot(.) in front of py3venv makes the directory a hidden directory (the dot is optional):

python3 -m venv .py3venv

Type the following command to get a list of all the directories. You should see the .py3venv folder there.

ls -a

List all the contents inside the .py3venv folder.

ls .py3venv/

Now that the virtual environment has been created, we can activate it using the following command:

source ~/.py3venv/bin/activate

Look what happened. There is a prefix on the current line that has the name of the virtual environment we created. This prefix means that the .py3venv virtual environment is currently active.

When a virtual environment is active that means that when we create software programs here in Python, these programs will use the settings and packages of just this virtual environment. 

Keep your terminal window open. We’re not ready to close it just yet. Move on to the next section so that we can write our first program in Python.

Return to Table of Contents

Create Your First Program (“Hello World”)

Let’s create our first program. Programs is Python are often called scripts. In this post, sometimes I will say scripts and sometimes I will say programs. Both words mean the same thing.

Create a new directory named python_for_robotics.

mkdir python_for_robotics

Move to that directory.

cd python_for_robotics

Create a new program named using gedit, the built-in text editor in Linux.


A blank screen should pop up. This is where we will write our program.


Now, type the following Python code in the text editor.

#!/usr/bin/env python

print("Hello, World!")

Click Save, and then close the text editor.

If you type ls and press Enter, you will see your program there.


Now, run the program by typing:


Here is the output you should get:


Let’s leave the virtual environment now by typing:


You should see that the virtual environment prefix is gone.

Now return to your home directory.

cd ~

Return to Table of Contents

Create Your Second Program

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


Now type:

print(“Hello World”)

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. This is exactly what we did in the previous section. You then run those files using the Python interpreter.

Python files end with the .py extension. Let’s write another Python script now.

Open a terminal window.

Move to your Desktop.

cd Desktop

Type gedit to open up the Linux text editor.

Type the following program, and save it as I am following the Google Python Style Guide.


The program above might look a little intimidating at first. Don’t worry about trying to understand everything at this stage. We’ll cover everything by the end of this post.

Here is what you should know right now:

  • 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
  • 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 is located (mine is on the Desktop).

Run the program.


Return to Table of Contents

Make Your Second Program Executable

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 program is located, and type:

chmod +x

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


Return to Table of Contents

Do Basic Calculations

Now that you have written some programs in Python, let’s see how we can do some basic mathematical calculations.

Open a fresh Linux terminal window. 

As you saw earlier, you can launch Python directly in your terminal window without having to write all the code in an actual file. You can then run pieces of code, one at a time. Let’s do that now.

Type the following command to launch the Python interpreter.


Your screen should look like this:


Let’s add two numbers together and see what Python produces for the output.

3 + 15

You should see 18, which is the sum of 3 and 15. 


If we wanted to run this code in an actual file, we can do it. First, we need to exit the Python interpreter. Type this:


Go to the python_for_robotics directory.

cd python_for_robotics

Open a new Python file named


We will create a Python program that adds two numbers together. Here is the Python code:

#!/usr/bin/env python

print(3 + 15)

Click Save and then close the window to go back to the terminal window.

To run the code you just wrote, type the following command:


You should see 18.


In this example, we did addition, but you can do a lot of other mathematical operations such as subtraction, multiplication, and division.

Return to Table of Contents

Create Variables

Now let’s explore how to create variables. What are variables? Variables are containers. They are just like the plastic containers you use to store food in your refrigerator; except, variables don’t store food…they store information such as numbers, text, and lists of numbers and text. Variables have names as well, which helps us distinguish one variable from another.

Your first encounter with variables was probably when you were a teenager taking your first class in algebra. For example, look at this mathematical expression.

x = 10

x in this case is a variable. It contains the value 10. The name of this variable is x. 

Variables are called variables because they can vary in value. Let’s take a look at this a little bit closer.

Open up a fresh terminal window in Linux.

Launch the Python interpreter (remember that any code that you run directly in the Python interpreter can also be placed in a .py file and run using the python <file name goes here> command).


Let’s create a variable named automaticaddison and assign it a value of 100.

automaticaddison = 100

Print the variable.


Let’s create another variable named number and assign it the value 34.25.

number = 34.25

Now print it.


You should see the number print to your screen.

Let’s create another variable named my_name and assign it the value of “Addison”. “Addison” in this case is known as a string. A string is a collection of letters. In Python, we always enclose strings in either single or double quotes. It doesn’t matter which one you choose, just stay consistent.

my_name = "Addison"

Print it.


Here is the output.


Can we do mathematical operations using just variables? Yes we can. Let’s add the automaticaddison and number variables and see what we get. We should get 134.25 because the value of automaticaddison is 100, and the value of number is 34.25.

print(automaticaddison + number)

Remember that variables are called variables because they can vary in value.

How about we change the value of automaticaddison from 100 to 1000?

automaticaddison = 1000

Now type this code and press Enter.

print(automaticaddison + number)

You should get 1034.25, which makes sense because 1000 + 34.25 = 1034.25.

Return to Table of Contents

Work With Lists

What if we want to create a list of numbers and assign this list to a variable? I’ll talk about that in this section.

Let’s start by creating a list of the numbers from 1-10. I’m going to open up a new terminal window.

Launch the Python interpreter.


Type the following code.

list_of_numbers = [1,2,3,4,5,6,7,8,9,10]

You just created a list of the numbers one through 10. A list in Python is enclosed in square brackets.

Let’s print the list.  


Here is what the output looks like. 


What do we do if we want to print the first item in the list? The first item in this list of 10 numbers is number 1. In Python, a list begins at index 0. Therefore, in order to print the first item in the list, we write the following code:


Here is the output:


What about if we want to print out the last item in the list, number 10. 10 is at index 9, so we need to type this code:


Lists aren’t just limited to numbers. We can make a list of strings as well.

Recall that a string is a collection of letters. Your name is a string, for example. AutomaticAddison is a string. A string can also include punctuation and spaces: What is the best website for robotics? 

Earlier in this post, you worked with the string, Hello World.

Let’s create a list of strings. Remember that strings have to be enclosed in either double quotes (“) or single quotes (‘). The name of the list will be days_of_the_week.

days_of_the_week = ["Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

Print the second day of the week.


Here is what you should get:


What if we want to print all days of the week after Tuesday? Tuesday is at index 2 in the list, so we want everything from index 3 to the end of the list.

Here is the code that we write:


That colon (:) after the index in the list means “get everything from the index to the end of the list.”

That’s it for lists. Now let’s move on to tuples. 

Return to Table of Contents

Create a Tuple

In this section, we’ll take a look at how to create a tuple. Tuples are like lists except for two differences:

  1. Once you create a tuple, you cannot change it.
  2. Tuples are enclosed in parentheses while lists are enclosed in square brackets.

Aside from these differences, everything else is the same.

Let’s create a tuple of the first five letters of the alphabet.

alphabet = ("a","b","c","d","e")

Print it.


Let’s try to change the middle value of the tuple (the letter ‘c’). Let’s change ‘c’ to ‘z’. ‘c’ is located at index 2.

alphabet[2] = "z"

Here is the output:


You should see a message that says: 

TypeError: ‘tuple’ object does not support item assignment.

You are seeing this error because you can’t modify the values in a tuple. If you want to change the value in a tuple, you need to have made it a list instead of a tuple. Remember this: tuples cannot change after you’ve created them.

Now that we have seen what tuples are, let’s move on to dictionaries, another built-in data type in Python. 

Return to Table of Contents

Create a Dictionary

A dictionary in Python is a lot like a dictionary in the real world. In a real-world online dictionary, you type in a word, and the program outputs the definition of that word; in a Python dictionary, you type in a key, and the program outputs the value of that key.

A real-world dictionary is made up of word-definition pairs. A Python dictionary consists of key-value pairs. The value in a Python dictionary can be any data type you can think of (lists, tuples, numbers, strings, etc.).

Let’s see how to create a dictionary in Python. Open a new terminal window and launch the Python interpreter.


Let’s create a dictionary of capitals of some countries around the world. The name of the dictionary will be world_capitals.

First create the dictionary. To create the dictionary, you use curly brackets {}.

world_capitals = {}

Add  a new item (i.e. key-value pair) to the dictionary.

world_capitals["United States"] = "Washington, D.C."

Print the dictionary.


We could have also added multiple capitals all in a single line of code. Here is how we do that:

world_capitals = {"United States":  "Washington, D.C.", "France": "Paris", "India": "New Delhi"} 

What is the capital of France? To print the value for the “France” key in the dictionary we just created, type this code:


Let’s print a list of all the values.


Print a list of all the keys.


Now let’s create another dictionary that has population values. We’ll then print it.

population = {"United States":  327000000, "France":66990000, "India": 1339000000} 

That’s it for dictionaries. If you want to have a handy reference for Python’s built-in methods for dictionaries, check out this list.

Our next stop in this post is the conditional statement.

Return to Table of Contents

Using If-Else Statements to Make Decisions

When programming robots, we often want a robot to take some action or return some data based on some condition. For example, imagine a security guard robot whose job is to check the age for each person wanting to enter a nightclub. Suppose this robot is naive. Instead of checking the person’s ID, the robot asks for the person’s age.

We want this robot to allow anybody inside the club who is 18 years old or older but turn away anyone who is under the age of 18. In other words, if the person is greater than or equal to 18 years old, let them in, otherwise, keep them out.

Robot security guard checking the age of the person wanting to enter the nightclub.

Let’s work through the code for this robot together. 

Open up a fresh terminal window and navigate to the python_for_robotics directory.

cd python_for_robotics

Create a new Python file named


Type the following code. 

#!/usr/bin/env python

# Robot security guard for a nightclub

# Ask for the person's age
age = input("How old are you?: ")

# Make a decision based on the person's age
if age < 18:
  print("You may NOT enter")
  print("You may enter")

The hash # symbol is how we write comments in Python. Everything after the # symbol is a comment. When we run the program on our computer, the computer will ignore this line.

The input command is Python’s way of accepting input from the user (in this case, the person trying to enter the nightclub). We store the user’s input inside the variable named age.

The next piece of code is an if-else statement

An if-else statement consists of the if keyword, followed by a condition (i.e. age greater than or equal to 18 years old) and a colon (🙂. All the code after the colon (but before the else keyword) will run if the condition is TRUE. Otherwise, if the condition is FALSE (the person is under the age of 18 years old), all the code in the block of code below the else keyword will run.

Note that I indented the blocks of code below the if and the else keywords.

Remember this as it is very important: In Python, indenting (i.e. leaving whitespace using either the Tab key on your computer or the Space bar) has meaning. Code that is at the same location in the program and is indented the same number of spaces from the left margin will run together. 

This is just like indenting a paragraph in a book or essay. Programming statements are like sentences. Blocks of code (i.e. a grouped set of programming statements) are like paragraphs. Each time you indent in Python, you are creating a new block of code.

Now, let’s run the program. Click Save and exit the text editor (i.e. gedit).



Here is the output:


We can also add more conditions using what are known as elif (short for else-if) statements. Let’s modify our program so that anybody who is between the ages of 18-21 is allowed to enter, but they cannot drink alcohol.

Type the following command to open the program:


Write the following code. Make sure your code looks exactly like what you see here.

#!/usr/bin/env python

# Robot security guard for a nightclub

# Ask for the person's age
age = input("How old are you?: ")

# Make a decision based on the person's age
if age < 18:
  print("You may NOT enter")
elif age >= 18 and age < 21:
  print("You may enter, but you CANNOT drink")
  print("You may enter, and you CAN drink")

Now save the file, exit the text editor, run the code, and observe the output. 


There you have it…conditional statements in Python. These statements are useful for enabling a robot to make decisions based on a set of conditions

Now let’s take a look at another Python structure: loops.

Return to Table of Contents

Do Stuff Again and Again Using Loops

When programming robots, we often want the robot to do something again and again. For example, we might want a security guard robot for a hotel to repeatedly walk up and down a hallway (move left → move right→ move left–> move right, etc.).

Python has two ways to run the same pieces of code again and again:

  1. For loops
  2. While loops

Return to Table of Contents

For Loops

For loops allow the code inside them to be executed repeatedly for a known number of times. Let’s look at an example.

Imagine we want our security guard robot to walk up and down the hall 5 times. Each time the robot moves, it prints its motion to the screen. One way to do this is by writing the following Python code:

print("robot moving right")
print("robot moving left")
print("robot moving right")
print("robot moving left")
print("robot moving right")
print("robot moving left")
print("robot moving right")
print("robot moving left")
print("robot moving right")
print("robot moving left")

Typing out the same code over and over gets tedious really fast. You will be happy to know there is a better way…and that is where the for loop comes into play.

Open up a fresh terminal window and type the following commands.

cd python_for_robotics

Now type the following code:

#!/usr/bin/env python

# Here is our for loop. 
# We use the range keyword to specify 
# the number of times we want the code inside
# the for loop to execute.
for i in range (5):
  print("robot moving right")
  print("robot moving left")

The variable i on the line of the for loop starts at 0 and ends at 5. It increases by 1 after each time the code inside the for loop executes. 

We could have just as easily written the following equivalent code:

#!/usr/bin/env python

# From 0 to 5 (i.e. run the code 5 times)
for i in range (0, 5):
  print("robot moving right")
  print("robot moving left")

Save the program and close it to go back to the Linux terminal window.

Now type this command to run the code:


What did you get for the output? You should get something that looks like this?


We can also use for loops to move through each item in a list, one-by-one. Software engineers have a name for this process: iteration. Open your file.


Add the following code underneath the for loop.

#!/usr/bin/env python

# Here is our for loop. 
# We use the range keyword to specify 
# the number of times we want the code inside
# the for loop to execute.
for i in range (5):
  print("robot moving right")
  print("robot moving left")

# Create the list
colors = ["red","orange","green","blue"]

# Here we are using x instead of i. We can use
# any character for this variable.
for x in colors:

Save the program and close the window to return to the terminal window.

Now run the code:


Here is the output you should get:


That’s it for the basics of for loops. Now let’s move on to while loops.

Return to Table of Contents

While Loops

The other type of loop in Python is called a while loop. While loops execute a set of statements until a condition is FALSE (i.e. no longer true). 

For example, let’s say we have a robot that operates in a three-dimensional space. The robot moves around the space and prints its x, y, and z coordinates. Once the robot gets more than 5 units from the origin (where x=0, y=0, and z=0), in either the x, y, or z direction, the robot stops printing its position. Let’s use a while loop to make this happen.

Open a fresh terminal window.

Change directories.

cd python_for_robotics

Create a new Python file called


Write the following code:

#!/usr/bin/env python

# The robot begins at the origin (x=0, y=0, and z=0)
robot_x = 0
robot_y = 0
robot_z = 0

# Print the robot's starting position
print("Current Position: x=" + str(robot_x) + " y=" + 
  str(robot_y) + " z=" + str(robot_z))

# Stop executing code once either x, y, or z exceeds 5
while (robot_x < 5 or robot_y < 5 or robot_z < 5):

  # Increment the robot's x position by 1
  robot_x += 1

  # Increment the robot's y position by 1
  robot_y += 1

  # Increment the robot's z position by 1
  robot_z += 1

  # Print the robot's current position  
  print("Current Position: x=" + str(robot_x) + " y=" + 
    str(robot_y) + " z=" + str(robot_z))

print("Mission complete.")

Save the file and close the terminal window.

Now type the following command to run the code:


Here is the output.


That’s it for while loops. Now let’s take a look at functions.

Return to Table of Contents

Using Functions

A function in Python is a piece of code designed to perform a specific task. When you are writing big programs with hundreds of lines of code, programs without functions can be really hard to read and debug (i.e. debug means “to find errors”). In this case, you can use functions to group lines of code into separate bundles based on the specific task they are designed to perform. 

Functions are really helpful because they allow you to reuse pieces of code rather than typing identical lines of code again and again. 

For example, imagine if you had to add multiple pairs of numbers together and print out the sum each time. Let’s do this now.

Open a new Linux terminal, and type the following commands:

cd python_for_robotics

Now type the following code:

#!/usr/bin/env python

# Add 3 and 4 and print the result
a = 3 + 4
print("3 + 4 = " + str(a))

# Add 5 and 7 and print the result
b = 5 + 7
print("5 + 7 = " + str(b))

# Add 2 and 9 and print the result
c = 2 + 9
print("2 + 9 = " + str(c))

Save the code, and return to the terminal.

Run the script (Remember that a script is the same thing as a program in Python).


Here is the output.


Now, let’s rewrite the script using functions. Type this command:


Write the following code. All functions need to first be defined before we can use them. The Python keyword def (which means define) is used right before you define a function:

#!/usr/bin/env python

# Define the add function.
# This function accepts two numbers 
# and adds them together.
# The function also prints the sum.
def add(number1, number2):
  sum = number1 + number2
  print(str(number1) + " + " + str(
    number2) + " = " + str(sum))

# Define the main function. 
# This method calls the add function above.
def main():

# When we run the program, this is where 
# the program will start executing code.

Save the script, return to the terminal, and run the script.


Here is the output.


See how we condensed all of that tedious, repetitive code (in using functions? 

Now, when we want to add two numbers together, all we need to do is to add a single line of code inside the main function.

For example, if we want to add 25 and 30 together, we add this line of code inside the definition of the main function:

add(25, 30)

Functions can accept input, perform some calculation or processing, and then generate output. Functions are not required to accept input, but they often do.

When a function (e.g. add) is called by the main function, the code inside that function executes (e.g. numbers are added, and the sum is printed). Once the block of code inside the function executes, program control then goes back to the main program and continues where it left off.

That’s it for the basics of functions. Functions can do a lot more than what I have covered here, but this is really all you need to know at this stage.

If you’re interested in learning more about functions, you can check out this tutorial

Return to Table of Contents

When Things Go Wrong: Handling Exceptions

You know that sometimes things do not go as planned when you write a piece of software. Your program might stop all of a sudden due to some unforeseen error.

You should anticipate errors that might appear when running a program and then find a way to handle them. Errors in Python are referred to as exceptions. I’ll show you below how to handle exceptions in Python.

Open a new Linux terminal, and type the following commands:

cd python_for_robotics

Now type the following code:

#!/usr/bin/env python

# Here we will print the variable named automaticaddison.

Save the file, and exit the code editor to return to the terminal window.

Run the code that you have just written:


Here is the output.


Whoa, that is ugly! What just happened?

The problem is that you tried to print a variable named automaticaddison, but you never defined the variable. Therefore, your program stops right dead in its tracks because it has no idea what automaticaddison means. 

Let’s fix this error by using Python’s built-in error handling capabilities.

Type the following command:


Delete all the code that is currently in the file.

Now type this code (The \n character puts the text after it on a new line):

#!/usr/bin/env python




  print("An exception occurred.\nVariable automaticaddison is not defined.")

Save the file and return to the terminal window. 

Run the code.


Much better! What is happening in the code?

When you run the script, the try block is the first piece of code that gets executed. If there is an error inside the try block, the except (short for “exception”) block of code gets executed. If there is NOT an error inside the try block, program control skips the except block of code and moves on to whatever code follows that.

So the two keywords here are try and except

  • try contains the code you want to execute
  • except contains the code for handling any exception thrown during the execution of the try block.

One other cool thing we can do is to add a third block of code after the except block that gets executed regardless if an exception arises or not. The name of this block is called the finally block.

Type the following command:


Now add these lines of code right after the except block:

#!/usr/bin/env python




  print("An exception occurred.\nVariable automaticaddison is not defined.")


  print("This program has finished executing. Thank you, and have a nice day!")

Save the file and return to the terminal window. 

Run the script.


Here is the output:


That’s it for handling exceptions. If you want to do a deep dive on this topic, check out this tutorial.

Return to Table of Contents

Get Comfortable With Classes and Objects

In this section, we’ll get comfortable with classes and objects in Python. Python uses classes and objects as a way to model things that exist in the real world.

What is an Object?

The world is full of objects. Your smartphone is an object. Your motorcycle is an object. You are an object. Your wheeled robot is an object. That elephant you saw on your African safari is an object. 

Objects don’t even have to be things that you can touch. The program that you are watching on television is an object. Your favorite online video game is an object. 

Your English teacher back in grade school might have referred to objects as “nouns.” Nouns share two things in common. They all have data (things the object “knows”) and functions (things the object can “do”).

For example, your motorcycle has data (i.e. things it knows) such as:

  • Model (Honda)
  • Color (red)
  • Speed (200 miles per hour). 
  • Horsepower (200)

It has functions (i.e things it can do) such as:

  • Speed up
  • Slow down
  • Stop

In Python, software objects are modeled after objects in the real world. And like objects in the real world, objects in Python are made up of data and functions. 

In the real world, we often have multiple objects of the same type. For example, the motorcycle that you own is just one of many motorcycles that exist in the world. In the language of Python, we would say that your motorcycle is part of the motorcycle class. Let’s take a closer look at classes now.

What is a Class?

A class is a template for an object. You can think of a class as a cookie cutter, while the individual cookies made from that sheet are the objects. Classes and objects enable reuse of code in your Python programs. 

A cookie cutter

For example, imagine you own a motorcycle factory and want to program robots to automatically manufacture motorcycles. It would be really tedious and inefficient to create a brand new template for each motorcycle you want the robots to produce. 

The smarter thing to do is to create a single class (i.e. template or blueprint) called the motorcycle class. Then, when you want the robots to produce a single motorcycle (i.e. object), all you need to do is reuse the class (i.e. template). You can reuse a class (template) over and over to create as many motorcycle objects as you like. 

Each individual motorcycle (i.e. object) that comes out of your factory will have its own data values (e.g. color, horsepower,etc.) but all the motorcycles will be made from the same general motorcycle class.

Let’s create a program now that does what I’ve explained above. You own a factory and need to code your robots to manufacture motorcycles.

In the program, we will:

  • Define a class called Motorcycle
  • Create three individual motorcycles (i.e. objects)
  • Execute some of the functions of the objects

Open a new Linux terminal, and type the following commands:

cd python_for_robotics

Now type the following code. I included a lot of comments so that you know what is going on at each step in the code. Read it slowly from top to bottom, one block at a time. Take your time. There is no hurry:

#!/usr/bin/env python

# Here is where we define the class named Motorcycle. 
# It is the model (template) from which we will
# create individual motorcycles (objects)
class Motorcycle:

  #### Data ####

  # All classes have a function called __init__(), 
  # which executes each time you create a new object 
  # from the class. This function sets the initial 
  # data values.
  # All new cars will start with a speed of 0 miles 
  # per hour (mph)
  # The self parameter refers to the current object 
  # being created by the class.
  def __init__(self, model, color, speed, horsepower):
    self.model = model
    self.color = color
    self.speed = 0
    self.hp = horsepower        

  #### Functions ####
  # Function to speed up the car by a certain amount
  # The self parameter you see below is always the
  # first parameter in the function of a class.
  # It references the current object (i.e. individual
  # motorcycle). 
  # The second parameter (input) is 
  # speed_increase_in_mph which is how much we want to
  # increase a motorcycle object's speed by.
  def speed_up(self, speed_increase_in_mph):
    self.speed += speed_increase_in_mph
    print(self.model + " speed is now " + 
      str(self.speed) + " mph")

  # Function to slow down the car by a certain amount.
  # It accepts just one parameter (input), the 
  # desired reduction in speed.
  def slow_down(self, speed_decrease_in_mph):
    self.speed -= speed_decrease_in_mph

    # A car cannot go any slower than 0 mph, which is 
    # why we take the maximum value of the car's speed
    # and 0 mph.
    print(self.model + " speed is now " + 
      str(max(self.speed, 0)) + " mph")

  def stop(self):
    self.speed = 0
    print(self.model + " speed is now " + str(self.speed) + 
      " mph. Car is now stopped.")

# Here is where we define the main function of the program.
def main():

   # The syntax below shows how to create an object of a class.
   # Using the Motorcycle class as a template, we will build 
   # three individual motorcycles. 
   # Each one will have a certain model name, color, speed, and 
   # horsepower that we need to specify. 
   motorcycle_1 = Motorcycle("motorcycle_1", "red", 0, 183)
   motorcycle_2 = Motorcycle("motorcycle_2", "white", 0, 200)
   motorcycle_3 = Motorcycle("motorcycle_3", "blue", 0, 118)

   # The syntax below shows how to call a function from an object.
   # Now that the motorcycles are built, we drive them out of the 
   # factory at 60 mph, 100 mph, and 45mph, for motorcycles, 1, 2, 
   # and 3, respectively.

   # Slow down motorcycle 2 by 10 mph

   # Stop all motorcycles

# Run the main function.  
# Code execution starts (and ends) here.

Save the script and return to the Linux terminal. 

Run the script you just developed.


Here is the output.


Whew! We’re finally done with objects and classes. If you want a good reference on objects and classes, take a look at this tutorial here.

Return to Table of Contents

Working With Files: Create, Write, Close, Append, and Read

When you build a robotics project, you will often have to work with files. For example, imagine an underwater robot that needs to take periodic measurements of the water temperature. You might want to record all the information from the temperature sensor inside a text file. 

In this section, I will show you how to create, write, close, append, and read from files.

Return to Table of Contents

Create a New File

Open a new Linux terminal, and type the following commands:

cd python_for_robotics

Type in the following code. This program will create a new text file called my_file.txt.

#!/usr/bin/env python

# The open command opens a new file.
# The parameter 'w' indicates that you want 
# to create a new file.
# The newly created file is stored inside a 
# variable named 'f'
f = open("myfile.txt", "w")

Save this program and return to the terminal. Run


Type the following command to list all the files in the current directory.


You should see my_file.txt.

Return to Table of Contents

Write to a File and Close

Now, open up your program again.


Underneath the previous code you wrote, write the following Python code.

#!/usr/bin/env python

# The open command opens a new file.
# The parameter 'w' indicates that you want 
# to create a new file.
# The newly created file is stored inside a 
# variable named 'f'
f = open("myfile.txt", "w")

# We write this text to the text file
f.write(" loves robotics.")

# Close the text file

Save this program and return to the terminal. 



Now type the following command to open the text file.

gedit myfile.txt

Here is what you should see.


Return to Table of Contents

Append Data to a File and Read

How do we add more text to the end of the text file? We’ll do that now.

Add the following lines of code to your program.

#!/usr/bin/env python

# The open command opens a new file.
# The parameter 'w' indicates that you want 
# to create a new file.
# The newly created file is stored inside a 
# variable named 'f'
f = open("myfile.txt", "w")

# We write this text to the text file
f.write(" loves robotics.")

# Close the text file

# Append (i.e. 'a') text to the existing text file
f = open("myfile.txt", "a")

# Here is the text we are appending.
# The '\n' symbol means to put the text on a new line.
f.write("\nNow we have some more text in this file!")

# Open and read (i.e. 'r') the file
f = open("myfile.txt", "r")

Save the file and return to the terminal.



Here is the output:


Now type the following command to open the text file.

gedit myfile.txt

Here is what you should see.


If you want an excellent reference on files, check out this tutorial at w3 schools.

Return to Table of Contents

Working With Modules

You have finally made it to the last section of this tutorial. We’re almost at the end here! Congrats on making it this far.

In this section, we will explore modules in Python. 

What is a module? A module is a file that contains functions that you would like to include in your application.

Let’s write a module. This module will be used by a chat robot that will greet visitors who come to

Open a new Linux terminal, and type the following commands:

cd python_for_robotics

Type in the following code to create your module. This module will have only one function, but it could have many more.

#!/usr/bin/env python

def greet_website_visitors(name):
  print("Welcome to, " + name)

Save the file and return to the terminal.

Now open up a new file.


Type the following code:

#!/usr/bin/env python

import my_module

# This line of code calls the 
# greet_website_visitors() function that is inside of

Save the file and return to the terminal.



Here is the output:


That’s it! If you’re interested in a really good reference about modules, check out this post.


Congratulations on reaching the end of this post! What you have just learned is really important because Python is one of the most popular programming languages in robotics. We have covered the most important concepts of Python that you will use again and again for your career. 

If you want an excellent Python reference, I recommend you check out the Python tutorial. I have it bookmarked. It covers the fundamentals I’ve walked through above plus some extra stuff that I left out.

If you want to try out your new Python skills on a robot in simulation, check out either of these posts:

Otherwise, if you would like to go ahead and build and program a real physical robot using ROS, check out this post:

Keep building! 

Return to Table of Contents

How Do Neural Networks Make Predictions?

Neural networks are the workhorses of the rapidly growing field known as deep learning. Neural networks are used for all sorts of applications where a prediction of some sort is desired. Here are some examples:

  • Predicting the type of objects in an image or video
  • Sales forecasting
  • Speech recognition
  • Medical diagnosis
  • Risk management
  • and countless other applications… 

In this post, I will explain how neural networks make those predictions by boiling these structures down to their fundamental parts and then building up from there.

You Will Need 

Create Your First Neural Network

Imagine you run a business that provides short online courses for working professionals. Some of your courses are free, but your best courses require the students to pay for a subscription. 

You want to create a neural network to predict if a free student is likely to upgrade to a paid subscription. Let’s create the most basic neural network you can make.


OK, so there is our neural network. To implement this neural network on a computer, we need to translate this diagram into a software program. Let’s do that now using Python, the most popular language for machine learning.

# Declare a variable named weight and 
# initiate it with a value
weight = 0.075

# Create a method called neural_network that 
# takes as inputs, the input data (number of 
free courses a student has taken during the 
# last 30 days) and the weight of the connection. 
# The method returns the prediction.

def neural_network(input, weight):

  # The input data multiplied by the weight 
  # equals the prediction
  prediction = input * weight

  # This is the output
  return prediction

So we currently have five students, all of whom are free students. The number of free courses these users have taken during the last 30 days is 12, 3, 5, 6, and 8. Let’s code this in Python as a list.

number_of_free_courses_taken = [12, 3, 5, 6, 8]

Let’s make a prediction for the first student, the one who has taken 12 free courses over the last 30 days.


Now let’s put the diagram into code.

# Extract the first value of the list...12...
# and store into a variable named input
first_student_input = number_of_free_courses_taken[0]

# Call the neural_network method and store the 
# prediction result into a variable
first_student_prediction = neural_network(
                         first_student_input, weight)

# Print the prediction

OK. We have finished the code. Let’s see how it looks all together.

weight = 0.075

def neural_network(input, weight):

  prediction = input * weight

  return prediction

number_of_free_courses_taken = [
                        12, 3, 5, 6, 8]

first_student_input = number_of_free_courses_taken[0]

first_student_prediction = neural_network(
                       first_student_input, weight)


Open a Jupyter Notebook and run the code above, or run the code inside your favorite Python IDE.

Here is what I got:


What did you get? Did you get 0.9? If so, congratulations!

Let’s see what is happening when we run our code. We called the neural_network method. The first operation performed inside that method is to multiply the input by the weight and return the result. In this case, the input is 12, and the weight is 0.075. The result is 0.9.


0.9 is stored in the first_student_prediction variable.


And this, my friend, is the most basic building block of a neural network. A neural network in its simplest form consists of one or more weights which you can multiply by input data to make a prediction

Let’s take a look at some questions you might have at this stage.

What kind of input data can go into a neural network?

Real numbers that can be measured or calculated somewhere in the real world. Yesterday’s high temperature, a medical patient’s blood pressure reading, previous year’s rainfall, or average annual rainfall are all valid inputs into a neural network. Negative numbers are totally acceptable as well.

A good rule of thumb is, if you can quantify it, you can use it as an input into a neural network. It is best to use input data into a neural network that you think will be relevant for making the prediction you desire.

For example, if you are trying to create a neural network to predict if a patient has breast cancer or not, how many fingers a person has probably not going to be all that relevant. However, how many days per month a patient exercises is likely to be a relevant piece of input data that you would want to feed into your neural network.

What does a neural network predict?

A neural network outputs some real number. In some neural network implementations, we can do some fancy mathematics to limit the output to some real number between 0 and 1. Why would we want to do that? Well in some applications we might want to output probabilities. Let me explain.

Suppose you want to predict the probability that tomorrow will be sunny. The input into a neural network to make such a prediction could be today’s high temperature. 


If the output is some number like 0.30, we can interpret this as a 30% change of the weather being sunny tomorrow given today’s high temperature. Pretty cool huh!

We don’t have to limit the output to between 0 and 1. For example, let’s say we have a neural network designed to predict the price of a house given the house’s area in square feet. Such a network might tell us, “given the house’s area in square feet, the predicted price of the house is $432,000.”

What happens if the neural network’s predictions are incorrect?

The neural network will adjust its weights so that the next time it makes a more accurate prediction. Recall that the weights are multiplied by the input values to make a prediction.

What is a neural network really learning?

A neural network is learning the best possible set of weights. “Best” in the context of neural networks means the weights that minimize the prediction error.

Remember, the core math operation in a neural network is multiplication, where the simplest neural network is:

Input Value * Weight = Prediction

How does the neural network find the best set of weights?

Short answer: Trial and error

Long answer: A neural network starts out with random numbers for weights. It then takes in a single input data point, makes a prediction, and then sees if its prediction was either too high or too low. The neural network then adjusts its weight(s) accordingly so that the next time it sees the same input data point, it makes a more accurate prediction.

Once the weights are adjusted, the neural network is fed the next data point, and so on. A neural network gets better and better each time it makes a prediction. It “learns” from its mistakes one data point at a time.

Do you notice something here?

Standard neural networks have no memory. They are fed an input data point, make a prediction, see how close the prediction was to reality, adjust the weights accordingly, and then move on to the next data point. At each step of the learning process of a neural network, it has no memory of the most recent prediction it made.

Standard neural networks focus on one input data point at a time. For example, in our subscriber prediction neural network we built earlier in this tutorial, if we feed our neural network number_of_free_courses_taken[1], it will have no clue what it predicted when number_of_free_courses_taken[0] was the input value.

There are some networks that have short term memories. These are called Long short-term memory networks (LSTM).

How to Do Histogram Matching Using OpenCV

In this tutorial, you will learn how to do histogram matching using OpenCV. Histogram matching (also known as histogram specification), is the transformation of an image so that its histogram matches the histogram of an image of your choice (we’ll call this image of your choice the “reference image”).

For example, consider this image below.


We want the image above to match the histogram of the reference image below.


After performing histogram matching, the output image needs to look like this:


Then, to make things interesting, we want to use this mask to mask the output image.

Masked output image

You Will Need


Below is the source code for the program that makes everything happen. Make sure you copy and paste this code into a single Python file (mine is named Then put that file, as well as your source, reference, and mask images all in the same directory (or folder) in your computer. Once you have done that, run the code using the following command (note: mask image is optional):

python <source_image> <ref_image> [<mask_image>]

For example (put this command all on one line):

python aspens_in_fall.jpg forest_resized.jpg mask.jpg

Source Code

#!/usr/bin/env python

Welcome to the Histogram Matching Program!

Given a source image and a reference image, this program
returns a modified version of the source image that matches
the histogram of the reference image.

Image Requirements:
  - Source image must be color.
  - Reference image must be color.
  - The sizes of the source image and reference image do not
    have to be the same.
  - The program supports an optional third image (mask) as
    an argument.
  - When the mask image is provided, it will be rescaled to
    be the same size as the source image, and the resulting
    matched image will be masked by the mask image.

  python <source_image> <ref_image> [<mask_image>]

# Python 2/3 compatibility
from __future__ import print_function

import cv2 # Import the OpenCV library
import numpy as np # Import Numpy library
import matplotlib.pyplot as plt # Import matplotlib functionality
import sys # Enables the passing of arguments

# Project: Histogram Matching Using OpenCV
# Author: Addison Sears-Collins
# Date created: 9/27/2019
# Python version: 3.7

# Define the file name of the images
SOURCE_IMAGE = "aspens_in_fall.jpg"
REFERENCE_IMAGE = "forest_resized.jpg"
MASK_IMAGE = "mask.jpg"
OUTPUT_IMAGE = "aspens_in_fall_forest_output"
OUTPUT_MASKED_IMAGE = "aspens_in_fall_forest_output_masked.jpg"

def calculate_cdf(histogram):
    This method calculates the cumulative distribution function
    :param array histogram: The values of the histogram
    :return: normalized_cdf: The normalized cumulative distribution function
    :rtype: array
    # Get the cumulative sum of the elements
    cdf = histogram.cumsum()

    # Normalize the cdf
    normalized_cdf = cdf / float(cdf.max())

    return normalized_cdf

def calculate_lookup(src_cdf, ref_cdf):
    This method creates the lookup table
    :param array src_cdf: The cdf for the source image
    :param array ref_cdf: The cdf for the reference image
    :return: lookup_table: The lookup table
    :rtype: array
    lookup_table = np.zeros(256)
    lookup_val = 0
    for src_pixel_val in range(len(src_cdf)):
        for ref_pixel_val in range(len(ref_cdf)):
            if ref_cdf[ref_pixel_val] >= src_cdf[src_pixel_val]:
                lookup_val = ref_pixel_val
        lookup_table[src_pixel_val] = lookup_val
    return lookup_table

def match_histograms(src_image, ref_image):
    This method matches the source image histogram to the
    reference signal
    :param image src_image: The original source image
    :param image  ref_image: The reference image
    :return: image_after_matching
    :rtype: image (array)
    # Split the images into the different color channels
    # b means blue, g means green and r means red
    src_b, src_g, src_r = cv2.split(src_image)
    ref_b, ref_g, ref_r = cv2.split(ref_image)

    # Compute the b, g, and r histograms separately
    # The flatten() Numpy method returns a copy of the array c
    # collapsed into one dimension.
    src_hist_blue, bin_0 = np.histogram(src_b.flatten(), 256, [0,256])
    src_hist_green, bin_1 = np.histogram(src_g.flatten(), 256, [0,256])
    src_hist_red, bin_2 = np.histogram(src_r.flatten(), 256, [0,256])    
    ref_hist_blue, bin_3 = np.histogram(ref_b.flatten(), 256, [0,256])    
    ref_hist_green, bin_4 = np.histogram(ref_g.flatten(), 256, [0,256])
    ref_hist_red, bin_5 = np.histogram(ref_r.flatten(), 256, [0,256])

    # Compute the normalized cdf for the source and reference image
    src_cdf_blue = calculate_cdf(src_hist_blue)
    src_cdf_green = calculate_cdf(src_hist_green)
    src_cdf_red = calculate_cdf(src_hist_red)
    ref_cdf_blue = calculate_cdf(ref_hist_blue)
    ref_cdf_green = calculate_cdf(ref_hist_green)
    ref_cdf_red = calculate_cdf(ref_hist_red)

    # Make a separate lookup table for each color
    blue_lookup_table = calculate_lookup(src_cdf_blue, ref_cdf_blue)
    green_lookup_table = calculate_lookup(src_cdf_green, ref_cdf_green)
    red_lookup_table = calculate_lookup(src_cdf_red, ref_cdf_red)

    # Use the lookup function to transform the colors of the original
    # source image
    blue_after_transform = cv2.LUT(src_b, blue_lookup_table)
    green_after_transform = cv2.LUT(src_g, green_lookup_table)
    red_after_transform = cv2.LUT(src_r, red_lookup_table)

    # Put the image back together
    image_after_matching = cv2.merge([
        blue_after_transform, green_after_transform, red_after_transform])
    image_after_matching = cv2.convertScaleAbs(image_after_matching)

    return image_after_matching

def mask_image(image, mask):
    This method overlays a mask on top of an image
    :param image image: The color image that you want to mask
    :param image mask: The mask
    :return: masked_image
    :rtype: image (array)

    # Split the colors into the different color channels
    blue_color, green_color, red_color = cv2.split(image)

    # Resize the mask to be the same size as the source image
    resized_mask = cv2.resize(
        mask, (image.shape[1], image.shape[0]), cv2.INTER_NEAREST)

    # Normalize the mask
    normalized_resized_mask = resized_mask / float(255)

    # Scale the color values
    blue_color = blue_color * normalized_resized_mask
    blue_color = blue_color.astype(int)
    green_color = green_color * normalized_resized_mask
    green_color = green_color.astype(int)
    red_color = red_color * normalized_resized_mask
    red_color = red_color.astype(int)

    # Put the image back together again
    merged_image = cv2.merge([blue_color, green_color, red_color])
    masked_image = cv2.convertScaleAbs(merged_image)
    return masked_image

def main():
    Main method of the program.
    start_the_program = input("Press ENTER to perform histogram matching...") 

    # A flag to indicate if the mask image was provided or not by the user
    mask_provided = False

    # Pull system arguments
        image_src_name = sys.argv[1]
        image_ref_name = sys.argv[2]
        image_src_name = SOURCE_IMAGE
        image_ref_name = REFERENCE_IMAGE

        image_mask_name = sys.argv[3]
        mask_provided = True
        print("\nNote: A mask was not provided.\n")

    # Load the images and store them into a variable
    image_src = cv2.imread(cv2.samples.findFile(image_src_name))
    image_ref = cv2.imread(cv2.samples.findFile(image_ref_name))

    image_mask = None
    if mask_provided:
        image_mask = cv2.imread(cv2.samples.findFile(image_mask_name))

    # Check if the images loaded properly
    if image_src is None:
        print('Failed to load source image file:', image_src_name)
    elif image_ref is None:
        print('Failed to load reference image file:', image_ref_name)
        # Do nothing

    # Convert the image mask to grayscale
    if mask_provided:
        image_mask = cv2.cvtColor(image_mask, cv2.COLOR_BGR2GRAY)
    # Calculate the matched image
    output_image = match_histograms(image_src, image_ref)

    # Mask the matched image
    if mask_provided:
        output_masked = mask_image(output_image, image_mask)

    # Save the output images
    cv2.imwrite(OUTPUT_IMAGE, output_image)
    if mask_provided:
        cv2.imwrite(OUTPUT_MASKED_IMAGE, output_masked)
    ## Display images, used for debugging
    cv2.imshow('Source Image', image_src)
    cv2.imshow('Reference Image', image_ref)
    cv2.imshow('Output Image', output_image)
    if mask_provided:
        cv2.imshow('Mask', image_mask)
        cv2.imshow('Output Image (Masked)', output_masked)

    cv2.waitKey(0) # Wait for a keyboard event

if __name__ == '__main__':

Sample Output