Skip to content

Commit a385afa

Browse files
authored
Added content on interacting with Python
1 parent 2ba054c commit a385afa

File tree

1 file changed

+113
-7
lines changed

1 file changed

+113
-7
lines changed

_episodes/01-conda.md

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ exercises: 15
55
questions:
66
- "What are the main Python libraries used in atmosphere and ocean science?"
77
- "How do I install and manage all the Python libraries that I want to use?"
8+
- "How do I interact with Python?"
89
objectives:
910
- "Identify the main Python libraries used in atmosphere and ocean science and the relationships between them."
1011
- "Explain the advantages of Anaconda over other Python distributions."
1112
- "Extend the number of packages available via conda using conda-forge."
1213
- "Create a conda environment with the libraries needed for these lessons."
14+
- "Open a Jupyter Notebook ready for use in these lessons"
1315
keypoints:
1416
- "xarray and iris are the core Python libraries used in the atmosphere and ocean sciences."
1517
- "Use conda to install and manage your Python environments."
@@ -237,20 +239,124 @@ $ conda list
237239
> {: .language-bash}
238240
{: .callout}
239241
242+
## Interacting with Python
240243
241-
> ## Install the libraries required for this lesson
244+
Now that we know which Python libraries we want to use and how to install them,
245+
we need to decide how we want to interact with Python.
246+
247+
The most simple way to use Python is to type code directly into the interpreter.
248+
This can be accessed from the bash shell:
249+
250+
~~~
251+
$ python
252+
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
253+
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
254+
Type "help", "copyright", "credits" or "license" for more information.
255+
>>> print("hello world")
256+
hello world
257+
>>> exit()
258+
$
259+
~~~
260+
{: .language-bash}
261+
262+
The `>>>` prompt indicates that you are now talking to the Python interpreter.
263+
264+
A more powerful alternative to the default Python interpreter is IPython (Interactive Python).
265+
The [online documentation](https://ipython.readthedocs.io/en/stable/)
266+
outlines all the special features that come with IPython,
267+
but as an example, it lets you execute bash shell commands
268+
without having to exit the IPython interpreter:
269+
270+
~~~
271+
$ ipython
272+
Python 3.7.1 (default, Dec 14 2018, 13:28:58)
273+
Type 'copyright', 'credits' or 'license' for more information
274+
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
275+
276+
In [1]: print("hello world")
277+
hello world
278+
279+
In [2]: ls
280+
data/ script_template.py
281+
plot_precipitation_climatology.py
282+
283+
In [3]: exit
284+
$
285+
~~~
286+
287+
(The IPython interpreter can also be accessed via the Anaconda Navigator
288+
by running the QtConsole.)
289+
290+
While entering commands to the Python or IPython interpreter line-by-line
291+
is great for quickly testing something,
292+
it's clearly impractical for developing longer bodies of code
293+
and/or interactively exploring data.
294+
As such, Python users tend to do most of their code development and data exploration
295+
using either an Integrated Development Environment (IDE) or Jupyter Notebook:
296+
297+
* Two of the most common IDEs are [Spyder](https://www.spyder-ide.org/)
298+
and [PyCharm](https://www.jetbrains.com/pycharm/)
299+
(the former comes with Anaconda)
300+
and will look very familiar to anyone
301+
who has used MATLAB or R-Studio.
302+
* [Jupyter Notebooks](https://jupyter.org/) run in your web browser
303+
and allow users to create and share documents that contain live code,
304+
equations, visualizations and narrative text.
305+
306+
We are going to use the Jupyter Notebook to explore our precipitation data
307+
(and the plotting functionality of xarray) in the next few lessons.
308+
A notebook can be launched from the Anaconda Navigator (not shown)
309+
or the bash shell:
310+
~~~
311+
$ jupyter notebook &
312+
~~~
313+
{: .language-bash}
314+
315+
(The `&` allows you to come back and use the bash shell without closing
316+
your notebook first.)
317+
318+
> ## JupyterLab
319+
>
320+
> The Jupyter team have recently launched
321+
> [JupyterLab](https://blog.jupyter.org/jupyterlab-is-ready-for-users-5a6f039b8906)
322+
> which combines the Jupyter Notebook with many of the features common to an IDE.
323+
>
324+
{: .callout}
325+
326+
> ## Install the Python libraries required for this lesson
242327
>
243328
> Go ahead and install jupyter, xarray, cartopy and cmocean using either the Anaconda Navigator,
244329
> Bash Shell or Anaconda Prompt (Windows).
245330
>
246-
> (You may like to create a separate `pyaos-lesson` environment,
331+
> (You may like to create a separate `pyaos-lesson` conda environment,
247332
> but this is not necessary to complete the lessons.)
248333
>
249334
> > ## Solution
250-
> > ~~~
251-
> > $ conda config --add channels conda-forge
252-
> > $ conda install jupyter xarray netCDF4 cartopy cmocean
253-
> > ~~~
254-
> > {: .language-bash}
335+
> >
336+
> > The "Software installation" section of the
337+
> > [Setup menu](https://carpentrieslab.github.io/python-aos-lesson/setup.html)
338+
> > at the top of the page
339+
> > contains a series of drop-down boxes explaining how to install the Python libraries
340+
> > on different operating systems.
341+
> > Use the "default" instructions unless you want to create the separate
342+
> > `pyaos-lesson` conda environment.
343+
> >
255344
> {: .solution}
256345
{: .challenge}
346+
347+
> ## Launch a Jupyer Notebook
348+
>
349+
> In preparation for the next lesson,
350+
> open a new Jupyter Notebook (from either the bash shell or Anaconda Navigator)
351+
> and import xarray, catropy, matplotlib and numpy using the following Python command:
352+
> ~~~
353+
> import xarray as xr
354+
> import cartopy.crs as ccrs
355+
> import matplotlib.pyplot as plt
356+
> import numpy as np
357+
> ~~~
358+
{: .language-python}
359+
>
360+
> (Hint: Hold down the shift and return keys to execute a code cell in a Jupyter Notebook.)
361+
>
362+
{: .challenge}

0 commit comments

Comments
 (0)