{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Installing and Using Python\n", "-------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Learning Objectives

\n", "
\n", "
\n", "\n", "> - Install Python and a large range of 3rd party modules on your laptop\n", "> - Show you how to use Python on the University's Managed Desktop Machines\n", "> - Introduce you to the different ways of running Python\n", "> - A brief introduction to the Jupyter notebook\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Installing Python on your laptop\n", "--------------------------\n", "\n", "## STOP!\n", "--------\n", "Before you install Python on your own laptop, do you need to? For this course we'll be using the online tool Google colab and you can continue to use this, and work with data on your Google drive, for the rest of your degree. If you are happy to use Colab, you can skip this notebook.\n", "\n", "If you feel you really need a Python install on your own laptop, read on...\n", "\n", "## Carry on...\n", "---------------\n", "\n", "There are many ways and places to obtain Python. I strongly recommend downloading and installing the Anaconda Python distribution. As well as installing the Python language and it's standard library, the Anaconda package also installs most of the modules and libraries needed for scientific computing. \n", "\n", "Point your web-browser at the [Anaconda download page](http://continuum.io/downloads) and download the correct version for your machine. You almost certainly want the graphical installer for Python 3.X. Then follow the installation instructions for your O/S at http://docs.continuum.io/anaconda/install.html." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Three ways to run Python\n", "-----------------------------------------------------------------\n", "\n", "### 1. The IPython command-line interpreter\n", "### 2. Editing Python (.py) files\n", "### 3. The Jupyter notebook\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 1. The IPython command-line interpreter\n", "\n", "The command line interpreter is a program where you can type Python code and see the results immediately. As such, I use the interpreter a **lot**. Normally when I am writing new code I will try out the code in the interpreter as I am going along, and check the results match my expectations. I **strongly** encourage this way of working.\n", "\n", "The interpreter is launched from the command-line. This is familiar to Linux/Unix users. Perhaps not so much for those used to Mac or Windows machines. To start the command line\n", "\n", "* Mac OS/X: In Finder/Applications, search for \"Terminal\"\n", "* Linux/Unix: Ctrl-Alt-t\n", "* Windows: Start Menu -> Anaconda (64 bit) -> Anaconda Python 3\n", "\n", "\n", "\n", "Type ```ipython``` at the command-line to start the interpreter\n", "\n", "\n", "\n", "Any python commands you type into the interpreter will be executed. For example, type ```print( \"Hello World!\" )```\n", "\n", "\n", "\n", "To close the terminal, either type ```exit()``` or Ctrl-d.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Start the IPython interpreter, and get Python to print your name to the screen.\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "The interpreter is most useful when you want to do some testing of an idea, or a quick calculation. The IPython interpreter has really nice [features](http://ipython.org/ipython-doc/3/interactive/tutorial.html), like **tab-completion of code**, and exensive **help** features (type help, or enter a question mark after the name of an object). Look at the URL above for more info.\n", "\n", "However, it's not easy to re-run code you've typed into the interpreter. If you want to write complex programs to execute later, you are better off writing Python (.py) files. \n", "\n", "\n", "## 2. Editing Python (.py) files\n", "\n", "Editing Python files requires using a text editor. The best ones will highlight your code for you and make it easier to read. I recommend Visual Studio Code from Microsoft, since it works on any computer. Sage Math Cloud has it's own editor built in.\n", "\n", "Creating a Python file is easy. On your own laptop, open a text editor (like TextEdit), write in the code you want to execute, and save it with a .py extension. \n", "\n", "In the example below I've used TextWrangler on OS X to save a file called ```hello_world.py```\n", "\n", "\n", "\n", "To run the commands in the file, simply start a command-line session (see above) and type, for example, ```python hello_world.py```\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Exercise

\n", "
\n", "
\n", "\n", "> Create a file called \"hello_world.py\" containing the Python statements to print out your name. Save it and execute it using Python on your system \n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. The Jupyter notebook\n", "\n", "The Jupyter notebook (formerly known as the IPython notebook) is a really nice way of running Python. It allows you to mix Python code, text comments, image display and nicely formatted equations. These notes were themselves written in the Jupyter notebook.\n", "\n", "You can start a notebook session from the command line by typing ```jupyter notebook```. Your web browser will open to an interactive notebook page. Google colab works with notebooks by default.\n", "\n", "You should see a page a bit like the one below open in your web browser.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", "

Conclusions

\n", "
\n", "
\n", "\n", "> * We've installed Python on our own machines\n", "> * We've discussed different ways of running Python\n", "> * Coming next - a detailed look at the Jupyter Notebook" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }