IM939 - Lab 1 - Part 1

This is simply a test, embedding a jupyternotebook (ipynb) within a qmdfile.

4.1 The embed starts below

This is a copy of the regular Jupyter notebook, for embedding it in a qmd file to see if they are formatted differently.

Welcome to the first Python lab.

Our aim is to introduce a little bit of Python and help you be comfortable with using Jupyter notebooks. No previous knowledge is assumed and questions are encouraged!

Please make sure you have Anaconda Python installed and running on your computer. Instructions for doing this on a Windows 10 machine can be found in the video below. There is a little code before the video which is needed to display the video in the notebook, you can ignore this for now.

For your convenience, the installation files for anaconda for each platform are linked below.

Windows MacOS

4.2 Python?

Python is a very popular general purpose programming language. Data scientists use it to clean up, analyse and visualise data. A major strength of python is that the core Python functionality can be extended using libraries. In future labs, you will learn about popular data science libraries such as pandas and numpy.

It is useful to think of programming languages as a structured way to tell the computer what to do. We cover some of the basic features of Python in this lab.

4.2.1 Anaconda

Anaconda is a collection of different programs. These programs include Python, many of the most popular data science libaries, Jupyter notebooks and development environments such as VS Code or Spyder, which are Integrated Development Environments (IDEs) that we can use for python.

4.2.2 Jupter Notebooks

Jupyter notebooks, such as this one, allow you to combine text and code into documents you can edit in the browser. The power of these notebooks is in documenting or describing what you are doing with the code alongside that code. For example, you could detail why you chose a particular clustering algorithm above the clustering code itself. In other words, it add narrative and helps clarify your workflow.

4.3 Getting started

If you send Python a number then it will print that number for you.

You will see both the input and output displayed. The input will have a label next to it like ‘In [1]’ where the number tells you how much code has already been sent to the Python interpreter (the programming interpreting the Python code and returnign the result). A line such as ‘In [100]’ tells you that 99 code cells have been passed to the Python interpreter in the current session.

Python can carry out simple arithetic.

Each time the code in the cell is run and the result from the Python interpreter is displayed.

5 Data Types

5.1 int, floats, strings

Integers are whole numbers. We used them above.

You can also have floats (numbers with decimal points)

and a series of characters (strings).

Data types are great and operators such as * do different things depending on the data type. For instance,

That seems quite sensible. What about if we had a string? Run teh below line. What is the * doing?

There are also operators which only work with particular data types.

This error message is very informative indeed. It tells us the line which caused the problem and that we have an error. Specifically, our error is a TypeError.

The line

‘I have a cunning plan’ / 2

consists of

string / int

We are trying to divide a string and int. The / operand is not able to divide a string by an int.

5.1.1 lists and dictionaries

You can collect multiple values in a list.

Or add keys to the values as a dictionary.

6 Variables

Variables are bins for storing things in. These things can be data types. For example, the below creates a variable called my_height and stores the in 140 there.

The Python interpreter is now storing the int 140 in a bin called my_height. If you pass the variable name to the interpreter then it will behave just like if you typed in 140 instead.

140

Variables are neat when it comes to lists.

Wait, what happened above? What do you think the [1] does?

You can index multiple values from a list.

6.1 Bringing it all together

What does the below do?

As an aside, you can include comments which are not evaluated by the Python interpreter.

7 Congratulations

You’ve reached the end of the first notebook. We’ve looked at basic data type and variables. These are key components of all programming languages and a key part of working with data.

In the next notebook we will examine using libraries, loading in data, loops and functions.

Source: labs-wk1.1.ipynb