Overview

The goal of this tutorial is to give a concise introduction to Python and is aimed at Civil and Environmental Engineering undergraduate and graduate students. This tutorial is by no means comprehensive. There are several very good detailed sources for Python. Some of the popular Python resources are listed below:

Note

If the figures in this tutorial appear too small or too big, try a different browser. If the formatting appears odd on multiple browsers, send us an email and we will do our best to fix the issue.

Installing Python

This section provides instructions for installing Python. There are several python variants and distribution available. I normally use the Anaconda distribution. Anaconda is an open source Python distribution whcih contains most of the essential libraries we need for our application. I normally use Python to link up with Gurobi optimization software. Often the Anaconda distribution updates itself faster than the Gurobi Python support.

Installing Python

If you are not worried about linking up with Gurobi. You can download the latest version from the Anaconda website.

  • Go to the webpage: https://www.continuum.io/downloads
  • There are two common variants of Python - Python 2.X and Python 3.X
  • This tutorial is for Python 3.X. So download the latest Anaconda for Python 3.X corresponding to your operating system.
  • The rest of the instructions assume that you have a windows operating system
  • Run the installation file
  • For windows installation, in the advanced installation options, make sure you check the add anaconda to the system path environmental variable option

First Python Program

This section outlines the various ways to run Python programs

Command Line

Python can be run using the command line interface.

  • In the command window, type Python and press enter
  • Type ``print(“Hello World”)’’ and press enter

Text Editor

  • Use a text editor like Notepad++ or Sublime Text.
  • Create new file.
  • Type ``print(“Hello World”)’’
  • Save the file as ``HelloWorld.py’’
  • Go to the folder where you saved the HelloWorld.py file and open a command window. In Windows you can do this by pressing Shift + Right Click and clicking ``Open command window here" option.
  • Type python HelloWorld.py and press enter.
  • You should see the output now.

Spyder Integrated Development Environment (IDE)

There are several popular and free IDE’s for Python. We will use the Spyder IDE which is installed with Anaconda.

  • Open the Spyder IDE. You will find this in Windows, in the all programs list, under Anaconda.
  • Click on File, New File, and then type ``print(“Hello World”)’’.
  • Save the file as ``HelloWorld.py’’
  • Press F5 to run the file.
  • You should see the output now.

Jupyter Notebooks

The Jupyter notebook app allows us to run python code, views output, text through a web brower. There are two ways to start a jupyter notebook.

  • Double click the jupyter notebook app. You will find this in Windows, in the all programs list, under Anaconda.
  • Go to a folder where you want the notebook document to reside. Open a command window in the folder. Then type jupyter notebook and press Enter.
  • You should now be able to see the notebook dashboard.
  • For details on notebook basics click here
  • To create a new notebook, click on New, and then select Python[conda root]
  • In jupyter notebooks, the code is entered in cells.
  • In the first cell, type ``print(“Hello World”)’’ and then press Shift + Enter. You should get the following output.
  • You can enter more commands in the next cell and run them by selecting the corresponding cells and pressing Shift + Enter.
  • Give the notebook a new name by selecting File, Rename.
  • If you want to save the notebook, select File, Download as “notebook (.ipynb)”.
  • If you want to shut down the notebook, close the webbrowser tab as well as the corresponding jupyter notebook command window.
  • If you have opened multiple notebooks, click on Running tab in notebook dashboard and just shutdown the ones you need.
  • If you want to open a notebook which you closed, go to the folder where you saved the notebook file, open a command window and type ``jupyter notebook filename.ipynb’’.

My recommendation is to use Spyder or Jupyter Notebook for all of your coding.

Python Tutorial