Basic elements of Python#

These Python lessons provide an overview of some of the basic elements in the Python programming language. In the first set they include how to define and use variables, the concept of a data type, and how to determine the data type of a variable in Python, and Python lists. Later parts of the lessons introduce the NumPy numerical Python library, and the main conceptual programming components we will use: functions, loops, and conditional statements.

Please note that this is not intended to be a comprehensive introduction, but rather something to give you enough background to be able to work on the programming assignments later in this course. For more about basic Python programming we refer you to the Python resources page.

Sources#

With the exception of the NumPy lesson, these lessons are based on the materials from the Introduction to Python for Geographic Data Analysis book.

About this document#

This is a Jupyter Notebook, with content designed to cover the basic concepts of programming in Python. The contents of this document are divided into cells, which can contain Markdown-formatted text, Python code, or raw text. You can execute code in a cell by pressing Shift+Enter. Try this out in the examples below.

Getting started#

Presumably if you have made it this far, you have already opened this Jupyter Notebook on the CSC Notebooks platform or your own computer. If not, you can launch Jupyter Lab on your own computer by typing the following in a terminal window:

jupyter lab

This should open a new Jupyter Lab session from which you can open this document by navigating its location in the Files tab and double clicking on it. After that you should be ready to go.

Lesson materials#

Materials for this lesson can be found on the website for the Introduction to Python for Geographic Data Analysis book.

Student notebooks#

Students in the course can work through notebooks with limited code cell content by creating their copy of the notebooks from GitHub Classroom. Instructions for this can be found on the Exercise notebooks in GitHub Classroom page.

Exercise(s)#

Getting to know lists#

  • Create a Python list containing 4 items/values and store it with the variable my_list.

  • After you have created the list, check its data type, then use the index values to check the data types of the contents of the list.

  • What kinds of data can you store in lists? Does each list value need to be the same data type?

# You can create your example list in this cell
# By the way, you can comment lines in Python (or a Jupyter Notebook Python cell)
# by starting the line with a number sign
# Comments are not executed, but can help explain what you code does

# Put your code below