Learning Python 3 with the Linkbot/Intro
- 1 First things first
- 2 Installing Python and the Linkbot Control Module (PyBarobo)
- 3 Interactive Mode
- 4 Creating and Running Programs
- 5 Using Python from the command line
- 6 Where to get help
First things first
So, you've never programmed before. As we go through this tutorial, I will attempt to teach you how to program. There really is only one way to learn to program. You must read code and write code (as computer programs are often called). I'm going to show you lots of code. You should type in code that I show you to see what happens. Play around with it and make changes. The worst that can happen is that it won't work. When I type in code it will be formatted like this:
##Python is easy to learn print("Hello, World!")
That's so it is easy to distinguish from the other text. If you're reading this on the web, you'll notice the code is in color -- that's just to make it stand out, and to make the different parts of the code stand out from each other. The code you enter will probably not be colored, or the colors may be different, but it won't affect the code as long as you enter it the same way as it's printed here.
If the computer prints something out it will be formatted like this:
(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.)
Note that this is a Python 3 tutorial, which means that most of the examples will not work in Python 2.7 and before. As well, some of the extra libraries (third-party libraries) have not yet been converted. You may want to consider learning from the Non-Programmer's Tutorial for Python 2.6. However, the differences between versions are not particularly large, so if you learn one, you should be able to read programs written for the other without much difficulty.
There will often be a mixture of the text you type (which is shown in bold) and the text the program prints to the screen, which would look like this:
Halt! Who Goes there? Josh You may pass, Josh
(Some of the tutorial has not been converted to this format. Since this is a wiki, you can convert it when you find it.)
I will also introduce you to the terminology of programming - for example, that programming is often referred to as coding or hacking. This will not only help you understand what programmers are talking about, but also help the learning process.
Now, on to more important things. In order to program in Python you need the Python 3 software. If you don't already have the Python software go to http://www.python.org/download/ and get the proper version for your platform. Download it, read the instructions and get it installed.
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 totally sufficient for the beginning. As you get more into programming, you will probably switch to some other editor like emacs, vi or another.
The Python download page is: http://www.python.org/download. The most recent version is Python 3.4.1 (as at 19th May 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 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.
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.
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.
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.
The latest Python 3.4.1 installer can help set your path for you. If you are installing Python 3.4.1 on Windows, select Install for all users. There will be an installation step called Customize Python 3.4.1 . During this step, if you scroll down to the last entry of customizable options, you will see an option that reads Add python.exe to Path. By default, this setting is set to Entire feature will be unavailable. Change the install setting to Will be installed on the local hard drive to automatically add Python to your path. Once this is done, you will no longer need to follow the instructions in the following section titled "Configuring your PATH environment variable"
Once Python is installed and the path is configured, open a 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:
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 Advanced System Settings link, then click the Environment Variables... button.
On Windows XP: Right-click the My Computer icon on your desktop and select Properties. Select the Advanced tab, then click the Environment Variables... 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 System Variables in the bottom half of the editor, find a variable called PATH. If there is is one, select it and click Edit.... 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):
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:
print("Hello World") #stops console from exiting end_prog = "" while end_prog != "q": end_prog = input("type q to quit")
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 >>>
>>> is Python's way of telling you that you are in interactive mode. In interactive mode what you type is immediately run. Try typing
1+1 in. Python will respond with
2. 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
New Window. In the new window that appears, type the following:
Now save the program: select
File from the menu, then
Save. Save it as "
hello.py" (you can save it in any folder you want). Now that it is saved it can be run.
Next run the program by going to
Run Module (or if you have an older version of IDLE use
Run script). This will output
Hello, World! on the
*Python Shell* 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).
- Always save the program with the extension .py. Do not put another dot anywhere else in the file name.
- Only use standard characters for file names: letters, numbers, dash (-) and underscore (_).
- White space (" ") should not be used at all (use underscores instead).
- Do not use anything other than a letter (particularly no numbers!) at the beginning of a file name.
- Do not use "non-english" characters (such as ä, ö, ü, å or ß) 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
python3 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
./hello.py 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.
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
In order not to reinvent the wheel and discuss the same questions again and again, people will appreciate very much if you do a web search for a solution to your problem before contacting these lists!
There are a lot of other Python users out there, and usually they are nice and willing to help you. This very active user community is organised mostly through mailing lists and a newsgroup:
- The tutor mailing list is for folks 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 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.