Dark theme

Intro to running Python



Computer programs are generally written in a human-readable form made up of words and maths notation like this:

value = 2
answer = 4 + value
if answer > 0 :
   print (answer)

In most cases these days, you write programs in a text file, which is passed to a piece of software called an "interpreter". The interpreter changes it to instructions in a form the computer recognises, and runs the program (sometimes the translation process, "compilation", is separated out from the running, but not with Python).

We generally refer to single text files containing simple commands as a "script" or "program", also using "program" for larger sets of code in multiple text files that work together.

To interpret and run the file we can use an Integrated Development Environment (IDEs), Jupyter Notebooks, or run the interpreter from the "Command Prompt" (Windows) or "Terminal Emulator" (Macs). Here we'll do the latter. We'll come back to IDEs later. The advantage of the command prompt is that it directly runs the software at the core of everything else – if Python is working at all, it will work (whereas IDEs may have problems of their own). Learning to use the command prompt also gets you familiar with sorting out problems with your code using the minimal information.


Work through the instructions below to write and run a basic Python program.

Windows

1) Open up a text editor: hold down the 'Windows' key on the keyboard and push "r" at the same time (or right-click the windows icon on the taskbar and click run), type notepad++.

2) Type the following into a blank page:

print ("Hello World")

Note that Python is case sensitive, so you need to type it exactly as it is above, in terms of the capitalisation.

3) Save the file onto your desktop or home space with the name HelloWorld.py – make sure your text editor isn't saving it as HelloWorld.py.txt ; some editors can add ".txt" to the end of text files, whatever you'd rather have (and you don't have to stick with .txt for text files   putting .py on the end doesn't change the format of the file, just the name). This is especially problematic with Windows, which will hide file extensions, so it is worth turning this off.

4) Open up a command prompt: hold down SHIFT and right-click the Desktop (not the new file). Holding shift adds a new command to the menu: "Open command window here...". Click this – it will open a command prompt and change its directory so it is running in the Desktop directory.

5) Type the following, to pass the Python file to the Python interpreter (again, case sensitive):

python HelloWorld.py

Mac/Linux

1) Open up a text editor: hold down CTRL and SPACE together, and then search for TextEdit. Click TextEdit to open it.

2) By default TextEdit saves as "Rich Text Format" rather than standard text (which we need). See how to change this.

3) Make a new blank page, and type the following into a blank page:

print ("Hello World")

4) Save the file onto your desktop, changing the name to HelloWorld.py.

5) Open up a terminal: hold down CTRL and SPACE together, and then search for Terminal. Once open, type cd ~/Desktop and press enter to move directory to the desktop.

6) Type the following, to pass the Python file to the Python interpreter:

python HelloWorld.py


You should see the Hello World message appear. If this appears, congratulations! You're a programmer! "Hello World" is the classic starting point for all programmers; it's the simplest program that responds to show your interpreter is working.


If it didn't work, no problem: we're about to look at how to cope with problems!

  1. Start
  2. Get the software
  3. Writing our first program: running at the command line
  4. Debugging <-- next
  5. IDEs: IDLE and Spyder
  6. Jupyter Notebook