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. 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 format like this:
##Python is easy to learn print "Hello, World!"
Thats so it is easy to distinguish from the other text. To make it confusing I will also print what the computer outputs in that same font.
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.
First you should check to see if you can run Python in interactive mode. To do this go to a command prompt and type python. If everything is working you should see something like this:
Python 1.5.1 (#1, Dec 17 1998, 20:58:15) [GCC 2.7.2.3] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>The »> is Python's way of telling you it is waiting for you to type something in.
If you did not get the »> prompt then something is wrong. Check and make sure that you properly installed Python. Check to see that the python executable is in the path.
Once you got the Python interpreter running you can play around with it. Here are some things you could try:
>>> 1+1 2 >>> 3*4+5 17 >>> a=12 >>> a 12 >>> "Hi" 'Hi' >>>
To exit try typing Ctrl+Z or Ctrl+D or if neither of those works type import sys; sys.exit(0)
. Next, how to run Python programs. First you need create a Python program. To do that you should type in the following in a text editor:
print "Hello, World!"
Now save the file in some convenient location as hello.py. Next go to directory with the file that you save the file in and type python hello.py. The screen should look something like this:
>python hello.py Hello, World!
If you get some error message check to make sure you saved the file in the right place.
From now on I will mainly give you programs to type in that you should save and then run.
This will open up a DOS screen with a prompt that probably looks like this:
C:\WINDOWS>
Type python and hit Enter. If you get Bad command or file name then either you forgot to install Python or python.exe is not in the PATH variable (to see what the PATH variable has in it type PATH).
To fix the problem click Start»Find»Files or Folders and then look for a file Named python.exe. If it is found then the problem is the PATH, otherwise Python was not properly installed.
To fix the PATH problem the directory that includes python.exe needs to be added to PATH. The directory that python is in is the folder that Find shows to the left of the name. If the whole name is not shown Right click on the python icon and go to Properties. The Properties dialog will have a line called Location: that shows the directory where python.exe is at. Next go to the command prompt and type in PATH="C:\Location of Python\";%PATH%
. For Example:
C:\WINDOWS>python Bad command or file name C:\WINDOWS>PATH="C:\Programe Files\Python";%PATH% C:\WINDOWS>python Python 1.5.1 (#0, Apr 13 1998, 20:22:04) [MSC 32 bit (Intel)] on win32 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>Type Ctrl+Z to get out of interactive mode (i.e. hold down the Ctrl Key and then press down on the Z key at the same time, then release both.).
Next type this program in a text editor (such as Notepad):
print "Hello, World!"and save it in a convenient location such as
C:\python\hello.py
.
From here you need to change the path to the Python directory where you saved your first program. The command to do this is (Hit Enter):
cd \pythonwhich should give you a prompt like:
C:\Python>
Now you are ready to run your first program. Just type python hello.py
>python hello.py Hello, World!
From now on in the tutorial I will assume that you know how to create and run programs.
Download py152.exe from http://www.python.org/download/download_windows.html The file is located at ftp://ftp.python.org/pub/python/win32/py152.exe Download this file and save it to your computer.
Run py152.exe by double clicking on the executable. This will start the Python installation process. Use the defaults for the install. The computer will restart after this is done.
The next step is to add the Python executable to the path. The path specifies how DOS finds commands. First you will find where the Python executable was installed. Next create a PATH command to tell DOS where to find Python. Lastly add the PATH command to the autoexec.bat so that it will be permanent. The following tells how to do this.
This is what you should do to add Python to the path. First get to an MS-DOS Prompt. The way to get to a MS-DOS Prompt is go to StartRun. When the Run dialog pops up type ``command'' and press Enter (Note: don't type the ``quotation marks'', just type the stuff between them) (Note two: type Enter after everything that I put in this ``font''). A window titled MS-DOS Prompt should appear.
Type ``cd \''. The command ``cd'' stands for change directory. When ``cd'' is followed by a ``\'' it means change to the root directory. The C:\> prompt tells you that you are in the root directory. Once in the root directory the next task is to find Python.
Type ``dir /s python.exe'' This should give you some output like:
Volume in drive C is WIN 95 Volume Serial Number is 283F-12D9 Directory of C:\Program Files\Python PYTHON EXE 5,120 04-13-99 11:31a python.exe 1 file(s) 5,120 bytes Total files listed: 1 file(s) 5,120 bytes 0 dir(s) 344,498,176 bytes freeThe important part of this is the line:
Directory of C:\Program Files\Pythonwhich shows the directory that python.exe is located in. This can be used to create a PATH variable that allows python to be run from anywhere on the system. The syntax is ``PATH=%PATH%;"directory"'' where directory is the directory that python.exe is in. For this example the command is:
PATH=%PATH%;"C:\Program Files\Python"
Type this in at the DOS Prompt and then you should be able to run Python with the ``python'' command. If you did everything right you should see (version numbers and dates may be different):
Python 1.5.2 (#4, Dec 16 1999, 18:55:39) [GCC 2.7.2.3] on Win 95 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>If you get the prompt you have gotten into Python. Try typing in it ``print "Hello"''. You should see Hello printed. Try tying ``print 1+1''. You should see 2 printed. Type ``Ctrl-Z'' (i.e. type z while holding down the Ctrl key) to get out of Python's interactive mode.
If there is some problem with the PATH then you will see something like:
C:\>python Bad command or file name
Right now the change you made is only temporary. To get Python to be in the PATH every time the computer starts you need to add it to the autoexec.bat file. The autoexec.bat file is run every time the computer starts. To do this open the autoexec.bat in a text editor like notepad. Type the command ``notepad c:\autoexec.bat'' on the DOS Prompt. If you don't have a autoexec.bat notepad will ask you if you want to create one (say yes). Add the line that you used above (i.e. something like: ``PATH=%PATH%;"C:\Program Files\Python"'') to the autoexec.bat. Add the line as close to the bottom as you can, just don't add the line after a line that tells windows to load:
PATH=%PATH%;"C:\Program Files\Python" winThe win loads Windows so I added the path line above it. If you don't see a win line add the path line to the end. Save the autoexec.bat file, reboot your computer and it should be install.
When your computer boots again go to the DOS Prompt and try to run ``python'' again to see if the change was successful.
Now that Python is set up we need to use it to run programs. Go to a DOS Prompt (StartRun, then type ``command''). Change to the root directory (``cd \ ''). Now make a directory with the command ``mkdir prg'' which makes the directory prg. Switch into that directory with the ``cd prg'' command. Create a file there by typing the ``notepad hello.py'' command. That will open a notepad window. In Notepad type:
print "Hello, World!"Save the file and then go back to the DOS Prompt. Now type ``python hello.py'' in at the DOS Prompt. Python should reply with Hello, World! if everything is working right. If you get the error:
Bad command or file namecheck to see if the path is correct. Also check to make sure that you spelled python correctly. If you get the error:
C:\PROGRA~1\PYTHON\PYTHON.EXE: can't open file 'hello.py'check to see that you properly saved the hello.py file and that you are in the correct directory.
Once you have that working create another file with the ``notepad ask.py'' command. Put the following text in the file:
data = raw_input("Type something here:") print "You typed:",dataSave and then run with the ``python ask.py'' command. The program should ask you to type in data and after you hit Enter it will tell you what you type. You should get something like this:
Type something here:Hi You typed: HiIf you got that, congratulations, you now have a working Python environment.