Learning Python 3 with the Linkbot/Intro

First things first
The Advanced Python 3 lessons before you are geared toward furthering your knowledge and abilities with programming using Python. The tutorials are designed to allow you to interact with the Linkbot robots by programming them to move in very specific ways. By seeing the Linkbot respond to your programming commands via code that you write, your abilities to use Python coding will motivate you to learn even more. First, you will just type in the code that is displayed in the tutorial to see how the Linkbot responds. Then, you will make your own changes to that code to play around with the Linkbot's abilities. The worst thing that can happen is that the program won't work and nothing will happen with the robot, so feel free to play with the code. When you are expected to type in code, it will look like this:

The reason that it will be formatted this way is to make code you need to type easy to distinguish from other text. Additionally, the code will be in color with different parts in different colors to help you see the code's distinct parts. When you enter code, it will not necessarily be in color, but it won't matter as long as you type it the same way that it is written here.

If the computer prints something out it will be formatted like this:

Hello, World!

(Note that printed text goes to your screen, and does not involve paper. Before computers had screens, the output of computer programs would be printed on paper.)

Please notice that these Advanced Python lessons are set up for Python 3, which means that most of the examples will not work in Python 2.7 and previous versions. Additionally, some of the extra lessons created by other programmers like you may not have been converted to Python 3. However, the differences between one version of Python and another are not really large, so if you learn how to code in one version, you should be able to read programs written for the other version without too much difficulty. At some point, you might want to look at the Non-Programmer's Tutorial for Python 2.6.

There will often be a mixture of the text you type (which is shown in bold) and the text the program prints on the screen, which would look like this:

Halt! Who Goes there? Linkbot You may pass, Linkbot

These lessons will introduce you to the terminology or vocabulary of programming. For example, programming is also often called coding or hacking. By learning the special vocabulary of programming, you will be able to understand what programmers are talking about and sound like a programmer yourself.

Finally, it's very important for your success with these lessons that you have Python 3 software. If you don't already have the Python software, go to http://www.python.org/download/ and get the Python 3 version for your computer platform. Most likely if you are learning in a classroom, your teacher will already have done this for you beforehand. If not, download it, read the instructions, and install the program.

Installing Python and the Linkbot Control Module (PyBarobo)
For Python programming, you need a working Python installation and a text editor. Python comes with its own editor IDLE, which is quite nice and sufficient for the beginning programmer. As you get more into programming, you will probably switch to some other editor like emacs, vi or another editor.

The Python download page is: http://www.python.org/download. The most recent version is Python 3.4.2 (as of 18th October 2014); Python 2.7 and older versions will not work with this tutorial. There are various different installation files for different computer platforms available on the download site. Here are some specific instructions for the most common operating systems:

Ubuntu Linux Users
To install everything you need to run this curriculum, which includes Python 3, Python 3 setup tools, and the Barobo Python package, make sure you have an internet connection and run these commands in a terminal window: sudo apt-get install python3 python3-setuptools python3-numpy idle3 sudo easy_install3 pybarobo

You will also have to add your user account to the "dialout" group. You can do this by using the command: sudo usermod -a -G dialout $USER The above command will add whatever user you are logged in as to the "dialout" group. If you wish to add a user account that is not the one that is logged in, replace "$USER" with the user name of the other account. You may have to log out and log back in for the change to take effect.

For other distributions of Linux, follow the instructions provided by either your distribution or on the Python website to get Python 3, NumPy, and PyBarobo installed.

Raspberry Pi (Raspbian) Users
Raspbian comes with Python 3 and numpy installed by default. To get the Raspberry Pi ready to control the Linkbot, make sure your Pi is connected to the internet and type the following commands: sudo usermod -a -G dialout $USER sudo apt-get install python3-setuptools sudo easy_install3 pybarobo You may have to log out and log back in again for the changes to take effect.

Mac users
Starting from Mac OS X (Tiger), Python ships by default with the operating system, but you will need to update to Python 3 until OS X starts including Python 3 (check the version by starting python3 in a command line terminal). Also IDLE (the Python editor) might be missing in the standard installation. If you want to (re-)install Python, get the MacOS installer from the Python download site.

Windows users
Download the appropriate Windows installer (the x86 MSI installer, if you do not have a 64-bit AMD or Intel chip). Even if you do have a 64-bit chip, I would still recommend downloading the 32-bit version because some of the Barobo Linkbot libraries are pre-built for 32-bit system by default. Start the installer by double-clicking it and follow the prompts. Be sure to include the optional package "pip", which can come in handy for installing additional Python modules and add-ons.

Next, you will need to install a driver, which is available here: Installer. Follow the link, download the file and run it. Once Python is installed and the path is configured, open a windows command prompt and type the following command: pip install pybarobo

See https://docs.python.org/3/using/windows.html#installing-python for more information.

Configuring your PATH environment variable
The PATH environment variable is a list of folders, separated by semicolons, in which Windows will look for a program whenever you try to execute one by typing its name at a Command Prompt. You can see the current value of your PATH by typing this command at a Command Prompt:

echo %PATH%

The easiest way to permanently change environment variables is to bring up the built-in environment variable editor in Windows. How you get to this editor is slightly different on different versions of Windows.

On Windows 8: Press the Windows key and type Control Panel to locate the Windows Control Panel. Once you've opened the Control Panel, select View by: Large Icons, then click on System. In the window that pops up, click the Advanced System Settings link, then click the Environment Variables... button.

On Windows 7 or Vista: Click the Start button in the lower-left corner of the screen, move your mouse over Computer, right-click, and select Properties from the pop-up menu. Click the <tt>Advanced System Settings</tt> link, then click the <tt>Environment Variables...</tt> button.

On Windows XP: Right-click the <tt>My Computer</tt> icon on your desktop and select <tt>Properties</tt>. Select the <tt>Advanced</tt> tab, then click the <tt>Environment Variables...</tt> button.

Once you've brought up the environment variable editor, you'll do the same thing regardless of which version of Windows you're running. Under <tt>System Variables</tt> in the bottom half of the editor, find a variable called <tt>PATH</tt>. If there is one, select it and click <tt>Edit...</tt>. Assuming your Python root is C:\Python34, add these two folders to your path (and make sure you get the semicolons right; there should be a semicolon between each folder in the list):

<tt>C:\Python34 C:\Python34\Scripts</tt>

Note: If you want to double-click and start your Python programs from a Windows folder and not have the console window disappear, you can add the following code to the bottom of each script:

<tt> </tt>

Interactive Mode
Go into IDLE (also called the Python GUI). You should see a window that has some text like this: Python 3.0 (r30:67503, Dec 29 2008, 21:31:07) [GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2 Type "copyright", "credits" or "license" for more information.

****************************************************************   Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 3.0 >>> The  is Python's way of telling you that you are in interactive mode. In interactive mode what you type is immediately run. Try typing  in. Python will respond with. Interactive mode allows you to test out and see what Python will do. If you ever feel you need to play with new Python statements, go into interactive mode and try them out.

Creating and Running Programs
Go into IDLE if you are not already. In the menu at the top, select  then. In the new window that appears, type the following:

Now save the program: select  from the menu, then. Save it as " " (you can save it in any folder you want). Now that it is saved it can be run. Just a Rookies note here, if you change a program, save it with a version notation like  that way you won't change your original program that worked.

Next run the program by going to  then   (or if you have an older version of IDLE use   then  ). This will output  on the   window.

For a more in-depth introduction to IDLE, a longer tutorial with screenshots can be found at http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html

Program file names
It is very useful to stick to some rules regarding the file names of Python programs. Otherwise some things might go wrong unexpectedly. These don't matter as much for programs, but you can have weird problems if you don't follow them for module names (modules will be discussed later).
 * 1) Always save the program with the extension <tt>.py</tt>. Do not put another dot anywhere else in the file name.
 * 2) Only use standard characters for file names: letters, numbers, dash (<tt>-</tt>) and underscore (<tt>_</tt>).
 * 3) White space ("<tt> </tt>") should not be used at all (use underscores instead).
 * 4) Do not use anything other than a letter (particularly no numbers!) at the beginning of a file name.
 * 5) Do not use "non-english" characters (such as <tt>ä</tt>, <tt>ö</tt>, <tt>ü</tt>, <tt>å</tt> or <tt>ß</tt>) in your file names—or, even better, do not use them at all when programming.

Using Python from the command line
If you don't want to use Python from the command line, you don't have to, just use IDLE. To get into interactive mode just type  without any arguments. To run a program, create it with a text editor (Emacs has a good Python mode) and then run it with.

Additionally, to use Python within Vim, you may want to visit Python wiki page about VIM

Running Python Programs in *nix
If you are using Unix (such as Linux, Mac OS X, or BSD), if you make the program executable with chmod, and have as the first line: you can run the python program with  like any other command.

Where to get help
At some point in your Python career you will probably get stuck and have no clue about how to solve the problem you are supposed to work on. This tutorial only covers the basics of Python programming, but there is a lot of further information available.

Python documentation
First of all, Python is very well documented. There might even be copies of these documents on your computer, which came with your Python installation:
 * The official Python 3 Tutorial by Guido van Rossum is often a good starting point for general questions.
 * For questions about standard modules (you will learn what this is later), the Python 3 Library Reference is the place to look at.
 * If you really want to get to know something about the details of the language, the Python 3 Reference Manual is comprehensive but quite complex for beginners.

Python user community
There are a lot of other Python users out there, and usually they are nice and willing to help you. Before you ask the Python user community a question, everyone will appreciate it if you do a web search for a solution to your problem before contacting the community. This very active user community is organized mostly through mailing lists and a newsgroup:


 * The tutor mailing list is for people who want to ask questions regarding how to learn computer programming with the Python language.
 * The python-help mailing list is python.org's help desk. You can ask a group of knowledgeable volunteers questions about all your Python problems.
 * The Python newsgroup [news:comp.lang.python comp.lang.python] (Google groups archive) is the place for general Python discussions, questions, and the central meeting point of the community.
 * Python wiki has a list of local user groups, you can join the group mailing list and ask questions. You can also participate in the user group meetings.