Intro to Programming and Data Analytics with Python


To program our Python, we're going to use something called iPython Notebook. This makes running Python really easy, and builds Python analyses directly into data analysis reports. However, before we look at this, we'll quickly look at how you run Python 'normally'.


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

import math
radius = 10
answer = 2 * math.pi * radius
print ("The result is ", str(answer))

In most cases with scripting languages, 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).

To interpret and run the file we can use an Integrated Development Environment (IDEs), or run the interpreter from the "Command Prompt" (or "Terminal Emulator"). Here we'll do the latter. We'll come back to IDEs at the end.


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

Windows

1) Open up a text editor: right-click the windows icon on the taskbar, click run, type notepad.

2) Type the following into a blank page:

print ("Hello World")

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

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:

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). Go into TextEdit's Preferences and click Plain Text (rather than Rich Text), and unclick the Smart Quotes and Smart Dashes options. Make sure you make a new page after doing 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
  4. Debugging <-- next
  5. iPython Notebook
  6. Data Analysis