Now that we have a script written (myscript.py), the next question is how to make it run? There are many ways to do this:
To run a script from within the IDLE GUI: if the script is not open, then right-click on the myscript.py icon, and choose "Edit with IDLE". Once the script is opened in the IDLE text editor, to run it, just go Run -> Run Module (or as a shortcut, you can press F5 instead). If the script has been modified since it was last saved, IDLE will prompt you to save the script before running it. This is good practise in case IDLE crashes while trying to run the script.
Hopefully you will see that Python has printed out "Hello World!" to screen as you have told it, and is waiting for further instructions. You could just type the following into the shell to achieve the same effect:
Typing short commands into the shell directly is useful for trying things out, and we'll do some more of that later.
The second method above is self-explanatory. If Python is installed correctly under windows, double clicking on a .py file will execute the script. However, for this trivial example, Python will open a terminal window, print the message the the window and close the window immediately. This will likely happen so fast that you will not have a chance to read the message! Nevertheless, this can be a very useful method to execute more complicated scripts that take longer to run, or whose output results in a file on your hard drive for example.
The third method, running from the command-line, is useful for doing computation remotely on another computer, or for automating the simultaneous execution of several scripts on your own computer. It requires that the Python executable is in your PATH. This is a little tricky to set up under Windows, particularly on Newcastle university desktop machines so I won't go into it today.
Ok, so now you know two different ways to execute a Python script in Windows, save the script below and try to use both methods to execute it, and see what happens. This code might seem incomprehensible. It is quite complicated and difficult, but I hope that you'll be surprised at the complexity of the result given how short the text is. Most of the lines in this script are a bit obscure and are simply there to get the window popup working. We won't go into that specifically in this course, but I hope that by the end of the day you will be able to recognise several important features of this script and feel confident making a few tweaks to it here and there to see what happens. A version with some explanatory comments can be seen here
import Tkinter, numpy
nframes=100000
txtframe=20
window = Tkinter.Tk()
w,h = 750,750
txt=list("abcdefghijklmnopqrstuvwxyz0123456789")
colour=numpy.array(numpy.random.randint(0,255,3),dtype=numpy.uint8)
for i in xrange(0,nframes):
window.title("http://www.staff.ncl.ac.uk/conor.lawless/ScientificPython Frame number: "+str(i))
C=Tkinter.Canvas(window, width=w, height=h)
C.pack()
newcol= "#%02x%02x%02x" % tuple(colour)
C.create_rectangle(0,0, w, h, fill=newcol,outline=newcol)
C.create_text(w/2,h/2,text=txt[(i/txtframe)%len(txt)],fill="white",font=("Arial", "150","bold"))
C.update()
C.destroy()
colnew=colour+numpy.random.randint(-1,2,3)*2
colnew=[max(min(col,255),0) for col in colnew]
colour=numpy.array(colnew,dtype=numpy.uint8)
window.destroy()
If your (Windows) computer has the ability to play sound, you could try the simpler but equally awesome example below. A version with explanatory comments can be seen here.
import winsound
music=((784,1),(880,1),(698,1),(349,1),(523,3))
for x in music:
winsound.Beep(x[0],250*x[1])
Overview ∴ Installation ∴ First Script ∴ Execution ∴ Libraries ∴ Structure ∴ Other Resources