In this assignment we will check out a fundamental Python data type: dictionaries. We will also use a *.py
file to import data to help understand what a dictionary is.
While dictionaries are fun and useful, the real purpose of this assignment is to confirm that you are comfortable using Jupyter notebooks, as we will use them a lot in MUDE. If you have used these *.ipynb
files before, this will be easy although the VSC interface is slightly different than Jupyter software).
If you have never used a Jupyter notebook before, don't panic: your teachers are available to help (don't forget about question hours on Tue and Thu!). Hopefully one of your group members can also help you get acquainted with these files and how to use them.
If you find notebooks to be "simple" we ask that you please help your fellow group members if they are unfamiliar with this tool! This will help you become a more collaborative group later on in the quarter, so help each other out now while the assignments are easier.
Part 0: Making sure you can run Python in the notebook¶
In a simple sense, Jupyter Notebooks are documents that consist of many cells. Some cells are formatted text (Markdown, like this one), others can be used to execute Python code. Because we have already set up a conda environment that includes the Python programming language, all we have to do is tell VS Code which environment to use and it will take care of sending the code in the Python cells to the environment so that they can be executed as desired. Then the output from running that code is displayed in the space below the Python cell from which it was executed. Let's try it with a simple print()
statement!
If you did the installation of Miniconda and Jupyter properly, you should be able to execute Python cell below
Task 0: Confirm that Python and your conda environment are working properly by running the cell below. If you did the installation correctly, this should involve the following:
- Click the triangular "run" icon next to the cell.
- At the top of your VSC window you will be asked to select a Python environment. Choose the conda environment you just created,
mude-base
- Confirm that the output of the cell is generated as expected.
- That's it!
print('If you connected to an environment with Python,'
' this sentence will be printed below.')
Part 1: What is a Dictionary?¶
When getting started, if you are simply using dictionaries (we will write them later), there are really only a few fundamental things to know about a dictionary:
- It is a Python data type that stores key-value pairs.
- It is common to define a key as a string (e.g.,
'this is a string'
) - The value can be almost anything.
- It uses a syntax with square brackets to get the value associated with a given key.
Task 1.1: run the cell below to see what happens when you define a dictionary and then print it.
test_dictionary = {'key1': 'value1', 'key2': 'value2'}
print(test_dictionary)
To access the value, you can use the following syntax:
test_dictionary['KEY']
Task 1.2:
run the cell below to access the value for key2
.
test_dictionary['key2']
Task 1.3:
run the cell below to access add a key-value pair for key3
.
test_dictionary['key3'] = 'value3'
print(test_dictionary['key3'])
That's it! Now you are ready to start using a dictionary with "real" data!
Part 2: Using a dictionary with "real" data¶
In this part we illustrate how easy it is to load text-based files into notebooks, then operate on them with Python code.
Task 2.1:
open up the contents of the file catchme.py
and read it. This file defines a dictionary called data
, which we will import into our notebook and access the content by running the cell below.
import catchme
data = catchme.data
Task 2.2:
If the cell above ran without error, you should be use the dictionary data
in exactly the same way as test_dictionary
. To check whether you understand how a dictionary works, see if you can print the value for the species of this "thing" in the dictionary.
YOUR_CODE_HERE
That's it! This was a short PA...later ones will be longer.