Non-Programmer's Tutorial for Python 2.6/Intro
First things first
[edit | edit source]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:
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.)
If you try this program out and you get a syntax error, check and see what version of python you have. If you have python 3.0, you should be using the Non-Programmer's Tutorial for Python 3.0. This article was made 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 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. 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 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
[edit | edit source]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 3.1, but any Python 2.x version since 2.2 will work for this tutorial. Be careful with the upcoming Python 3, though, as some major details will change and break this tutorial's examples. A version of this tutorial for Python 3 is at Non-Programmer's Tutorial for Python 3. 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:
Linux, BSD and Unix users
[edit | edit source]You are probably lucky and Python is already installed on your machine. To test it type python on a command line. If you see something like that in the following section, you are set.
If you have to install Python, just use the operating system's package manager or go to the repository where your packages are available and get Python. Alternatively, you can compile Python from scratch after downloading the source code. If you get the source code make sure you compile in the Tk extension if you want to use IDLE.
Mac users
[edit | edit source]Starting from Mac OS X (Tiger), Python ships by default with the operating system, but you might want to update to the newer version (check the version by starting python in a command line terminal). Also IDLE (the Python editor) might be missing in the standard installation. If you want to (re-)install Python, have a look at the Mac page on the Python download site.
Windows users
[edit | edit source]Some computer manufacturers pre-install Python. To check if you already have it installed, open command prompt (cmd in run menu) or MS-DOS and type python. If it says "Bad command or file name" you will need to download the appropriate Windows installer (the normal one, if you do not have a 64-bit AMD or Intel chip). Start the installer by double-clicking it and follow the procedure. Python for windows can be downloaded from the official site of python
Interactive Mode
[edit | edit source]Go into IDLE (also called the Python GUI). You should see a window that has some text like this:
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 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 1.2.1 >>>
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 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
[edit | edit source]Go into IDLE if you are not already. In the menu at the top, select File
then New Window
. In the new window that appears, type the following:
print "Hello, World!"
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
then Run Module
(or if you have a older version of IDLE use Edit
then 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
Running Python Programs in Unix
[edit | edit source]If you are using Unix (such as Linux, Mac OSX, or BSD), if you make the program executable with chmod
, and have as the first line:
#!/usr/bin/env python2
you can run the python program with ./hello.py
like any other command.
Note: In some computer environments, you need to write:
#!/usr/bin/env python
Example for Solaris:
#!/usr/bin/python
Program file names
[edit | edit source]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 somewhere 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 (e.g. 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
[edit | edit source]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 python
without any arguments. To run a program, create it with a text editor (Emacs has a good Python mode) and then run it with python program_name
.
Additionally, to use Python within Vim, you may want to visit Using vim as a Python IDE
Where to get help
[edit | edit source]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
[edit | edit source]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 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 Library Reference is the place to look.
- If you really want to get to know something about the details of the language, the Python Reference Manual is comprehensive but quite complex for beginners.
Python user community
[edit | edit source]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.
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!