Skip to content

Commit 2474a31

Browse files
committed
convert markdown to jupytext 1.1.0rc
1 parent 6956e5b commit 2474a31

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2379
-2159
lines changed

Python/Module1_GettingStartedWithPython/GettingStartedWithPython.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python
1212
name: python3
1313
---
1414

15+
<!-- #region -->
1516
# Introducing the Python Programming Language
1617

1718
In this section we will learn
@@ -25,24 +26,25 @@ In this section we will learn
2526

2627
For example, the following text obeys the rules of the Python language:
2728

28-
# ```python
29+
```python
2930
x = 2 + 3
3031
print('2 + 3 = {}'.format(x))
31-
# ```
32+
```
3233

3334
According to the Python language, it will instruct the computer to:
3435

3536
- compute 2 + 3
3637
- assign the resulting value (5) to a variable `x`, in memory
3738
- access the value of `x`, and print to the computer's screen: '2 + 3 = 5'
3839

40+
<!-- #endregion -->
3941

40-
42+
<!-- #region -->
4143
An example of code that **does not** obey Python's rules is:
4244

43-
# ```python
45+
```python
4446
x = 2 ~ 3
45-
# ```
47+
```
4648
because character `~` has no meaning in the Python language when it is placed between two integers.
4749

4850
To "learn Python" thus entails learning the grammatical rules specified by the Python language, and the computer instuctions that they map to. Additionally, you will want to familiarize yourself with many of the convenient tools that are "pre-written" and come with the Python language; they are part of the so-called standard library.
@@ -52,7 +54,7 @@ Given this basic understanding of what the Python programming language is, we no
5254
- Write a Python "script": a text file containing Python code.
5355
- Pass this text file to a **Python interpreter**, which will instruct the computer to carry out the operations described by the code.
5456

55-
57+
<!-- #endregion -->
5658

5759
## Python Scripts
5860
You can save the the valid code from the preceding section in a text file using a simple text editor, and *voilà* you have written a **Python script**: a text file containing Python code. It is standard to save this text file using the suffix `.py` (e.g. `my_code.py`), rather than the familiar `.txt` (e.g. `my_text.txt`). There is nothing special about the `.py` suffix; it simply helps differentiate files that contain Python code from run-of-the-mill text files, which contain plain English.
@@ -68,7 +70,7 @@ Do not use word processing programs, like Microsoft Word, to write code. They wi
6870

6971
Now that you have a Python script, how do you get the computer to read it and follow its instructions? You will need to install a **Python interpreter** on your computer to accomplish this. This is what people mean, whether they know it or not, when they tell you to "install Python" on your computer.
7072

71-
73+
<!-- #region -->
7274
## What is a Python Interpreter and What Does it Mean to "Install Python"?
7375

7476
A Python interpreter is any computer program that is capable of doing the following:
@@ -100,27 +102,28 @@ If you "install Python on your computer" from [python.org](https://www.python.or
100102

101103
Once you have a Python interpreter installed on your machine, using it to execute a Python script is quite simple. Assume for the sake of simplicity that the `python` interpreter program and `my_script.py` are in the same directory (a.k.a 'folder') in your computer. Then, in a terminal (`cmd.exe` for Windows) you can execute the following command:
102104

103-
# ```shell
105+
```shell
104106
python my_script.py
105-
# ```
107+
```
106108

107109
this will instruct the Python interpreter program `python` to read your text file `my_script.py`, ensure that your code obeys all of the grammatical rules specified by the Python language, and then send instructions to your computer in accordance with the code that it reads. If your script simply contains the code `print("hello world")`, then the text `hello world` will appear in your terminal window, as expected.
108110

109111
In practice, you will be able to simply execute `python my_script.py` in any directory, and your computer will know where to look to find the `python` program. This will be set up during the installation process.
110112

111113
It may be confusing to think that the Python language is interpreted by using a program written in another language. How, then, is that language interpreted? The answer, in the case of CPython, is that C code need not be interpreted; programs exist for Windows, Mac, and Linux that can translate C code directly into machine instructions.
114+
<!-- #endregion -->
112115

113-
116+
<!-- #region -->
114117
## Why Python?
115118

116119
Python has become a hugely popular programming language. In fact, it is likely the [most popular introductory language at universities](https://cacm.acm.org/blogs/blog-cacm/176450-python-is-now-the-most-popular-introductory-teaching-language-at-top-u-s-universities/fulltext). First and foremost, its syntax is designed to be intuitive and readable. For example, the following Python code sums the numbers 0-9, and prints the result:
117-
# ```python
120+
```python
118121
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
119122
print(sum(a))
120-
# ```
123+
```
121124

122125
This can be reproduced by the following C++ code, which is arguably less-intuitive:
123-
# ```cpp
126+
```cpp
124127
#include <iostream>
125128
#include <vector>
126129
#include <numeric>
@@ -129,12 +132,12 @@ int main() {
129132
std::vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
130133
std::cout << std::accumulate(a.begin(), e.end(), 0) << std::endl;
131134
}
132-
# ```
135+
```
133136

134137
As such, Python is a language that is conducive to rapidly prototyping and testing code. Additionally, it is open-sourced: it is free to use, anyone can participate in improving/maintaining the language, and many people create new libraries that add tremendous functionality to the language. Thus, Python has become exceptionally popular especially among scientists, engineers, and other researchers.
135138

136139
We will be relying heavily on Python and a library for doing optimized numerical computations, called NumPy, throughout this course.
137-
140+
<!-- #endregion -->
138141

139142
## Summary
140143

Python/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python

Python/Module1_GettingStartedWithPython/Informal_Intro_Python.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python

Python/Module1_GettingStartedWithPython/Installing_Python.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python
@@ -40,7 +40,7 @@ Some of the packages provided by Anaconda, like NumPy, have been [optimized](htt
4040

4141
You will need to know how to open a terminal (cmd.exe for Windows users) on your computer, and how to navigate between directories in the terminal. If you do not know how to do this, read a 'how-to' for whatever operating system you are using.
4242

43-
43+
<!-- #region -->
4444
### What did this just do to my computer?
4545

4646
This created a directory called `Anaconda3` (or some variant of this) on your computer, which contains all of the files associated with the CPython interpreter, all of the modules in Python's standard library, the aforementioned 3rd party packages that come as part of the Anaconda distribution (e.g. NumPy, SciPy, Jupyter, iPython), and the `conda` package manager. It also contains the executable files for all of these applications. The default install location for Anaconda is:
@@ -57,9 +57,9 @@ For Linux and Mac users, it is very likely that your system already has a versio
5757

5858
The simple solution to this is to *not* have the Anaconda-installer include Anaconda in your path. Instead, you can create an alias that will allow you manually prepend Anaconda to your path. E.g., in Linux you can add the following alias to your `~/.bashrc` file:
5959

60-
# ```shell
60+
```shell
6161
alias anaconda="export PATH=/home/<your_username>/anaconda3/bin:$PATH"
62-
# ```
62+
```
6363

6464
With this alias in place, you can simply invoke the command `anaconda` in your terminal to place Anaconda at the beginning of your path for that terminal session.
6565

@@ -71,36 +71,37 @@ With this alias in place, you can simply invoke the command `anaconda` in your t
7171
7272
Assuming that your initial installation of Anaconda contained Python 3, then your *root* (i.e. default) conda environment is that Python 3 environment. You can now create a conda environment that instead includes Python 2.7 and all of the 3rd party packages that come with Anaconda. Execute the following command in your terminal:
7373

74-
# ```shell
74+
```shell
7575
conda create -n py27 python=2.7 anaconda
76-
# ```
76+
```
7777

7878
Once the installation process is complete, you will be able to activate this environment, which we have named `py27`, by executing the command (Linux, Mac):
79-
# ```shell
79+
```shell
8080
source activate py27
81-
# ```
81+
```
8282
in your terminal. If you are in Windows, the command is:
83-
# ```shell
83+
```shell
8484
activate py27
85-
# ```
85+
```
8686
Activating an environment simply updates your system's path, swapping the directory `Anaconda3` with `Anaconda3/envs/py27` in this instance. Thus your system will now find the Python 2.7 executable and its associated libraries in its path. Note that this path change only occurs effect in *that particular terminal session*. Any other terminal session will default to the root conda environment.
8787

8888
Having activated your `py27` environment, you can start a vanilla Python console, an iPython console, a Jupyter notebook, run a Python script, etc. These will now all use Python 2.7. You can also use `conda` (and `pip`) to install Python 2.7-compatible packages in this environment. As long as your path points to `Anaconda3/envs/py27` and not `Anaconda3`, it is as if this is the only version of Python that lives on your computer.
8989

9090
Deactivating this environment will return you to the root Python 3 environment. That is, it will switch your path back to including `Anaconda3` instead of `Anaconda3/envs/py27`. Simply invoke the command (Mac, Linux):
9191

92-
# ```shell
92+
```shell
9393
source deactivate
94-
# ```
94+
```
9595

9696
On Windows, the command is:
9797

98-
# ```shell
98+
```shell
9999
deactivate
100-
# ```
100+
```
101101

102102
And like that, conda environments give you all of the powers of a necromancer, allowing you to nimbly cross back and forth between the land of the living (Python 3) and the dead (Python 2.7).
103103

104104
Conda environments have more uses than simply switching back and forth between Python 3 and 2. Many people like to make a new conda environment for every major project that they work on, so that they can freely install any dependencies that are needed for that particular project, without worrying about conflicts with their other work. You should be keen on making regular use of conda environments.
105105

106106
It is highly recommended that you take time to read through [this tutorial on managing conda environments](https://conda.io/docs/user-guide/tasks/manage-environments.html).
107+
<!-- #endregion -->

Python/Module1_GettingStartedWithPython/Jupyter_Notebooks.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python
@@ -52,19 +52,19 @@ Although a Jupyter notebook opens in your browser, *everything is happening loca
5252

5353

5454

55-
55+
<!-- #region -->
5656
## Notebook Cells
5757
Whereas a Python console only allows you to work on one line of code at a time, a notebook allows you to write code within "cells" and to execute these chunks of code cell-by-cell. In the first cell, write the lines of code:
5858

59-
# ```python
59+
```python
6060
x = 3
6161
y = 4
62-
# ```
62+
```
6363
then press `<SHIFT>+<ENTER>`. This will execute all of the code within the given cell (in this instance, assigning the variables `x` and `y` with the values 3 and 4, respectively) and then creates a new cell below. In the next cell type the code:
6464

65-
# ```python
65+
```python
6666
x + y
67-
# ```
67+
```
6868
and hit `<SHIFT>+<ENTER>` to execute this code. The number 7 will appear beneath the cell - this, of course, is the value that is returned when `3 + 4` is evaluated:
6969

7070
![jupyter notebook example](attachments/jupyter_early.png)
@@ -74,7 +74,7 @@ Notice that the notebook "knows" about its variables across its cells. This does
7474
Formally, the cells within a given notebook share a common "namespace": any variable defined in a cell can be referenced or redefined in any other cell within the notebook. On the other hand, separate notebooks are completely independent from one another. You can be working on multiple notebooks at once, and they will never "know" about one another.
7575

7676
A major value of using a notebook is that you can rapidly edit these cells (say, change `x = 3` to `x = 10`), and re-execute them to nimbly tinker with whatever code you are developing. Although simple, this is a hugely powerful environment for prototyping code.
77-
77+
<!-- #endregion -->
7878

7979
## An Example Notebook
8080
To show off a more exciting use-case, let's create a notebook that plots some data for us. We'll use matplotlib, a Python library that is used for plotting data, and NumPy, the premiere library for doing numerical work in Python. We will import these libraries for use in our code. Next we'll define some mathematical functions. And finally, we'll plot these functions evaluated on a large number of closely-spaced points on the domain.

Python/Module1_GettingStartedWithPython/Numerical_Work_In_Python.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python
1212
name: python3
1313
---
1414

15+
<!-- #region -->
1516
# Doing Numerical Work in Python
1617

1718
Python's elegant and flexible syntax makes it a particularly attractive language. However, this also makes Python considerably slower than other, more stringent languages (e.g., the C programming language). This is a non-issue in many circumstances. It is, however, a major roadblock when one is doing serious numerical work.
@@ -23,11 +24,11 @@ Fortunately, there are several packages that you can use in Python, which allow
2324
The fundamental package for scientific computing with Python. NumPy provides an N-dimensional array that can be used to represent, say, a large matrix of numbers. It also provides a huge number of mathematical functions that can operate on these arrays. It is crucial to note that these functions will actually be executed in C, so they are incredibly fast compared to the same functions written in vanilla Python. NumPy will be used throughout the machine learning component of the course.
2425

2526
Here is example code for summing the numbers 1-100 in NumPy:
26-
# ```python
27+
```python
2728
import numpy as np
2829
numbers = np.arange(1, 101) # an array storing the numbers 1-100
2930
numpy_result = numbers.sum()
30-
# ```
31+
```
3132
<div class="alert alert-warning">
3233

3334
**Note**:
@@ -42,7 +43,7 @@ Numba gives you the power to speed up your applications with high-performance fu
4243
We will not be using this package in this course, but it is a tremendous tool that is becoming instrumental for doing numerical work in Python.
4344

4445
Here is example code for summing the numbers 1-100 in numba:
45-
# ```python
46+
```python
4647
import numba
4748
@numba.njit
4849
def sum_func():
@@ -51,7 +52,7 @@ def sum_func():
5152
result += i
5253
return result
5354
numba_result = sum_func()
54-
# ```
55+
```
5556

5657
## Ending Remarks and Summary
5758
Using these packages can speed up numerical computations by hundreds or even thousands of times compared to pure Python. Your calculation that was taking an hour to complete now takes 3 seconds!
@@ -64,3 +65,4 @@ If you are interested in learning more about computational efficiency and Python
6465
- Tools like NumPy and Numba allow you do fast numerical computations in Python by "secretly" doing the numerical work behind the scenes in a fast language, like C.
6566
- We will be working with NumPy (but Numba is fantastic and very worthwhile to learn if you do any serious numerical work in Python).
6667
- Both NumPy and Numba came with your installation of Anaconda
68+
<!-- #endregion -->

Python/Module1_GettingStartedWithPython/SiteFormatting.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,62 @@ jupyter:
44
text_representation:
55
extension: .md
66
format_name: markdown
7-
format_version: '1.0'
8-
jupytext_version: 1.0.1
7+
format_version: '1.1'
8+
jupytext_version: 1.1.0-rc0
99
kernelspec:
1010
display_name: Python 3
1111
language: python
1212
name: python3
1313
---
1414

15+
<!-- #region -->
1516
# A Quick Guide to Formatting
1617
This section provides a brief overview of the code formatting style that will be used throughout this text. You are not expected to understand the details of the code, here. This merely provides a guide for what is to come.
1718

1819
Any code that is included in-line within plain text will be formatted distinctly as so: "the variable `x` was updated...". Such items will be distinguished with backticks wherever such formatting is not available. Take for example the following commented line within Python code:
1920

20-
# ```python
21+
```python
2122
# the variable `x` will be updated
22-
# ```
23+
```
2324

2425
Python code will be displayed within distinct, colorized code blocks. These will typically begin with a comment, which is meant to serve as a caption that summarizes the purpose of the code block:
2526

26-
# ```python
27+
```python
2728
# demonstrating a basic for-loop
2829
cnt = 0
2930
for i in range(10):
3031
cnt += 1
3132

3233
#`cnt` is now 10
33-
# ```
34+
```
3435

3536
The symbol `>>>` appears within code blocks to indicate "console-style" code, which distinguishes between code being entered by a user and the resulting output. The purpose of this is that it allows us to easily display the result of a computation without having to rely on calling the `print` function. For instance, the following code assigns the integer `1` to the variable `x`, and then displays the result of `x + 2`:
3637

37-
# ```python
38+
```python
3839
# demonstrating the distinction of
3940
# input and output via >>>
4041

4142
>>> x = 1
4243
>>> x + 2
4344
3
44-
# ```
45+
```
4546

4647
The code blocks throughout a given section of the text should be understood to be persistent even if there is a mix of "pure" code blocks and "console-style" code blocks. For example, a function may be defined at the beginning of a section, and then referenced throughout the rest of that section:
47-
# ```python
48+
```python
4849
# defining an example function
4950
def my_func(x):
5051
return x**2
51-
# ```
52+
```
5253

5354
We can spend some time talking about `my_func` and then see it in action:
54-
# ```python
55+
```python
5556
# demonstrating `my_func`
5657
>>> my_func(10.)
5758
100.
58-
# ```
59+
```
5960

6061
Lastly, the input and output of an iPython console and a Jupyter notebook alike is displayed as follows:
62+
<!-- #endregion -->
6163

6264
```python
6365
2 + 3

0 commit comments

Comments
 (0)