Running Python from the shell

In the previous section, you used used a text editor to create a file.

Let's create a file again, and this time write some Python. Don't worry if you don't know Python yet - this will be a very simple program!

Using the text editor you used in the last section, create a new file called hello.py. If you are using nano, do this by typing

nano hello.py

Inside this file, just type one line:

print "Hello world!"

Then save the file and return to the command line. Check that your new file is there and that it contains what you expect by running

ls

and then

cat hello.py

The line we put in the file, print "Hello world!" is actually a line of Python code. If we give this command to Python, it will do what you might guess: print out the string "Hello world!". The file hello.py is now actually a one-line program.

To tell Python to run this program, on the command line enter

python hello.py

If Python is installed on the computer, this should run your program!

When you've learned how to write more advanced Python code, you can turn it into a file in exactly the same way, and use the algorithms you write to analyse your data from the shell.