<jupyter_start><jupyter_text>(getting_started)= Setting up Your Python Environment OverviewIn this lecture, you will learn how to1.  get a Python environment up and running2.  execute simple Python commands3.  run a sample program4.  install the code libraries that underpin these lectures AnacondaThe [core Python package](https://www.python.org/downloads/) is easy toinstall but *not* what you should choose for these lectures.These lectures require the entire scientific programming ecosystem,which-   the core installation doesn\'t provide-   is painful to install one piece at a time.Hence the best approach for our purposes is to install a Pythondistribution that contains1.  the core Python language **and**2.  compatible versions of the most popular scientific libraries.The best such distribution is[Anaconda](https://www.anaconda.com/what-is-anaconda/).Anaconda is-   very popular-   cross-platform-   comprehensive-   completely unrelated to the <NAME> song of the same nameAnaconda also comes with a great package management system to organizeyour code libraries.```{note}All of what follows assumes that you adopt this recommendation!```(install_anaconda)= Installing AnacondaTo install Anaconda, [download](https://www.anaconda.com/download/) thebinary and follow the instructions.Important points:-   Install the latest version!-   If you are asked during the installation process whether you\'d like    to make Anaconda your default Python installation, say yes. Updating AnacondaAnaconda supplies a tool called `conda` to manage andupgrade your Anaconda packages.One `conda` command you should execute regularly is the onethat updates the whole Anaconda distribution.As a practice run, please execute the following1.  Open up a terminal2.  Type `conda update anaconda`For more information on `conda`, type `condahelp` in a terminal.(ipython_notebook)= Jupyter Notebooks[Jupyter](http://jupyter.org/) notebooks are one of the many possibleways to interact with Python and the scientific libraries.They use a *browser-based* interface to Python with-   The ability to write and execute Python commands.-   Formatted output in the browser, including tables, figures,    animation, etc.-   The option to mix in formatted text and mathematical expressions.Because of these features, Jupyter is now a major player in thescientific computing ecosystem.{numref}`Figure %s ` shows the execution of some code (borrowed from[here](http://matplotlib.org/examples/pylab_examples/hexbin_demo.html))in a Jupyter notebook```{figure} /_static/lecture_specific/getting_started/jp_demo.png:scale: 50%:name: jp_demoA Jupyter notebook viewed in the browser```While Jupyter isn\'t the only way to code in Python, it\'s great forwhen you wish to-   get started-   test new ideas or interact with small pieces of code-   share scientific ideas with students or colleagues Starting the Jupyter NotebookOnce you have installed Anaconda, you can start the Jupyter notebook.Either-   search for Jupyter in your applications menu, or-   open up a terminal and type `jupyter notebook`    - Windows users should substitute \"Anaconda command prompt\" for \"terminal\" in the previous line.If you use the second option, you will see something like this```{figure} /_static/lecture_specific/getting_started/starting_nb.png:scale: 50%```The output tells us the notebook is running at `http://localhost:8888/`-   `localhost` is the name of the local machine-   `8888` refers to [port number](https://en.wikipedia.org/wiki/Port_%28computer_networking%29)    8888 on your computerThus, the Jupyter kernel is listening for Python commands on port 8888 of ourlocal machine.Hopefully, your default browser has also opened up with a web page thatlooks something like this```{figure} /_static/lecture_specific/getting_started/nb.png:scale: 50%```What you see here is called the Jupyter *dashboard*.If you look at the URL at the top, it should be `localhost:8888` orsimilar, matching the message above.Assuming all this has worked OK, you can now click on `New` at the topright and select `Python 3` or similar.Here\'s what shows up on our machine:```{figure} /_static/lecture_specific/getting_started/nb2.png:scale: 50%```The notebook displays an *active cell*, into which you can type Pythoncommands. Notebook BasicsLet\'s start with how to edit code and run simple programs. Running CellsNotice that, in the previous figure, the cell is surrounded by a greenborder.This means that the cell is in *edit mode*.In this mode, whatever you type will appear in the cell with theflashing cursor.When you\'re ready to execute the code in a cell, hit `Shift-Enter`instead of the usual `Enter`.```{figure} /_static/lecture_specific/getting_started/nb3.png:scale: 50%```(Note: There are also menu and button options for running code in a cellthat you can find by exploring) Modal EditingThe next thing to understand about the Jupyter notebook is that it usesa *modal* editing system.This means that the effect of typing at the keyboard **depends on whichmode you are in**.The two modes are1.  Edit mode    -   Indicated by a green border around one cell, plus a blinking cursor    -   Whatever you type appears as is in that cell2.  Command mode    -   The green border is replaced by a grey (or grey and blue) border    -   Keystrokes are interpreted as commands --- for example, typing `b` adds a new cell below the current oneTo switch to-   command mode from edit mode, hit the `Esc` key or `Ctrl-M`-   edit mode from command mode, hit `Enter` or click in a cellThe modal behavior of the Jupyter notebook is very efficient when youget used to it. Inserting Unicode (e.g., Greek Letters)Python supports [unicode](https://docs.python.org/3/howto/unicode.html),allowing the use of characters such as $\alpha$ and $\beta$ as names inyour code.In a code cell, try typing `\alpha` and then hitting the`tab` key on your keyboard.(a_test_program)= A Test ProgramLet\'s run a test program.Here\'s an arbitrary program we can use:.On that page, you\'ll see the following code<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute pie slices
N = 20
θ = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
colors = plt.cm.viridis(radii / 10.)

ax = plt.subplot(111, projection='polar')
ax.bar(θ, radii, width=width, bottom=0.0, color=colors, alpha=0.5)

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07556713672593501, 'fraction_numerical': 0.011189454322501532, 'mean_word_length': 4.779450841452613, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 5, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '106145', 'license': 'CC0-1.0', 'n_tokens_mistral': 1835, 'n_tokens_neox': 1737, 'n_words': 834, 'path': 'mini_book/_build/html/_sources/docs/getting_started.ipynb', 'repo_name': 'rebeccajohnson88/qss20', 'size': '6529'}