Introduction to Python#

Course Description#

Python is one of the most popular general-purpose programming languages around. It is used in a huge range of applications, from building systems incorporating cutting-edge machine learning, such as in self-driving cars, to building web applications with hundreds of millions of users, as seen in Instagram. As researchers, we use Python for all manner of tasks, from data analysis, to automation, to prototyping software, to creating engaging visuals for websites and publications. Like you, we started our journey into Python learning the basics, building a strong foundation for achieving these more complex tasks. This introductory course will teach you these basics, and get you up to speed on your own programming journeys.

Course Objectives#

  • Understand and declare the basic data types in Python

  • Understand the basic principles of control flow and for loops

  • Describe what a function is and define one that takes user-specified parameters

  • Perform simple data analysis/visualisation of a table of data

  • Recognise concepts in other programming languages

Pre-requisite Knowledge#

No pre-requisite knowledge needed. However, course attendees will need to follow our setup guide to install Python and JupyterLab, which we will use for the course.

Installation Guide#

Introduction#

For this course, attendees will need to install the programming language Python, and an editor, where we can type commands, and run our programs. We will also need an up-to-date web browser: we recommend a current version of Chrome, Safari, or Firefox.

Step 1. Installing Python (by installing Anaconda)#

Python, and also various editors, are included in the all-in-one installer called Anaconda. This makes it easy to install Python and various scientific add-ons, without having to know too much about programming before starting out.

The Software Carpentries have created some excellent guides for installing Anaconda. These can be found here, along with video tutorials for each operating system. Please follow the relevant installation guide for your operating system (macOS, Windows, or Linux).

Further installation instructions can be found on the Anaconda web-pages here. These are a bit more in-depth!

Step 2. Installing an editor#

In the first session we will use the JupyterLab editor that is included with Anaconda. We will work in a Jupyter notebook within this editor. In the second session, we will continue to use the JupyterLab editor, but might introduce Microsoft Visual Studio Code, one of the most popular code editors that you will probably end up using at some point.

Please note that you can use Jupyter notebooks without using JupyterLab. JupyterLab provides additional editor functionality over a notebook, and makes it easier to write scripts/more substantial pieces of code. A key element of this is providing easy access to a terminal/shell window, where system/shell commands can be run.

To install JupyterLab, please follow the installation instructions for Anaconda above, as JupyterLab is included with Anaconda.

Step 3. Launching JupyterLab#

To launch JupyterLab, we first need to start up Anaconda Navigator. This can be done by searching for Anaconda Navigator in your system search bar, and clicking on the green, circular icon.

Once this has opened, click the JupyterLab launch button, as below. This is different from selecting “Notebook”.

Anaconda Navigator

This should launch a web browser and open a tab for JupyterLab. The title of the tab will be JupyterLab. It will look like:

JupyterLab

Once you are here you are ready for the first session.

Other ways to launch JupyterLab#

You can also launch JupyterLab from the command line. On Windows, open up an Anaconda PowerShell, or a command line shell and type the following. On macOS, open a terminal window, and type the same command.

jupyter lab

Extra: Installing VSCode#

VSCode (Visual Studio Code) is a popular editor created by Microsoft. It has a large collection of extensions to help editing various programming languages. One of the languages that is well supported is Python.

We might take a look at VSCode during the second session, but these installation instructions are mostly here for your interest/benefit, as you might move on to using VSCode quite quickly.

You can download VSCode from code.visualstudio.com. There is a tutorial on how to setup Python with VSCode at python-tutorial.

To check that you have everything installed open VSCode. Then open a terminal window in VSCode (Terminal -> ‘New Terminal’).

In this terminal type python --version. If you see something similar to the following, you have set everything up correctly.

Python 3...

Help#

Please have a go at installing Anaconda yourself. If you are struggling, we will be holding an online drop-in before the first session. An email link to this drop-in session will be made available to you once your registration on the course has been confirmed.

FAQ#

  • My browser isn’t showing anything and I cannot change browser!

    • From StackOverflow the basic answer is to create a config file and specify the browser. A config file can be created with

      Jupyter notebook --generate-config
      

      Then the c.NotebookApp.browser and c.ServerApp.browser options can be set to the appropriate browser.

  • What is the difference between JupyterLab and a Jupyter Notebook?

    • JupyterLab is an editor (or integrated development environment, IDE) which can be used to create and edit notebooks and scripts.

    • A notebook is a particular format of script that allows code and text blocks to be interwoven. The code blocks can be executed in line to show their answers. This is a helpful format for explaining, demonstrating and exploring methods, and commonly used in research & teaching environments.

Acknowledgements#

This course was adapted from the Software Carpentries Programming with Python. It has been developed by the University of Exeter Research Software Engineering Group and a team of generous volunteers who are enthusiastic about sharing their skills with the wider research community.

Its provision is dependent on the time of these volunteers. If you have benefited in any way from this course and want to support its long term sustainability then please take the time to complete our feedback survey, recommend it to your colleagues, and enthuse about it to your senior leadership team!

Developers#

The contributors to this course include:

  • Michael Saunby

  • Simon Kirby

  • Duncan McDougall

  • Eilis Hannon

Course Delivery Content#

There are currently no additional content that is used outside of the self study notes to deliver this course.

License Info#

Instructional Material

The instructional material in this course is copyright © 2024 University of Exeter and is made available under the Creative Commons Attribution 4.0 International licence (https://creativecommons.org/licenses/by/4.0/). Instructional material consists of material that is contained within the “individual_modules/introduction_to_python” directory, and images folders in this directory, with the exception of code snippets and example programs found in files within these folders. Such code snippets and example programs are considered software for the purposes of this licence.

Software

Except where otherwise noted, software provided in this repository is made available under the MIT licence (https://opensource.org/licenses/MIT).

Copyright © 2024 University of Exeter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The software in this repository is adapted from software that is covered by the following copyright and permission notice:

Copyright © 2024 Software Carpentry

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.