Non-Programmer's Tutorial for Python 3/Intro

First things firstEdit

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.)

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 PythonEdit

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.3; 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:

Linux, BSD, and Unix usersEdit

You are probably lucky and Python is already installed on your machine. To test it type python3 on a command line. If you see something like that in the following section, you are set.

If you have to install Python, first try to use the operating system's package manager or go to the repository where your packages are available and get Python 3. Python 3.0 was released in December 2008; all the distributions should have Python 3 available, so you may not need to compile Python 3 from scratch after downloading the source code. Ubuntu and Fedora do have Python 3 binary packages available, but they are not yet the default, so they need to be installed specially.

Roughly, here are the steps to compile Python in UNIX (If these totally don't make sense, you may want to read another introduction to UNIX, such as Introduction to Linux):

$ tar -xvzf ~/Download/Python-3.2.tgz
... list of files as they are uncompressed 
  • Switch to the directory and tell the computer to compile and install the program
$ cd Python-3.2/
$ ./configure --prefix=$HOME/python3_install 
 ... lots of output.  Watch for error messages here ... 
$ make
 ... even more output.  Hopefully no error messages ... 
$ make install
  • Add python 3.0 to your path. You can test it first by specifying the full path. You should add $HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3
Python 3.2 (r32:88445, Mar 26 2011, 13:29:57) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

The above commands will install Python 3 to your home directory, which is probably what you want, but if you skip the --prefix, it will install it to /usr/local If you want to use IDLE, you need to make sure that tk and tcl and their development files are installed on the system. You will get a warning during the make phase if these are not available.

Mac usersEdit

Starting from Mac OS X (Tiger), Python ships by default with the operating system, but you will need to update to Python 3 as in the Unix instructions 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, have a look at the Mac page on the Python download site.

Windows usersEdit

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.


see http://docs.python.org/3.3/using/windows.html#

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 search for the Windows control panels. Once you've selected 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, click Edit.... Assuming your Python root is C:\Python33, add these two directories to your path (and make sure you get the semicolons right; there should be a semicolon between each folder in the list):

C:\Python33 C:\Python33\Scripts

note: If you want to double click and start your Python programs from a windows directory 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")

Interactive ModeEdit

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 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 ProgramsEdit

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

Program file namesEdit

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 .py. Do not put another dot anywhere else in the file name.
  2. Only use standard characters for file names: letters, numbers, dash (-) and underscore (_).
  3. White space (" ") 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 ä, ö, ü, å or ß) in your file names—or, even better, do not use them at all when programming.

Using Python from the command lineEdit

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 python3 program_name.

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

Running Python Programs in *nixEdit

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:

#!/usr/bin/env python3

you can run the python program with ./hello.py like any other command.

Where to get helpEdit

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 documentationEdit

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 communityEdit

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.

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!

← Front matter Intro Hello, World →
Last modified on 2 March 2014, at 10:05