Warm-Up: Jupyter Notebooks and Python Dictionaries

Warm-Up: Jupyter Notebooks and Python Dictionaries#

IITM Workshop Note

Depending on your time available and prior experience with Python, we recognize the “homework” assignment may not be possible to finish. However, we hope you are at least able to understand the Python code on this page, and use it interactively with the Live Code feature .

This page was modified from the MUDE version to illustrate a few key Python concepts, especially so those that are new to Python can see some of the essentials for the workshop.

In this section we use a Jupyter Notebook to briefly review and practice a few Python concepts.

The code on this page can be used interactively: click –> Live Code in the top right corner, then wait until the message Python interaction ready! appears.

When this page is activated a special version of Python will be loaded into your web browser that enables you to interact with this page as if it were a Jupyter Notebook. Note that package import does not work the same as it would on a “normal” computer, so the necessary imports have already been prepared for you.

The download button provides a *.ipynb file which you can run on your computer to make sure you have Python and Jupyter installed correctly in your IDE.

Python Examples#

The rest of this page takes the opportunity to illustrate a few basic Python features.

Key Data Types#

Here is an illustration of key data types in Python.

x = 3
print(f'x is a variable with type {type(x)} and value {x}')
x = 3.0
print(f'x is a variable with type {type(x)} and value {x}')
x = 'whoa'
print(f'x is a variable with type {type(x)} and value {x}')
x = [2, 4, 7, 4]
print(f'x is a variable with type {type(x)} and value {x}')
x is a variable with type <class 'int'> and value 3
x is a variable with type <class 'float'> and value 3.0
x is a variable with type <class 'str'> and value whoa
x is a variable with type <class 'list'> and value [2, 4, 7, 4]

Indexing#

Python starts indexing at 0, which is of course different than other languages like Matlab. Here is an illustration of how we can access and “slice” a list (accessing particular values). Note the 0 indexing and the colon notation, especially the reverse indexing and the fact that the slice indices are non-inclusive.

x = [2, 5, 3, 4]
index = 0
print(f'The item in x with index {index} is {x[index]}')
print(f'The first item in x is {x[0]}')
print(f'The last item in x is {x[-1]}')
print(f'The second through third items in x are {x[1:3]}')
print('     ^^^            ^^^')
print('the last one is tricky:')
print('  - "1:3" means 1, 2, but not 3...')
print('  - ... and "1, 2" means items 2 and 3!')
The item in x with index 0 is 2
The first item in x is 2
The last item in x is 4
The second through third items in x are [5, 3]
     ^^^            ^^^
the last one is tricky:
  - "1:3" means 1, 2, but not 3...
  - ... and "1, 2" means items 2 and 3!

What happens if we try to do matrix/vector manipulation with the list?

y = x + x
print(y)
[2, 5, 3, 4, 2, 5, 3, 4]

That’s not what we expected!

There’s probably a better way to do this…

Numpy#

Numpy is essential for scientific computing in Python. Here we illustrate the minimum code needed to be able to do matrix and vector calculations efficiently (in terms of code written, as well as numerically).

First start by importing the Numpy library in a conventional way, as np. Many “things” are available in Numpy using the dot notation to access them. For example, the constant pi, \(\pi\).

import numpy as np
print(np.pi)
print(f'Remember F strings still? Pi = {np.pi:.3f}')
3.141592653589793
Remember F strings still? Pi = 3.142

Numpy has a key data type: an “array” (technically an ndarray), which is easily created as follows:

y = np.array(x)
print(f'x is a variable with type {type(x)} and value {x}')
print(f'y is a variable with type {type(y)} and value {y}')
x is a variable with type <class 'list'> and value [2, 5, 3, 4]
y is a variable with type <class 'numpy.ndarray'> and value [2 5 3 4]

Now let’s try that list addition again:

z = y + y
print(z)
[ 4 10  6  8]

Great! Hopefully that is enough Python for you to get involved in the workshop activities.

Tip

For more Python fundamentals, visit our online textbook Learn Python, which is also an interactive online textbook.