Introduction to Programming with Python

Launch Binder  Open in Colab  Download Notebook  View on Github  Download Zip  Download TarGz  last updated

A Tufts University Data Lab Workshop
Written by Uku-Kaspar Uustalu

datalab.tufts.edu  @TuftsDataLab

Resources: go.tufts.edu/python
Slides: tufts.box.com/v/intro-python
Live offerings: go.tufts.edu/workshops
Contact: datalab-support@elist.tufts.edu


Table of Contents


Workshop Overview

This is a hands-on workshop designed to introduce you to the wonderful world of programming and get you acquainted with the popular and easy-to-use programming language Python. The first part of the workshop involves hands-on exercises using a command-line based Python interpreter and is intended to be completed at an instructor-guided session. The second part of the workshop is composed of an interactive Python notebook with built-in exercises and solutions. The notebook is suitable for self-guided study and covers the following:

The notebook is designed to be run in a pre-configured cloud-computing environment either via Binder or Google Colab and does not require the installation of any software. Use of Binder is recommended as it provides a JupyterLab interface and allows for an experience similar to using a local installation. It is also possible to run the workshop notebook using a local installation of JupyterLab Desktop or Anaconda/Mambaforge. Instructions on how to install a suitable Python environment are available here: go.tufts.edu/installingPython


Part 1/2: Working with Python on the Command Line

Launching a Virtual Terminal Instance (Recommended)

Launch Terminal

  1. Click on the Launch Terminal button above.
  2. A Binder instance will launch in a new tab with the message Starting Repository.
  3. Wait patiently and do not close the Binder tab. After a few minutes, a Terminal instance will launch.
  4. To launch a Python interpreter, type python and press Enter or Return.

Launching a Conda-Enabled Console using a Local Anaconda or Mambaforge Installation

  1. If you have Anaconda or Mambaforge installed, you can launch a Conda-Enabled Terminal or Prompt as follows:
    • Windows (Anaconda): Start > Anaconda3 > Anaconda Prompt
    • Windows (Mambaforge): Start > Mambaforge > Mambaforge Prompt
    • macOS: Applications > Utilities > Terminal
  2. Ensure the last line of text in the Terminal or Prompt begins with (base).
  3. To launch a Python interpreter, type python and press Enter or Return.

Part 2/2: Using an Interactive Python Notebook Environment

Running the Notebook using a Virtual JupyterLab Instance (Recommended)

Launch Binder

  1. Click on the Launch Binder button above.
  2. A Binder instance will launch in a new tab with the message Starting Repository.
  3. Wait patiently and do not close the Binder tab. After a few minutes, a JupyterLab instance will launch.
  4. If the workshop notebook does not automatically open, double-click on intro-python.ipynb in the file browser on the left.

Running the Notebook using Google Colab

Open in Colab

  1. Click on the Open in Colab button above. The Google Colab interface will open in a new tab.
  2. If you are not already signed into your Google account, click on Sign In in the upper-right and sign in with your Google credentials. You must be signed in with a Google account to be able to run notebooks in Google Colab.
  3. Optional: To retain any changes you make to this notebook, save a copy of the notebook to your Google Drive by clicking the Copy to Drive button or selecting File > Save a copy in Drive. This will save the notebook to the Colab Notebooks directory in your Google Drive. Once the saved copy pops up, close the original notebook and use the copy going forward. Feel free to rename the copy if desired. Any changes made to your personal copy will be automatically saved.

You might also see a message warning you that this notebook was not authored by Google and hence might contain malicious code. You can trust Data Lab notebooks, so click Run Anyway. But when running other third-party notebooks, you should review the code beforehand.


Running the Notebook using a Local JupyterLab Desktop Installation

Download Notebook

  1. Click on the Download Notebook button above to download the workshop notebook.
  2. Create a designated directory for this workshop and move the downloaded intro-python.ipynb notebook there.
  3. Open the directory containing intro-python.ipynb using File Explorer (Windows) or Finder (macOS).
  4. Right-click on the downloaded intro-python.ipynb file and select Open With > JupyterLab Desktop to launch the notebook.

Running the Notebook using a Local Anaconda Installation

Download Notebook

  1. Click on the Download Notebook button above to download the workshop notebook.
  2. Create a designated directory for this workshop and move the downloaded intro-python.ipynb notebook there.
  3. Launch Anaconda Navigator via the Start Menu (Windows) or from Applications (macOS).
    • Windows: Start > Anaconda3 > Anaconda Navigator
    • macOS: Applications > Anaconda Navigator
  4. Launch JupyterLab using the corresponding Launch button.
  5. JupyterLab will launch in a web browser. (A new tab will be generated if a browser is already open.)
  6. Within the JupyterLab file explorer on the left, navigate to the directory containing the intro-python.ipynb notebook.
  7. Once you are in the correct directory, double-click on intro-python.ipynb in the file browser to open the workshop notebook.

Running the Notebook using a Local Mambaforge Installation

Download Zip  Download TarGz

  1. Launch Terminal (macOS/Linux) or Mambaforge Prompt (Windows).
    • Windows: Start > Mambaforge > Mambaforge Prompt
    • macOS: Applications > Utilities > Terminal
  2. Run the following commands by typing or pasting the command into the console and then pressing Enter or Return.
    • Download and extract the workshop materials:
      curl -Lo - https://github.com/tuftsdatalab/intro-python/archive/workshop.tar.gz | tar -xzf -
      
    • Navigate into the extracted directory:
      cd intro-python-workshop
      
    • Create a new environment for the workshop:
      mamba env create -f environment.yml
      
    • Activate the workshop environment:
      conda activate intro-python
      
    • Open the workshop notebook in JupyterLab:
      jupyter lab intro-python.ipynb
      
  3. JupyterLab will launch in a web browser. (A new tab will be generated if a browser is already open.)
  4. If the workshop notebook does not automatically open, double-click on intro-python.ipynb in the file browser on the left.
  5. Do not close the console! Closing the console will also terminate JupyterLab. Leave the console running in the background.

Additional Resources {resources}

Software Carpentry Lessons