Path: blob/main/notebooks/ch-prerequisites/python-and-jupyter-notebooks.ipynb
3855 views
Introduction to Python and Jupyter notebooks
Python is a programming language where you don't need to compile. You can just run it line by line (which is how we can use it in a notebook). So if you are quite new to programming, Python is a great place to start. The current version is Python 3, which is what we'll be using here.
One way to code in Python is to use a Jupyter notebook. This is probably the best way to combine programming, text and images. In a notebook, everything is laid out in cells. Text cells and code cells are the most common. If you are viewing this section as a Jupyter notebook, the text you are now reading is in a text cell. A code cell can be found just below.
To run the contents of a code cell, you can click on it and press Shift + Enter. Or if there is a little arrow thing on the left, you can click on that.
If you are viewing this section as a Jupyter notebook, execute each of the code cells as you read through.
Above we created two variables, which we called a and b, and gave them values. Then we added them. Simple arithmetic like this is pretty straightforward in Python.
Variables in Python come in many forms. Below are some examples.
As well as numbers, another data structure we can use is the list.
Lists in Python can contain any mixture of variable types.
Lists are indexed from 0 in Python (unlike languages such as Fortran). So here's how you access the 42 at the beginning of the above list.
A similar data structure is the tuple.
A major difference between the list and the tuple is that list elements can be changed
whereas tuple elements cannot
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-42d08f1e5606> in <module>
----> 1 a_tuple[5] = 'apple'
TypeError: 'tuple' object does not support item assignment
Also, we can add an element to the end of a list, which we cannot do with tuples.
Another useful data structure is the dictionary. This stores a set of values, each labeled by a unique key.
Values can be any data type. Keys can be anything sufficiently simple (integer, float, Boolean, string). It cannot be a list, but it can be a tuple.
The values are accessed using the keys
New key/value pairs can be added by just supplying the new value for the new key
To loop over a range of numbers, the syntax is
Note that it starts at 0 (by default), and ends at n-1 for range(n).
You can also loop over any 'iterable' object, such as lists
or dictionaries
Conditional statements are done with if, elif and else with the following syntax.
Importing packages is done with a line such as
The numpy package is important for doing maths
We have to write numpy. in front of every numpy command so that it knows how to find that command defined in numpy. To save writing, it is common to use
Then you only need the shortened name. Most people use np, but you can choose what you like.
You can also pull everything straight out of numpy with
Then you can use the commands directly. But this can cause packages to mess with each other, so use with caution.
If you want to do trigonometry, linear algebra, etc, you can use numpy. For plotting, use matplotlib. For graph theory, use networkx. For quantum computing, use qiskit. For whatever you want, there will probably be a package to help you do it.
A good thing to know about in any language is how to make a function.
Here's a function, whose name was chosen to be do_some_maths, whose inputs are named Input1 and Input2 and whose output is named the_answer.
It's used as follows
If you give a function an object, and the function calls a method of that object to alter its state, the effect will persist. So if that's all you want to do, you don't need to return anything. For example, let's do it with the append method of a list.
Randomness can be generated using the random package.
These are the basics. Now all you need is a search engine, and the intuition to know who is worth listening to on Stack Exchange. Then you can do anything with Python. Your code might not be the most 'Pythonic', but only Pythonistas really care about that.