|
| 1 | +--- |
| 2 | +title: Introduction to Python |
| 3 | +teaching: 15 |
| 4 | +exercises: 0 |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::::: objectives |
| 8 | + |
| 9 | +- Examine the Python interpreter |
| 10 | +- Recognize the advantage of using the Python programming language |
| 11 | +- Understand the concept and benefits of using notebooks for coding |
| 12 | + |
| 13 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 14 | + |
| 15 | +:::::::::::::::::::::::::::::::::::::::: questions |
| 16 | + |
| 17 | +- Why learn Python? |
| 18 | +- What are Jupyter notebooks? |
| 19 | + |
| 20 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 21 | + |
| 22 | +## Introducing the Python programming language |
| 23 | + |
| 24 | +Python is a general purpose programming language. It is an interpreted language, |
| 25 | +which makes it suitable for rapid development and prototyping of programming segments or complete |
| 26 | +small programs. |
| 27 | + |
| 28 | +Python's main advantages: |
| 29 | + |
| 30 | +- Open source software, supported by [Python Software |
| 31 | + Foundation](https://www.python.org/psf/) |
| 32 | +- Available on all major platforms (Windows, macOS, Linux) |
| 33 | +- It is a good language for new programmers to learn due to its straightforward, |
| 34 | + object-oriented style |
| 35 | +- It is well-structured, which aids readability |
| 36 | +- It is extensible (i.e. modifiable) and is supported by a large community who |
| 37 | + provide a comprehensive range of 3rd party packages |
| 38 | + |
| 39 | +## Interpreted vs. compiled languages |
| 40 | + |
| 41 | +In any programming language, the code must be translated into "machine code" |
| 42 | +before running it. It is the machine code which is executed and produces |
| 43 | +results. In a language like C++ your code is translated into machine code and |
| 44 | +stored in a separate file, in a process referred to as **compiling** the code. |
| 45 | +You then execute the machine code from the file as a separate step. This is |
| 46 | +efficient if you intend to run the same machine code many times as you only have |
| 47 | +to compile it once and it is very fast to run the compiled machine code. |
| 48 | + |
| 49 | +On the other hand, if you are experimenting, then your |
| 50 | +code will change often and you would have to compile it again every time before |
| 51 | +the machine can execute it. This is where **interpreted** languages have the |
| 52 | +advantage. You don't need a complete compiled program to "run" what has been |
| 53 | +written so far and see the results. This rapid prototyping is helped further by |
| 54 | +use of a system called REPL. |
| 55 | + |
| 56 | +## REPL |
| 57 | + |
| 58 | +**REPL** is an acronym which stands for Read, Evaluate, Print and Loop. |
| 59 | + |
| 60 | +REPL allows you to write single statements of code, have them executed, and if |
| 61 | +there are any results to show, they are displayed and then the interpreter loops |
| 62 | +back to the beginning and waits for the next program statement. |
| 63 | + |
| 64 | +{alt='Python\_Repl'} |
| 65 | + |
| 66 | +In the example above, two variables `a` and `b` have been created, assigned to values |
| 67 | +`2` and `3`, and then multiplied together. |
| 68 | + |
| 69 | +Every time you press <kbd>Return</kbd>, the line is interpreted. The assignment statements don't produce any |
| 70 | +output so you get only the standard `>>>` prompt. |
| 71 | + |
| 72 | +For the `a*b` statement (it is more of an expression than a statement), because |
| 73 | +the result is not being assigned to a variable, the REPL displays the result of |
| 74 | +the calculation on screen and then waits for the next input. |
| 75 | + |
| 76 | +The REPL system makes it very easy to try out small chunks of code. |
| 77 | + |
| 78 | +You are not restricted to single line statements. If the Python interpreter |
| 79 | +decides that what you have written on a line cannot be a complete statement it |
| 80 | +will give you a continuation prompt of `...` until you complete the statement. |
| 81 | + |
| 82 | +## Introducing Jupyter notebooks |
| 83 | + |
| 84 | +[**Jupyter**](https://jupyter.org/) originates from IPython, an effort to make Python |
| 85 | +development more interactive. Since its inception, the scope of the project |
| 86 | +has expanded to include **Ju**lia, **Pyt**hon, and **R**, so the name was changed to "Jupyter" |
| 87 | +as a reference to these core languages. Today, Jupyter supports even more |
| 88 | +languages, but we will be using it only for Python code. Specifically, we will |
| 89 | +be using **Jupyter notebooks**, which allows us to easily take notes about |
| 90 | +our analysis and view plots within the same document where we code. This |
| 91 | +facilitates sharing and reproducibility of analyses, and the notebook interface |
| 92 | +is easily accessible through any web browser. Jupyter notebooks are started |
| 93 | +from the terminal using |
| 94 | + |
| 95 | +```bash |
| 96 | +$ jupyter notebook |
| 97 | +``` |
| 98 | + |
| 99 | +Your browser should start automatically and look |
| 100 | +something like this: |
| 101 | + |
| 102 | +{alt='Jupyter\_notebook\_list'} |
| 103 | + |
| 104 | +When you create a notebook from the *New* option, the new notebook will be displayed in a new |
| 105 | +browser tab and look like this. |
| 106 | + |
| 107 | +{alt='Jupyter\_notebook'} |
| 108 | + |
| 109 | +Initially the notebook has no name other than 'Untitled'. If you click on 'Untitled' you will be |
| 110 | +given the option of changing the name to whatever you want. |
| 111 | + |
| 112 | +The notebook is divided into **cells**. Initially there will be a single input cell marked by `In [ ]:`. |
| 113 | + |
| 114 | +You can type Python code directly into the cell. You can split the code across |
| 115 | +several lines as needed. Unlike the REPL we looked at before, the code is not |
| 116 | +interpreted line by line. To interpret the code in a cell, you can click the |
| 117 | +*Run* button in the toolbar or from the *Cell* menu option, or use keyboard |
| 118 | +shortcuts (e.g., <kbd>Shift</kbd>\+<kbd>Return</kbd>). All of the code in that cell will then be |
| 119 | +executed. |
| 120 | + |
| 121 | +The results are shown in a separate `Out [1]:` cell immediately below. A new input |
| 122 | +cell (`In [ ]:`) is created for you automatically. |
| 123 | + |
| 124 | +{alt='Jupyter\_notebook\_cell'} |
| 125 | + |
| 126 | +When a cell is run, it is given a number along with the corresponding output |
| 127 | +cell. If you have a notebook with many cells in it you can run the cells in any |
| 128 | +order and also run the same cell many times. The number on the left hand side of |
| 129 | +the input cells increments, so you can always tell the order in which they were |
| 130 | +run. For example, a cell marked `In [5]:` was the fifth cell run in the sequence. |
| 131 | + |
| 132 | +Although there is an option to do so on the toolbar, you do not have to manually |
| 133 | +save the notebook. This is done automatically by the Jupyter system. |
| 134 | + |
| 135 | +Not only are the contents of the `In [ ]:` cells saved, but so are the `Out [ ]:` cells. |
| 136 | +This allows you to create complete documents with both your code and the output |
| 137 | +of the code in a single place. You can also change the cell type from |
| 138 | +Python code to Markdown using the *Cell > Cell Type* option. [**Markdown**](https://en.wikipedia.org/wiki/Markdown) is |
| 139 | +a simple formatting system which allows you to create documentation for your |
| 140 | +code, again all within the same notebook structure. |
| 141 | + |
| 142 | +The notebook itself is stored as specially-formatted text file with an `.ipynb` |
| 143 | +extension. These files can be opened and run by others with Jupyter installed. This allows you to |
| 144 | +share your code inputs, outputs, and |
| 145 | +Markdown documentation with others. You can also export the notebook to HTML, PDF, and |
| 146 | +many other formats to make sharing even easier. |
| 147 | + |
| 148 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 149 | + |
| 150 | +- Python is an interpreted language |
| 151 | +- The REPL (Read-Eval-Print loop) allows rapid development and testing of code segments |
| 152 | +- Jupyter notebooks builds on the REPL concepts and allow code results and documentation to be maintained together and shared |
| 153 | +- Jupyter notebooks is a complete IDE (Integrated Development Environment) |
| 154 | + |
| 155 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 156 | + |
| 157 | + |
0 commit comments