Learning Python 3 with the Linkbot/Intro
- 1 First things first
- 2 Interactive Mode
- 3 Creating and Running Programs
- 4 Using Python from the command line
- 5 Where to get help
First things first
The Beginner 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:
##Python is easy to learn print("Hello, World!")
When you see code blocks like the one above, make sure you type it exactly as you see it, including capital letters and punctuation.
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 elements. 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:
(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 Beginner 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.
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 >>>
(For PyCharm Users, click on the little rectangular icon at the far lower left of the window to expand the small toolbar and select "Python Console" to start the Python Console.)
>>> 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
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
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.
Running Programs in your Integrated Development Environment (IDE)
Many IDEs, such as PyCharm, Eclipse, TextWrangler, Ninja-IDE, and IDLE, are able to run Python programs within the IDE itself. For instructions on how to do so, please consult the documentation or tutorials located on this wiki the Installation and Setup chapter here, or refer to the official documentation of the IDE.
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
The most common way to run a Python program in a Unix operating system is in a command terminal. There are several ways to achieve this. First, you can run the Python interpreter directly and tell it the name of your script. For instance, to run a python file named "hello.py", you could type:
Another method is to make the file "hello.py" executable using the command
chmod a+x hello.py
Then, on the first line of your Python program, tell the operating system that it is a python3 program with the following line:
Now, you can run the python program with
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
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 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.