Until now we have just entered commands into interpreter
To write a longer program need to write a program file or script
We will look at a few ways of writing and running scripts
Note: The method of running scripts is very platform specific
Normally, you will use a text editor like Emacs, Vim, Geany, or Notepad++
Here is a demo with Geany (because it's simple)
Let's open Geany:
Now I write my script/program in Geany
And save it as test.py in my home directory, called /home/john
(Will look different on Windows/Mac)
Notice that Geany now knows its a Python file because of the .py extension
Geany applies syntax highlighting
We've now created a file containing Python commands, stored on the hard disk
The next step is to run it
One method is to run it from the command line (or shell in UNIX/Linux)
Here I'm running it on Ubuntu Linux, through the shell
Better to use the -i flag to stay in Python when it finishes
Generally, the command line method is not the best one for scientific programming
Let's look at some other ways
A very easy way to run scripts is with IDLE
After starting IDLE, the first window is the Python shell (interpreter)
Go to the file menu
Yes, I know the fonts are ugly...
Select "New Window" to get a window like this:
This window serves as a text editor where we can write our program:
Before running the file we need to save it
Select "Save As" from the "File" menu
You should get a dialogue box like this (Might look different on Windows/Mac)
Make sure you save it somewhere sensible, where you can find it again
Now that it's saved we can run it
Select "Run Module" from the "Run" menu
The output is sent to the Python Shell
After you get comfortable, switch to IPython
Videos on installing and using IPython can be found here
Here's a very quick tutorial using screenshots from my Linux machine
First we fire up IPython
The next step is to run our file test.py in /home/john
The output is displayed in IPython
The only problem you might have is that test.py is not in IPython's current working directory
Error message looks like this
Let's check what directory we are in
'pwd' stands for "present working directory"
We are in /tmp, and the file is in /home/john
So I change to /home/john
'cd' stands for "change directory"
Now it runs okay
Things will be different with Windows or Mac
Write a program which
Solution:
X = raw_input("Enter a list of integers separated by commas: ")
X = X.split(',')
X = [int(x) for x in X]
X.sort()
print X