This lesson is being piloted (Beta version)

Intermediate Programming with Python

This lesson is intended to follow on from our Introduction to Python lesson. It is designed to be used in both Data Carpentry and Software Carpentry workshops. This lesson assumes that you will be working from the command-line with Python files and the Python interpreter. The exercises could be completed in other environments such as Jupyter or an IDE, but this may require some small adaptations to the exercises.

Why Python 3?

Please note that this lesson uses Python 3 rather than Python 2. Support for Python 2 is due to end in 2020, and users are strongly urged to move to Python 3 as soon as possible. Given this, it makes sense that new users should start with Python 3. As an added bonus, this allows us to make use of some new features of Python 3.

Prerequisites

  1. Learners need to have a basic familiarity with the command line. Specifically, you need to understand:
    1. what files and directories are,
    2. what a working directory is,
    3. how to edit a text file,
    4. how to start a Python interpreter,
    5. and how to execute Python files.
  2. Learners must install Python before the class starts. The Anaconda distribution is a free distribution that provides a consistent set of scientific Python libraries for Windows, Linux, and Mac. We recommend you use Anaconda for this course, however any Python 3 will work.

  3. Learners must download the data before class starts. Please download and unzip the file intermediate_python_data.zip.

    Please see the setup instructions for details.

Schedule

Setup Download files required for the lesson
00:00 1. Creating and Running Python Scripts How can I run Python scripts and see results from them?
How do you write comments?
00:25 2. While loops What other constructs are available for changing the flow of a program?
01:25 3. Getting Help When You Need It: Working with Documentation Where is the Python documentation and how do I use it?
How can the help() function be used?
What does the type() function do?
01:50 4. Errors and Exceptions How do you handle errors in your Python code?
02:20 5. Working with Files How do I open a file in Python?
How do I access the data or text inside an open file?
03:10 6. Functions What are functions?
How do I create my own functions in Python?
How do I use my own functions?
How do I document my functions?
03:50 7. Getting arguments from the command-line Why are command-line arguments useful?
How can I add command-line arguments to my programs?
05:00 8. Modules How do I use code defined in other files or libraries?
What does the import statement do?
What are the effects of the common import variations?
05:50 9. What's in a __name__? How do modules know when they are executing as a program, or being imported?
What does if __name__ == "__main__" do?
06:10 10. Testing What is testing?
How do I write tests for Python code?
What is Test Driven Development?
What is a doctest?
07:25 11. Final practical: How Many Articles Reference that they use R and/or Python? Can I write a command line Python progam using ArgParse?
08:55 Finish

The actual schedule may vary slightly depending on the topics and exercises chosen by the instructor.