├── LICENSE.txt
├── README.md
├── data
├── XKCD.png
├── indent.png
└── notebook.png
└── lessons
├── 01-installation.md
├── 02-introduction.md
└── introduction.md
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Attribution 4.0 International
2 |
3 | =======================================================================
4 |
5 | Creative Commons Corporation ("Creative Commons") is not a law firm and
6 | does not provide legal services or legal advice. Distribution of
7 | Creative Commons public licenses does not create a lawyer-client or
8 | other relationship. Creative Commons makes its licenses and related
9 | information available on an "as-is" basis. Creative Commons gives no
10 | warranties regarding its licenses, any material licensed under their
11 | terms and conditions, or any related information. Creative Commons
12 | disclaims all liability for damages resulting from their use to the
13 | fullest extent possible.
14 |
15 | Using Creative Commons Public Licenses
16 |
17 | Creative Commons public licenses provide a standard set of terms and
18 | conditions that creators and other rights holders may use to share
19 | original works of authorship and other material subject to copyright
20 | and certain other rights specified in the public license below. The
21 | following considerations are for informational purposes only, are not
22 | exhaustive, and do not form part of our licenses.
23 |
24 | Considerations for licensors: Our public licenses are
25 | intended for use by those authorized to give the public
26 | permission to use material in ways otherwise restricted by
27 | copyright and certain other rights. Our licenses are
28 | irrevocable. Licensors should read and understand the terms
29 | and conditions of the license they choose before applying it.
30 | Licensors should also secure all rights necessary before
31 | applying our licenses so that the public can reuse the
32 | material as expected. Licensors should clearly mark any
33 | material not subject to the license. This includes other CC-
34 | licensed material, or material used under an exception or
35 | limitation to copyright. More considerations for licensors:
36 | wiki.creativecommons.org/Considerations_for_licensors
37 |
38 | Considerations for the public: By using one of our public
39 | licenses, a licensor grants the public permission to use the
40 | licensed material under specified terms and conditions. If
41 | the licensor's permission is not necessary for any reason--for
42 | example, because of any applicable exception or limitation to
43 | copyright--then that use is not regulated by the license. Our
44 | licenses grant only permissions under copyright and certain
45 | other rights that a licensor has authority to grant. Use of
46 | the licensed material may still be restricted for other
47 | reasons, including because others have copyright or other
48 | rights in the material. A licensor may make special requests,
49 | such as asking that all changes be marked or described.
50 | Although not required by our licenses, you are encouraged to
51 | respect those requests where reasonable. More_considerations
52 | for the public:
53 | wiki.creativecommons.org/Considerations_for_licensees
54 |
55 | =======================================================================
56 |
57 | Creative Commons Attribution 4.0 International Public License
58 |
59 | By exercising the Licensed Rights (defined below), You accept and agree
60 | to be bound by the terms and conditions of this Creative Commons
61 | Attribution 4.0 International Public License ("Public License"). To the
62 | extent this Public License may be interpreted as a contract, You are
63 | granted the Licensed Rights in consideration of Your acceptance of
64 | these terms and conditions, and the Licensor grants You such rights in
65 | consideration of benefits the Licensor receives from making the
66 | Licensed Material available under these terms and conditions.
67 |
68 |
69 | Section 1 -- Definitions.
70 |
71 | a. Adapted Material means material subject to Copyright and Similar
72 | Rights that is derived from or based upon the Licensed Material
73 | and in which the Licensed Material is translated, altered,
74 | arranged, transformed, or otherwise modified in a manner requiring
75 | permission under the Copyright and Similar Rights held by the
76 | Licensor. For purposes of this Public License, where the Licensed
77 | Material is a musical work, performance, or sound recording,
78 | Adapted Material is always produced where the Licensed Material is
79 | synched in timed relation with a moving image.
80 |
81 | b. Adapter's License means the license You apply to Your Copyright
82 | and Similar Rights in Your contributions to Adapted Material in
83 | accordance with the terms and conditions of this Public License.
84 |
85 | c. Copyright and Similar Rights means copyright and/or similar rights
86 | closely related to copyright including, without limitation,
87 | performance, broadcast, sound recording, and Sui Generis Database
88 | Rights, without regard to how the rights are labeled or
89 | categorized. For purposes of this Public License, the rights
90 | specified in Section 2(b)(1)-(2) are not Copyright and Similar
91 | Rights.
92 |
93 | d. Effective Technological Measures means those measures that, in the
94 | absence of proper authority, may not be circumvented under laws
95 | fulfilling obligations under Article 11 of the WIPO Copyright
96 | Treaty adopted on December 20, 1996, and/or similar international
97 | agreements.
98 |
99 | e. Exceptions and Limitations means fair use, fair dealing, and/or
100 | any other exception or limitation to Copyright and Similar Rights
101 | that applies to Your use of the Licensed Material.
102 |
103 | f. Licensed Material means the artistic or literary work, database,
104 | or other material to which the Licensor applied this Public
105 | License.
106 |
107 | g. Licensed Rights means the rights granted to You subject to the
108 | terms and conditions of this Public License, which are limited to
109 | all Copyright and Similar Rights that apply to Your use of the
110 | Licensed Material and that the Licensor has authority to license.
111 |
112 | h. Licensor means the individual(s) or entity(ies) granting rights
113 | under this Public License.
114 |
115 | i. Share means to provide material to the public by any means or
116 | process that requires permission under the Licensed Rights, such
117 | as reproduction, public display, public performance, distribution,
118 | dissemination, communication, or importation, and to make material
119 | available to the public including in ways that members of the
120 | public may access the material from a place and at a time
121 | individually chosen by them.
122 |
123 | j. Sui Generis Database Rights means rights other than copyright
124 | resulting from Directive 96/9/EC of the European Parliament and of
125 | the Council of 11 March 1996 on the legal protection of databases,
126 | as amended and/or succeeded, as well as other essentially
127 | equivalent rights anywhere in the world.
128 |
129 | k. You means the individual or entity exercising the Licensed Rights
130 | under this Public License. Your has a corresponding meaning.
131 |
132 |
133 | Section 2 -- Scope.
134 |
135 | a. License grant.
136 |
137 | 1. Subject to the terms and conditions of this Public License,
138 | the Licensor hereby grants You a worldwide, royalty-free,
139 | non-sublicensable, non-exclusive, irrevocable license to
140 | exercise the Licensed Rights in the Licensed Material to:
141 |
142 | a. reproduce and Share the Licensed Material, in whole or
143 | in part; and
144 |
145 | b. produce, reproduce, and Share Adapted Material.
146 |
147 | 2. Exceptions and Limitations. For the avoidance of doubt, where
148 | Exceptions and Limitations apply to Your use, this Public
149 | License does not apply, and You do not need to comply with
150 | its terms and conditions.
151 |
152 | 3. Term. The term of this Public License is specified in Section
153 | 6(a).
154 |
155 | 4. Media and formats; technical modifications allowed. The
156 | Licensor authorizes You to exercise the Licensed Rights in
157 | all media and formats whether now known or hereafter created,
158 | and to make technical modifications necessary to do so. The
159 | Licensor waives and/or agrees not to assert any right or
160 | authority to forbid You from making technical modifications
161 | necessary to exercise the Licensed Rights, including
162 | technical modifications necessary to circumvent Effective
163 | Technological Measures. For purposes of this Public License,
164 | simply making modifications authorized by this Section 2(a)
165 | (4) never produces Adapted Material.
166 |
167 | 5. Downstream recipients.
168 |
169 | a. Offer from the Licensor -- Licensed Material. Every
170 | recipient of the Licensed Material automatically
171 | receives an offer from the Licensor to exercise the
172 | Licensed Rights under the terms and conditions of this
173 | Public License.
174 |
175 | b. No downstream restrictions. You may not offer or impose
176 | any additional or different terms or conditions on, or
177 | apply any Effective Technological Measures to, the
178 | Licensed Material if doing so restricts exercise of the
179 | Licensed Rights by any recipient of the Licensed
180 | Material.
181 |
182 | 6. No endorsement. Nothing in this Public License constitutes or
183 | may be construed as permission to assert or imply that You
184 | are, or that Your use of the Licensed Material is, connected
185 | with, or sponsored, endorsed, or granted official status by,
186 | the Licensor or others designated to receive attribution as
187 | provided in Section 3(a)(1)(A)(i).
188 |
189 | b. Other rights.
190 |
191 | 1. Moral rights, such as the right of integrity, are not
192 | licensed under this Public License, nor are publicity,
193 | privacy, and/or other similar personality rights; however, to
194 | the extent possible, the Licensor waives and/or agrees not to
195 | assert any such rights held by the Licensor to the limited
196 | extent necessary to allow You to exercise the Licensed
197 | Rights, but not otherwise.
198 |
199 | 2. Patent and trademark rights are not licensed under this
200 | Public License.
201 |
202 | 3. To the extent possible, the Licensor waives any right to
203 | collect royalties from You for the exercise of the Licensed
204 | Rights, whether directly or through a collecting society
205 | under any voluntary or waivable statutory or compulsory
206 | licensing scheme. In all other cases the Licensor expressly
207 | reserves any right to collect such royalties.
208 |
209 |
210 | Section 3 -- License Conditions.
211 |
212 | Your exercise of the Licensed Rights is expressly made subject to the
213 | following conditions.
214 |
215 | a. Attribution.
216 |
217 | 1. If You Share the Licensed Material (including in modified
218 | form), You must:
219 |
220 | a. retain the following if it is supplied by the Licensor
221 | with the Licensed Material:
222 |
223 | i. identification of the creator(s) of the Licensed
224 | Material and any others designated to receive
225 | attribution, in any reasonable manner requested by
226 | the Licensor (including by pseudonym if
227 | designated);
228 |
229 | ii. a copyright notice;
230 |
231 | iii. a notice that refers to this Public License;
232 |
233 | iv. a notice that refers to the disclaimer of
234 | warranties;
235 |
236 | v. a URI or hyperlink to the Licensed Material to the
237 | extent reasonably practicable;
238 |
239 | b. indicate if You modified the Licensed Material and
240 | retain an indication of any previous modifications; and
241 |
242 | c. indicate the Licensed Material is licensed under this
243 | Public License, and include the text of, or the URI or
244 | hyperlink to, this Public License.
245 |
246 | 2. You may satisfy the conditions in Section 3(a)(1) in any
247 | reasonable manner based on the medium, means, and context in
248 | which You Share the Licensed Material. For example, it may be
249 | reasonable to satisfy the conditions by providing a URI or
250 | hyperlink to a resource that includes the required
251 | information.
252 |
253 | 3. If requested by the Licensor, You must remove any of the
254 | information required by Section 3(a)(1)(A) to the extent
255 | reasonably practicable.
256 |
257 | 4. If You Share Adapted Material You produce, the Adapter's
258 | License You apply must not prevent recipients of the Adapted
259 | Material from complying with this Public License.
260 |
261 |
262 | Section 4 -- Sui Generis Database Rights.
263 |
264 | Where the Licensed Rights include Sui Generis Database Rights that
265 | apply to Your use of the Licensed Material:
266 |
267 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right
268 | to extract, reuse, reproduce, and Share all or a substantial
269 | portion of the contents of the database;
270 |
271 | b. if You include all or a substantial portion of the database
272 | contents in a database in which You have Sui Generis Database
273 | Rights, then the database in which You have Sui Generis Database
274 | Rights (but not its individual contents) is Adapted Material; and
275 |
276 | c. You must comply with the conditions in Section 3(a) if You Share
277 | all or a substantial portion of the contents of the database.
278 |
279 | For the avoidance of doubt, this Section 4 supplements and does not
280 | replace Your obligations under this Public License where the Licensed
281 | Rights include other Copyright and Similar Rights.
282 |
283 |
284 | Section 5 -- Disclaimer of Warranties and Limitation of Liability.
285 |
286 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
287 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
288 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
289 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
290 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
291 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
292 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
293 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
294 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
295 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
296 |
297 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
298 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
299 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
300 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
301 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
302 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
303 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
304 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
305 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
306 |
307 | c. The disclaimer of warranties and limitation of liability provided
308 | above shall be interpreted in a manner that, to the extent
309 | possible, most closely approximates an absolute disclaimer and
310 | waiver of all liability.
311 |
312 |
313 | Section 6 -- Term and Termination.
314 |
315 | a. This Public License applies for the term of the Copyright and
316 | Similar Rights licensed here. However, if You fail to comply with
317 | this Public License, then Your rights under this Public License
318 | terminate automatically.
319 |
320 | b. Where Your right to use the Licensed Material has terminated under
321 | Section 6(a), it reinstates:
322 |
323 | 1. automatically as of the date the violation is cured, provided
324 | it is cured within 30 days of Your discovery of the
325 | violation; or
326 |
327 | 2. upon express reinstatement by the Licensor.
328 |
329 | For the avoidance of doubt, this Section 6(b) does not affect any
330 | right the Licensor may have to seek remedies for Your violations
331 | of this Public License.
332 |
333 | c. For the avoidance of doubt, the Licensor may also offer the
334 | Licensed Material under separate terms or conditions or stop
335 | distributing the Licensed Material at any time; however, doing so
336 | will not terminate this Public License.
337 |
338 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
339 | License.
340 |
341 |
342 | Section 7 -- Other Terms and Conditions.
343 |
344 | a. The Licensor shall not be bound by any additional or different
345 | terms or conditions communicated by You unless expressly agreed.
346 |
347 | b. Any arrangements, understandings, or agreements regarding the
348 | Licensed Material not stated herein are separate from and
349 | independent of the terms and conditions of this Public License.
350 |
351 |
352 | Section 8 -- Interpretation.
353 |
354 | a. For the avoidance of doubt, this Public License does not, and
355 | shall not be interpreted to, reduce, limit, restrict, or impose
356 | conditions on any use of the Licensed Material that could lawfully
357 | be made without permission under this Public License.
358 |
359 | b. To the extent possible, if any provision of this Public License is
360 | deemed unenforceable, it shall be automatically reformed to the
361 | minimum extent necessary to make it enforceable. If the provision
362 | cannot be reformed, it shall be severed from this Public License
363 | without affecting the enforceability of the remaining terms and
364 | conditions.
365 |
366 | c. No term or condition of this Public License will be waived and no
367 | failure to comply consented to unless expressly agreed to by the
368 | Licensor.
369 |
370 | d. Nothing in this Public License constitutes or may be interpreted
371 | as a limitation upon, or waiver of, any privileges and immunities
372 | that apply to the Licensor or You, including from the legal
373 | processes of any jurisdiction or authority.
374 |
375 |
376 | =======================================================================
377 |
378 | Creative Commons is not a party to its public
379 | licenses. Notwithstanding, Creative Commons may elect to apply one of
380 | its public licenses to material it publishes and in those instances
381 | will be considered the “Licensor.” The text of the Creative Commons
382 | public licenses is dedicated to the public domain under the CC0 Public
383 | Domain Dedication. Except for the limited purpose of indicating that
384 | material is shared under a Creative Commons public license or as
385 | otherwise permitted by the Creative Commons policies published at
386 | creativecommons.org/policies, Creative Commons does not authorize the
387 | use of the trademark "Creative Commons" or any other trademark or logo
388 | of Creative Commons without its prior written consent including,
389 | without limitation, in connection with any unauthorized modifications
390 | to any of its public licenses or any other arrangements,
391 | understandings, or agreements concerning use of licensed material. For
392 | the avoidance of doubt, this paragraph does not form part of the
393 | public licenses.
394 |
395 | Creative Commons may be contacted at creativecommons.org.
396 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Scientific Python Crash Course
3 |
4 | 
5 |
6 | This is the material for a 20 hours crash course on Scientific Python
7 | that has been split in 5 beginner and 3 advanced
8 | modules. Contributions and feedbacks are welcome. Released under a
9 | [CC-BY 4.0
10 | International](https://creativecommons.org/licenses/by/4.0/legalcode) license.
11 |
12 |
13 |
14 |
15 | ## Material
16 |
17 | ### [1. Installation](lessons/01-installation.md)
18 |
19 |
20 |
21 | This lesson aims at providing the student with a clean development environment,
22 | including Python installation and essential packages (using the [Anaconda]
23 | installer), a decent text editor (e.g. [emacs], [vim], [atom], [notepad++]), a
24 | git command line and a shell. We'll also introduce the Python & [IPython]
25 | shells, the [Jupyter] notebook and explains how to run a python
26 | script from the command line or from inside the [IPython] shell.
27 |
28 | **Keywords**: `anaconda`, `shell`, `notebook`, `script`, `git`, `editor`
29 | **Prerequisites**: None
30 | **Resources**: None
31 |
32 |
33 |
34 |
35 | ### [2. Introduction](lessons/introduction.md)
36 |
37 |
38 |
39 | We introduce here the Python language. Only the bare minimum necessary for
40 | getting started with Numpy and Scipy is addressed here. To learn more about
41 | the language, consider going through the excellent tutorial
42 | https://docs.python.org/tutorial. Dedicated books are also available, such as
43 | http://www.diveintopython.net/.
44 |
45 | **Keywords**: `python`, `types`, `control flow`, `function`, `package`
46 | **Prerequisites**: Installation
47 | **Resources**: [The Python tutorial](https://docs.python.org/3/tutorial/)
48 |
49 |
50 |
51 |
52 | ### [3. Numerical computing (numpy)](https://scipy-lectures.org/intro/numpy/index.html)
53 |
54 |
55 |
56 | This lesson gives an overview of [NumPy], the core library for performant
57 | numerical computing, with support for large, multi-dimensional
58 | arrays and matrices, along with a large collection of high-level mathematical
59 | functions to operate on these arrays.
60 |
61 | **Keywords**: `numpy`, `array`, `dtype`, `shape`, `broadcast`
62 | **Prerequisites**: Introduction
63 | **Resources**: [SciPy Lecture Notes](https://scipy-lectures.org/)
64 |
65 |
66 |
67 |
68 | ### [4. Data visualization (matplotlib)](https://github.com/rougier/matplotlib-tutorial)
69 |
70 |
71 |
72 | In this lesson, we are going to explore [Matplotlib] that is the single most
73 | used Python package for 2D-graphics. It provides both a very quick way to
74 | visualize data from Python and to produce publication-quality figures in many
75 | different formats. We'll cover only most common use cases.
76 |
77 | **Keywords**: `matplotlib`, `figure`, `plot`, `data`
78 | **Prerequisites**: Introduction, Numerical computing
79 | **Resources**: [Scientific Visulization: Python + Matplotlib](https://www.labri.fr/perso/nrougier/scientific-visualization.html)
80 |
81 |
82 |
83 |
84 | ### [5. Scientific computing (scipy)]()
85 |
86 |
87 |
88 | We'll explore the [SciPy] library that contains a large number of independent
89 | modules for optimization, linear algebra, integration, interpolation, special
90 | functions, FFT, signal and image processing, ODE solvers and other tasks common
91 | in science and engineering.
92 |
93 | **Keywords**: `linear algebra`, `signal processing`, `analysis`, `integration`, `solvers`
94 | **Prerequisites**: Numerical Computing
95 | **Resources**: [SciPy Lecture Notes](https://scipy-lectures.org/)
96 |
97 |
98 |
99 |
100 | ### [6. Version Control (git)](https://swcarpentry.github.io/git-novice/)
101 |
102 |
103 |
104 | Version control is the lab notebook of the digital world: it’s what
105 | professionals use to keep track of what they’ve done and to collaborate with
106 | other people. Every large software development project relies on it, and most
107 | programmers use it for their small jobs as well. And it isn’t just for
108 | software: books, papers, small data sets, and anything that changes over time
109 | or needs to be shared can and should be stored in a version control system.
110 |
111 | **Keywords**: `git`, `github`, `clone`, `commit`, `push`, `fork`
112 | **Prerequisites**: Installation
113 | **Resources**: [Software Carpentry](https://software-carpentry.org/)
114 |
115 |
116 |
117 |
118 |
119 | ### [7. Vectorization techniques (numpy)](https://www.labri.fr/perso/nrougier/from-python-to-numpy)
120 |
121 |
122 |
123 | The goal of this lesson is to explain some vectorization techniques that
124 | can drastically improve computation, with several orders of magnitude in
125 | some cases.
126 |
127 | **Keywords**: `Code`, `Problem`, `Spatial`, `Temporal`
128 | **Prerequisites**: Numerical Computing
129 | **Resources**: [From Python to Numpy](https://www.labri.fr/perso/nrougier/from-python-to-numpy)
130 |
131 |
132 |
133 |
134 | ### [8. Interface with C (cython and ctypes)](https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html)
135 |
136 |
137 |
138 | [Cython] is a static compiler for both the Python programming language and the
139 | extended Cython programming language that eases the writing of C extensions.
140 | [Numba] translates Python functions to optimized machine code at runtime (just in
141 | time) using the industry-standard LLVM compiler library.
142 |
143 | **Keywords**: `Optimization`, `Cython`, `Numba`, `Compiler`, `JIT`
144 | **Prerequisites**: Numerical Computing , Vectorization techniques
145 | **Resources**: [SciPy Lecture Notes](https://scipy-lectures.org/)
146 |
147 |
148 |
149 |
150 | ### Bibliography
151 | * [SciPy 1.0: fundamental algorithms for scientific computing in Python](https://www.nature.com/articles/s41592-019-0686-2),
152 | P.Virtanen, R.Gommers & al., Nature Methods, 2020.
153 | * [Array programming with NumPy](https://www.nature.com/articles/s41586-020-2649-2),
154 | C.R. Harris, K.J. Millman & al., Nature, 2020.
155 | * [How to transform code into scientific contribution](https://www.frontiersin.org/articles/10.3389/fninf.2017.00069/full),
156 | N.P. Rougier & F.Benureau, Frontiers in Neuroinformatics, 2018.
157 | * [Ten simple rules for better figures](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833),
158 | N.P. Rougier, M. Droettboom & P. E. Bourne, Plos Computational Biology, 2014.
159 |
160 | ### Copyright notice
161 |
162 | Copyright © 2021 [Nicolas P. Rougier](http://www.labri.fr/perso/nrougier) —
163 | Released under a [CC-BY 4.0 International](https://creativecommons.org/licenses/by/4.0/legalcode) license.
164 | Banner image copyright © Randall Monroe ([XKCD #353](https://xkcd.com/353/)) —
165 | This course has been written in November 2021 using:
166 |
167 |
168 |
169 |
170 |
171 | [Anaconda]: https://www.anaconda.com/
172 | [Emacs]: http://www.emacs.org/
173 | [vim]: https://www.vim.org/
174 | [Atom]: https://atom.io/
175 | [Notepad++]: https://notepad-plus-plus.org/
176 | [IPython]: http://www.ipython.org/
177 | [Jupyter]: http://www.jupyter.org/
178 | [NumPy]: http://www.numpy.org/
179 | [Scipy]: http://www.scipy.org/
180 | [Matplotlib]: http://www.matplotlib.org/
181 | [Cython]: https://cython.org/
182 | [Numba]: https://numba.pydata.org/
183 |
--------------------------------------------------------------------------------
/data/XKCD.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rougier/scipy-crash-course/0e62b6a7ce42c934ce415f784500a59d8cc6534e/data/XKCD.png
--------------------------------------------------------------------------------
/data/indent.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rougier/scipy-crash-course/0e62b6a7ce42c934ce415f784500a59d8cc6534e/data/indent.png
--------------------------------------------------------------------------------
/data/notebook.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rougier/scipy-crash-course/0e62b6a7ce42c934ce415f784500a59d8cc6534e/data/notebook.png
--------------------------------------------------------------------------------
/lessons/01-installation.md:
--------------------------------------------------------------------------------
1 |
2 | ## Installation
3 |
4 |
5 | This lesson aims at providing the student with a clean development environment,
6 | including Python installation and essential packages (using the [Anaconda]
7 | installer), a decent text editor (e.g. [emacs], [vim], [atom], [notepad++]), a
8 | git command line and a shell. We'll also introduce the Python & [IPython]
9 | shells, the [Jupyter] notebook and explains how to run a python
10 | script from the command line or from inside the [IPython] shell.
11 |
12 |
13 |
14 | **Contents**
15 | * [Objectives](#objectives)
16 | * [Installation](#installation) ..............................................................................................................................................
17 |
18 | * [Python, IPython & Jupyter](#python-ipython--jupyter-) ....................................................................................................................
19 |
20 | * [Testing your installation](#testing-your-installation-) .........................................................................................................................
21 |
22 |
23 |
24 |
25 |
26 | ## Objectives
27 |
28 | The primary goals of this lesson are:
29 |
30 | * To ensure you have a clean Python installation (including [Jupyter])
31 | * To install a decent text editor on your system
32 | * To type a few Python lines and to run a python script
33 |
34 | ### Convention
35 |
36 | During this introduction, we'll use three different kind of console, namely
37 | `shell` (or powershell), `Python` and `IPython`. To help you distinguish them,
38 | there will be a small colored icon in front of them:
39 |
40 |
41 |
42 | ``` bash
43 | GNU bash, version 3.2.57(1)-release-(x86_64-apple-darwin18)
44 | $ _
45 | ```
46 |
47 |
48 |
49 | ``` Pycon
50 | Python 3.7.2 (default, Feb 12 2019, 08:15:36)
51 | [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
52 | Type "help", "copyright", "credits" or "license" for more information.
53 | >>> _
54 | ```
55 |
56 |
57 |
58 | ``` IPython
59 | Python 3.7.2 (default, Feb 12 2019, 08:15:36)
60 | Type 'copyright', 'credits' or 'license' for more information
61 | IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.
62 |
63 | In [1]: _
64 | ```
65 |
66 | In the examples above, `>>>` is the Python prompt and does not need to be
67 | typed. For IPython, the prompt is more likely to be something like
68 | `[12]:`. There is also a second prompt (`...`) meaning the previous line is not
69 | ended and needs to be terminated. This is for example the case when you enter a
70 | parenthese or an unbalanced expression (e.g. number of opening parenthesis is
71 | greater than the number of closing parenthesis).
72 |
73 | If you type a bash command in a Python shell or a Python command in a Bash
74 | shell, the console will report an error.
75 |
76 | > **Note**: to exit a Python shell or a regular shell, you can type `exit()` or
77 | type `Control-D`.
78 |
79 |
80 |
81 | ## Installation
82 |
83 | As of today (2019), Python exists mainly in two flavors: Python 2.x
84 | (deprecated) and Python 3.x. On some systems, the 2.x version is already
85 | installed and for some systems, the 3.x version might be installed as well (or
86 | instead). However, we cannot use any of them because we'll need to install
87 | several packages that might interfere with the system packages. We'll thus have
88 | to install our own version using [Anaconda] by Continuum Analytics which is a
89 | free Python distribution (including for commercial use and redistribution) and
90 | includes more than 400 of the most popular Python packages for science, math,
91 | engineering, and data analysis.
92 |
93 | [Anaconda] is available for several architectures:
94 |
95 | * [Anaconda for windows](https://www.anaconda.com/distribution/#windows)
96 | (Python 3.7 version and the 64-Bit Graphical Installer (614.3 MB))
97 | * [Anaconda for linux](https://www.anaconda.com/distribution/#linux)
98 | (Python 3.7 version and the 64-Bit (x86) Installer (652.5 MB)
99 | * [Anaconda for OSX](https://www.anaconda.com/distribution/#macos)
100 | (Python 3.7 version and the 64-Bit Graphical Installer (652.7 MB))
101 |
102 | When asked whether to install Visual Studio, choose yes if you're on Windows.
103 |
104 | Once installation has finished, we'll need to test it.
105 |
106 | > **Note** that If you're using a Windows machine, you'll need to install
107 | > [powershell](https://docs.microsoft.com/en-us/powershell/) that let you
108 | > manage your computer from the command line.
109 |
110 |
111 | You can now open a terminal (or powershell on Windows) and test if you've
112 | access to the `conda` command:
113 |
114 |
115 |
116 | ``` bash
117 | $ conda --version
118 | conda 4.5.12
119 | ```
120 |
121 | Since the installer might not be up to date (depending on when it was build),
122 | you may need to update your anaconda installation:
123 |
124 |
125 |
126 | ``` bash
127 | $ conda update --all
128 | Collecting package metadata: done
129 | Solving environment: done
130 |
131 | ## Package Plan ##
132 |
133 | ...
134 |
135 | Proceed ([y]/n)?
136 |
137 | ...
138 |
139 | Preparing transaction: done
140 | Verifying transaction: done
141 | Executing transaction: done
142 |
143 | $ conda --version
144 | conda 4.6.4
145 | ```
146 |
147 | > **Note**: If the `conda` does not work, you'll need to open the Anaconda
148 | > navigator and to update the packages through the interface. The reason why
149 | > the `conda` does not work might be linked to a `PATH` problem. In such case,
150 | > it might be worth to reboot your machine to see if this solves the problem.
151 |
152 |
153 | ### Git, a distributed version control system
154 |
155 | We now need to install Git which is not related to Python and has not been
156 | installed during the previous step. Depending on your system, it might be
157 | already installed but most probably it is not up to date and this is the reason
158 | why we'll install our own version. Download the version for your system and
159 | proceed with installation:
160 |
161 | * [Windows](https://git-scm.com/downloads/win)
162 | * [Linux](https://git-scm.com/download/linux)
163 | * [Mac](https://git-scm.com/download/mac)
164 |
165 | Once installation has finished, you can type:
166 |
167 |
168 |
169 | ``` bash
170 | $ git --version
171 | 2.20.1
172 | ```
173 |
174 | > **Note**: As for `conda`, this command may not work if your `PATH` has not
175 | > been updated properly. You can try starting a new terminal and/or reboot
176 | > your machine to see if this solves the problem.
177 |
178 |
179 |
180 | ### Code editor
181 |
182 | The last thing we need to install is a decent code editor. You might have one
183 | already installed on your system and if you're faimilar with it, no need to
184 | install another one. Still, you'll need to configure it properly. If you have
185 | no code editor (or if you're not sure you have one), you need to install
186 | one. There is actually a large choice but I will only list here the most
187 | relevant ones:
188 |
189 | * [Atom]
190 | * [Notepad++]
191 | * [Sublime Text]
192 | * [Emacs] (powerful but difficult for a beginner)
193 | * [vim] (powerful but difficult for a beginner)
194 |
195 | Whatever the editor you chose, you will need to configure it to **not** use
196 | tabulations but to insert spaces (e.g. 4) when you press the `tab` key. This is
197 | important because if you don't do it and mix spaces and tabulations, Python
198 | will complain (it is not allowed) and using only tabs is generally a bad
199 | idea. The way to configure this is editor dependent so I'll let you search how
200 | to do it in your preferred editor.
201 |
202 |
203 |
204 |
205 | ## Python, IPython & Jupyter
206 |
207 | You are now ready to start using Python and there are several ways to do that:
208 |
209 | 1. You can start a Python or IPython shell and type some Python commands
210 | 2. You can write a Python script and execute it
211 | 3. You can start the Jupyter notebook and type some Python code in the browser
212 | (and save your code and results in various formats such as PDF or HTML)
213 |
214 | The Python shell can be started using the command `python` or `python3`:
215 |
216 |
217 |
218 | ``` Pycon
219 | $ Python
220 | Python 3.7.2 (default, Feb 12 2019, 08:15:36)
221 | [Clang 10.0.0 (clang-1000.11.45.5)] on darwin
222 | Type "help", "copyright", "credits" or "license" for more information.
223 | >>> _
224 | ```
225 |
226 | Once inside the sell, you can start typing command:
227 |
228 |
229 |
230 | ``` Pycon
231 | >>> print("Hello world!")
232 | Hello world!
233 | ```
234 |
235 |
236 | The IPython shell can be started using the command `ipython` or `ipython3`:
237 |
238 |
239 |
240 | ``` Pycon
241 | $ IPython
242 | Python 3.7.2 (default, Dec 29 2018, 00:00:04)
243 | Type 'copyright', 'credits' or 'license' for more information
244 | IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
245 |
246 | In [1]: _
247 | ```
248 |
249 | It works pretty much like the default Python shell and you can type commands:
250 |
251 |
252 |
253 | ``` Pycon
254 | In [1]: print("Hello world!")
255 | Hello world!
256 | ```
257 |
258 | Both shells allow to enter Python commands, but IPython offers a set of
259 | supplementary commands as well as some goodies such as code completion and
260 | interactive figures. If you don't which one to use, choose IPython (you'll
261 | thank me later).
262 |
263 |
264 | Using the shell is fine for short code snippets, but as soon as you will want
265 | to write longer program, you'll need first to write your code in a text editor
266 | and then you will execute your code. Let me show you with a very simple script.
267 |
268 | Open your text editor and write:
269 |
270 | ```
271 | print("Hello world!")
272 | ```
273 |
274 | and save the file as `hello.py`. The `.py` is the regular file extension used
275 | for Python programs. You are free to use any exension you like, but using `.py`
276 | is a good idea since the operating system can identify the file as being a
277 | Python script.
278 |
279 | Take note on where you saved your file because you'll need to go to this place
280 | in the shell in order to be able to run it (you can also use the absolute path
281 | to execute it but it is less convenient.
282 |
283 |
284 |
285 | ``` Bash
286 | # Replace the path with the path where you saved your script
287 | $ cd ~/GitHub/scipy-crash-course/examples
288 | $ python hello.py
289 | Hello world!
290 | ```
291 |
292 | If you run the above command, Python will terminate as soon as your program
293 | has ended. If you want to stay within the Python interpreter, you'll have to use
294 | the `-i` switch (interactive mode) that tells Python to not exit once the program
295 | has finished.
296 |
297 | The IPython shell allows you to run a script through the magic
298 | command `%run` (there are many [other magic commands](https://ipython.org/ipython-doc/3/interactive/magics.html)).
299 |
300 |
301 |
302 | ```IPython
303 | In [1]: %run hello.py
304 | Hello world!
305 | ```
306 |
307 | Last, but not least, you can use the [Jupyter] notebook that is an open-source
308 | web application that allows you to create and share documents that contain live
309 | code, equations, visualizations and narrative text. It's a very powerful tool
310 | and is used by an increasing number of researcher.
311 |
312 | 
313 |
314 | If you want to get started with Jupyter notebook, you better read the
315 | [tutorial](https://jupyter.readthedocs.io/en/latest/running.html) at
316 | jupiter.org or this nice
317 | [tutorial](https://www.datacamp.com/community/tutorials/tutorial-jupyter-notebook)
318 | at datacamp.
319 |
320 |
321 |
322 |
323 | ## Testing your installation
324 |
325 |
326 | It's now time to check our installation to see if everything is ok. At this
327 | point, no need yet to understand what you're typing but we need to check if
328 | some important packages are present with the proper version.
329 |
330 |
331 | **Checking for numpy**
332 |
333 |
334 |
335 | ``` pycon
336 | >>> import numpy
337 | >>> print(numpy.__version__)
338 | 1.15.2
339 | >>> numpy.test() # This can last a few seconds
340 | ..........................
341 | ```
342 |
343 | **Checking for scipy**
344 |
345 |
346 |
347 | ``` pycon
348 | >>> import scipy
349 | >>> print(scipy.__version__)
350 | 1.1.0
351 | >>> scipy.test() # This can last several minutes
352 | ...........................
353 | ```
354 |
355 | **Checking for matplotlib**
356 |
357 |
358 |
359 | ``` pycon
360 | >>> import matplotlib
361 | >>> print(matplotlib.__version__)
362 | 3.0.0
363 | ```
364 |
365 | **Checking for cython**
366 |
367 |
368 |
369 | ``` pycon
370 | >>> import cython
371 | >>> print(cython.__version__)
372 | 0.28.2
373 | ```
374 |
375 | **Checking for numba**
376 |
377 |
378 |
379 | ``` pycon
380 | >>> import numba
381 | >>> print(numba.__version__)
382 | 0.40.0
383 | >>> numba.test() # This can last several minutes
384 | ...........................
385 | ```
386 |
387 |
388 | For each of these packages, the `x.y.z` version should be equal or greater than
389 | the displayed version. If this is not the case, then maybe you conda
390 | installation is not up to date and needs to be upgraded (go back the
391 | [Installation](#installation) section to see how this can be done.
392 |
393 |
394 |
395 |
396 | ##
397 |
398 | Copyright © 2019 [Nicolas P. Rougier](http://www.labri.fr/perso/nrougier) •
399 | Released under a [CC-BY 4.0 International](https://creativecommons.org/licenses/by/4.0/legalcode) license.
400 |
401 |
402 |
403 | [Anaconda]: https://www.anaconda.com
404 | [Python]: http://www.python.org
405 | [Numpy]: http://www.numpy.org
406 | [Scipy]: http://www.scipy.org
407 | [Matplotlib]: http://matplotlib.org
408 | [IPython]: http://ipython.org
409 | [Jupyter]: http://jupyter.org
410 | [Git]: https://git-scm.com
411 | [Cython]: http://cython.org
412 | [Unicode]: https://en.wikipedia.org/wiki/Unicode
413 | [Emacs]: http://www.emacs.org/
414 | [vim]: https://www.vim.org/
415 | [Atom]: https://atom.io/
416 | [Notepad++]: https://notepad-plus-plus.org/
417 | [Sublime Text]: https://www.sublimetext.com/
418 |
419 |
--------------------------------------------------------------------------------
/lessons/02-introduction.md:
--------------------------------------------------------------------------------
1 |
2 | ## Introduction
3 |
4 | We introduce here the Python language. Only the bare minimum necessary for
5 | getting started with Numpy and Scipy is addressed here. To learn more about
6 | the language, consider going through the excellent tutorial
7 | https://docs.python.org/tutorial. Dedicated books are also available, such as
8 | http://www.diveintopython.net/.
9 |
10 |
11 |
12 | **Contents**
13 | * [Objectives](#objectives)
14 | * [First steps ](#first-steps-) ..............................................................................................................................................
15 | * [Types & variables]() ..................................................................................................................................
16 | * [Control flow]() ...........................................................................................................................................
17 | * [Functions]() ...............................................................................................................................................
18 | * [Modules]() .................................................................................................................................................
19 | * [Input / Output]() ........................................................................................................................................
20 | * [Exceptions]() .............................................................................................................................................
21 |
22 |
23 |
24 |
25 | ## Objectives
26 |
27 | The primary goals of this lesson are:
28 |
29 | * To introduce Python types & manipulations
30 | * To start programming moderately complex scripts
31 | * To be able to read and write files
32 |
33 |
34 |
35 |
36 | ## First steps
37 |
38 | Now it's time to experience a little bit with Python. Let's start with simple
39 | arithmetic operations because Python can be used as a regular calculator with
40 | standard arithmetic operations (addition, subtraction, multiplication,
41 | division, etc.)
42 |
43 | #### Addition
44 |
45 | ```pycon
46 | >>> 2 + 3
47 | 5
48 | ```
49 |
50 | #### Subtraction
51 |
52 | ```pycon
53 | >>> 11 - 3
54 | 8
55 | ```
56 |
57 | #### Multiplication
58 |
59 | ```pycon
60 | >>> 3 * 4
61 | 12
62 | ```
63 |
64 | #### Division
65 |
66 | ```pycon
67 | >>> 11 / 5
68 | 2.2
69 | ```
70 |
71 | #### Integer division
72 |
73 | ```pycon
74 | >>> 11 // 5
75 | 2
76 | ```
77 |
78 | #### Modulo operation
79 |
80 | ```pycon
81 | >>> 11 % 5
82 | 1
83 | ```
84 |
85 | #### Power
86 |
87 | ```pycon
88 | >>> 2**3
89 | 8
90 | ```
91 |
92 | Note that you cannot have spaces between digits of a number:
93 |
94 | ```pycon
95 | >>> 1 0 + 2
96 | File "
299 | ## Exercises
300 |
301 | Before moving to the [next lesson](02-introduction.md), here are some simple
302 | exercises that should take you only a few minutes to solve. If you want the
303 | solution, just type these expression in a Python console.
304 |
305 |
306 | ### Find the type of the following expressions
307 |
308 | ```
309 | .0
310 | -1
311 | 1,
312 | 'float(4) + 5'
313 | 1e2
314 | 1j
315 | math.nan
316 | ```
317 |
318 | ### Are these legal Python expressions?
319 |
320 | ```
321 | 1 + 1 == 2
322 | 1 = 2
323 | 1 + 1i
324 | 1 <- 2
325 | 0.+.0
326 | 3***3
327 | 3 <<2>> 3
328 | ```
329 |
330 | ### Find the result of the following expressions
331 |
332 | ```
333 | 1.+.1
334 | 0b1+0xb1
335 | (1,)*3
336 | 1e1000 - 1e1000
337 | 'abc'*3
338 | 3 or 10
339 | 3 <2 > 3
340 | ```
341 |
342 |
343 |
344 |
345 |
346 |
347 | ## Basic types
348 |
349 |
350 |
351 |
352 | Beside being a convenient calculator, Python is also (and mostly) a powerful
353 | programming language with an elegant and intuitive syntax. Furthermore, you
354 | have to know that Python is an interpreted langage, meaning each time you enter
355 | a set of instructions, they need to be intepreted by the Python interpreter. This
356 | can make Python quite slow in some situation but we'll later how to overcome most
357 | of Python slowness.
358 |
359 | ### Variables
360 |
361 | Until now, we have been playing in the console, throwing some expressions in
362 | the interpreted and checked the result. Problem is that those expression cannot
363 | be re-used. It's thus time to save us some trouble and assign those expressions
364 | to variables. This can be done quite naturally.
365 |
366 | ``` python
367 | >>> width = 1
368 | >>> height = 2
369 | ```
370 |
371 | What is really cool though is that you can assign several variables at once:
372 |
373 | ``` python
374 | >>> width, height = 2,1
375 | >>> width
376 | 1
377 | >>> height
378 | 2
379 | ```
380 |
381 | However, you cannot refer a new variable on the same line
382 |
383 | ``` python
384 | >>> width, height = 2, 2*width
385 | Traceback (most recent call last):
386 | File "
735 | Click to see solution
736 |