This is not an introduction to Python
Before looking at the examples provided here, be sure to be familiar with the bases of Python 2.7.
If needed, work as much as you can on this website introToPython.org:
- Variables, Strings, and Numbers,
- Lists and Tuples,
- Introducing Functions,
- If Statements,
- While Loops and Input,
- and some external resources.
So what is this about? And why?
In February 2015, some students asked me about the possibility of frequently sharing small and interesting Python programs.
The reason seems to be that some of you want to be able to see what Python can really be used to do, and how does a real world Python program look like (a program designed to solve a concrete problem and not just an example in the lab).
Warning: this is external to the CS101 course
All these examples are NOT related to what will be covered in CS101 lectures and labs, and are NOT related to what will be asked in the lab exams and written exams.
I am sharing all these demos only for interested students who would like to discover a little more.
These examples and demos are here only to show you what Python can do when used cleverly for some examples of scientific computing, problem solving, plotting etc (a list of covered domain is given below).
But reading one or two "real world" Python example from time to time might help you, mainly by triggering your interest.
No need for spending time on each programs, no need for reading the documentation of the modules I used, and no need of asking me endless questions about how I did on thing, why I did another one etc.
But, please, feel free to contact me if needed, or by email at
CS101 at crans dot org(obviously, replace
And that might be true: reading examples can help you
I will really try to produce one or two new programs every week (Friday/Saturday usually).
And you can even send me what you write, I will probably change it a little bit, and upload it here!
Many examples from various domains (which are NOT in the syllabus for CS101)
It currently contains about 45 examples, covering:
- some plotting examples: a demo with 13 different examples (2D and 3D), and one big demo based on one exercise of the tutorial sheet on 2D/3D integral (intersection of two paraboloid) with a the volume being "automatically" computed (with NumPy+MatPlotLib, as the pie/bar charts for the CS101 first lab exam). This other program plots a visual representation of the Riemann rectangles, used in a method for computing integrals. See here for a tutorial on MatPlotLib. Only this part will be shortly studied with you for CS101.
- more examples of plotting: this fun example was my starting point for the CS101 lectures on scientific plotting (XKCD style).
- some simple algorithms and computation (quick exponentation, computations about the factorial and Fibonacci series, list sorting (lab #9), and more recently the sum and product of matrices (lab #10), as studied in CS101 already).
- solutions for the CS101 2nd Mid-Term Exam problem I, problem II (quick exponentation for matrices), problem III (averages and weighted averages).
- example of implementation of the Merge-Sort algorithm, as used in our lecture for CS101 and our 2nd Mid-Term exam.
- bits of linear algebra (demo about inner products, or dimension of span generated by some vectors, as you are studying it in MA102 (with NumPy/SciPy). We used this automatic generator of families of vectors of a certain rank (for a quiz in MA102).
- I also wrote the Gram-Schmidt process (on Rn), and the Gauss elimination algorithm.
- from MA102, some examples of formal determinants (MA102 Sheet 8) or eigen values and eigen vectors (MA102 Sheet 9) (formally computed with SymPy).
- some number related problems (like this one about prime numbers or about large prime factor, as studied in one CS101 lab assignment). See also this solver for polynomials of order 1, 2 and 3.
- some symbolic computations: a 2D line integral (as covered in MA102 part1) and an 2D integral, demonstrating Fubini's theorem (to change the order of integration) (with SymPy).
- numerical solving of a non-linear 1D PDE (the linked page is in French, sorry; but this will studied in your PDE course in 3rd year anyway).
- some black-box optimization using scipy.optimize (based on this excellent tutorial). You will do more of this next year in the MA204 course.
- one machine learning example (with Sklearn, for hand-written digits recognition, with a SVN).
- implementation of one numerical method for computing an integral, the Romberg's method. Another implementation with a recursive function.
- some simple image processing: detection of interesting parts (blobs), computation of the edges of some images, segmentation of an image and one example about entropy (with Skimage). The other module scipy.ndimage provides also some nice features.
- a partial solution for the Pi Day Hackathon I organized at MEC for Pi Day 2015. I also implemented a simple Monte-Carlo method for approximating pi.
- there is also one example of the mayavi 3D plotting module.
- and one example of use of tuples, lists, sets, and dictionaries (CS101 Lab #12).
- a small toy program, playing with strings and caracters, sent by a student.
- I also tried the mpld3 module for this example.
- a small demo of the super() function for easy class inheritance (thanks to one student for sharing her program).
- two demonstrations of how to manually read a gray-scale BMP image, and a colored BMP image.
- two examples of Monte-Carlo method for computing integrals: for 1D integrals or 2D integrals (on a simple domain).
- 3 different examples of Gaussian quadrature for computing 1D integrals.
- two examples of animated plots with the MatPlotLib API for animation (a moving sine wave, and a shrinking star in 2D), based on this tutorial.
The CS101 course is over, but maybe I will still write more examples, from time to time.
Contact me if needed?
- If you want a specific example, please contact me through Moodle, by email, here on this form online.
- If you found any issue in one of the programs shared here, or if you have any suggestion, feel free to open a bug ticket (it's free, easy, and open to anyone without being logged in).
Other resources for the CS101 course
The main reference for you is the CS101 page on the MEC Moodle portal (access restricted).
I also wrote a complete solution for two of the 6 programming project we gave at MEC:
- MEC CS101 : Integrals, the task was to implement various methods for computing definite integrals numericall (in 1D but not only),
- MEC CS101 : Matrices, a project about linear algebra operations and matrices.
/!\ Warning: do not spoil yourself the pleasure of solving these problems by yourself!!
Author | License ?
Except if something else is mentioned, all these files have been written by Lilian Besson and are copyrighted (C) 2015.
All these files are here publicly released, under the conditions of GNU Public License v3.
I might switch to the MIT License, but this does not really change anything for you.