Before we dive into learning how to program in pyhon, we need to get set up in a way that we can be productive. There are two things that we will need: An editor for writing our programs and an environment to execute them in, i.e. a python interpreter.
Let's start with the interpreter. One possibility would be to just install python and be done with it. That would be fine as long as we don't want to use code that other people have written (python packages, also called libraries), which we will need to install, which, especially on Windows, can quickly turn into a nightmare when packages conflict or we forgot to install some requirement or managed to overwrite something we shouldn't have.
To avoid this, we will use a package manager. This has two advantages. The first is that installing or updating packages will usually only require a single command and all dependencies of the package in question will be installed automatically, while we should get notified about any conflicts. The other advantage is that we can create multiple environments (with different packages installed in each) and switching between them or removing everything associated with one of them is again one command and should leave no traces or clog up our system. In other words, a package manager makes it a lot more difficult to mess something up.
The package manager we will use is called conda. To make it maximally easy, there is a bundle called Anaconda, which includes the conda package manager along with more than 100 commonly used packages (a bunch of which we will be using), sparing us the need to install all of them ourselves. If you're using your own computer, just click here and download Anaconda for your system (click on the symbol for your operating system and on download under Python 3.6 version). For windows you basically just need to run the .exe file and agree to everything and on linux call the downloaded file with bash, but see here for more detailed installation instuctions. If you're using a university managed desktop, then you will need to install anaconda via the software center (this will take quite a while and chances are it will ask you to install a bunch of updates for the other software on your system - be patient).
If you are using Windows, you will need something that functions similar to a linux terminal, which is an interactive text box that will allow us to send (conda) commands and python code to. The best one seems to be cmder, just download the zip-archive ("Download Full") and unpack the folder to where you want this program to be, but make sure it's on the local hard drive, so C:\Users\YourUsername\cmder would be a good place. Once you've done that, right-click on the cmder.exe and choose "Pin to taskbar" so you can call it by clicking on the icon that now appears in the taskbar (a lambda on a green background at the bottom of your screen).
With conda installed, we can create our first environment that we will use. Open up a terminal (run cmder.exe on Windows) and type in conda create --name TheNameYouWant
Whenever we want to use it now, we have to activate the environment. In Linux, you will want to open a tmux window first (just type tmux). Then you can activate the environment by typing activate TheNameYouWant
in Windows or source activate TheNameYouWant
in Linux. With the environment loaded, we can finally start python. You could just type python (and hit enter) and it should open the python interpreter, but we will instead use iPython, which is basically an enhanced version of the interpreter that will colour code what you type in, allow autocomplete, keep your history of inputs and adds a whole range of other neat features. Call it by typing "ipython3". If that works, then exit it by hitting ctrl+d (confirming by entering "y").
Now that python is installed and our environment is activated, we can install two packages that we will need. The first one is jupyter. Jupyter allows to create notebooks, which are basically web pages that run python in the background, allowing to integrate formatted text and executable python code, which is very useful for communicating code and we will rely heavily on jupyter notebooks in this course. To install it, enter conda install jupyter
and when asked confirm by entering "y". The other package we will need adds some extra functionality to the notebooks, install that one with conda install -c conda-forge jupyter_contrib_nbextensions
If that all went through, we can try it out by typing jupyter notebook
, which should automatically open your browser and show you the jupyter start page. In the adress bar (it should say something like http://localhost:8888/tree), replace tree with nbextension and hit enter, which should open another page, where you can select which extensions that we just installed should be active (you can probably also just click on this link: http://localhost:8888/nbextensions). We will need freeze, hide_input and runtools - just click on the little square in front of the names and a check mark should appear. That's it, you can close the browser tab now. Back in the terminal, the jupyter server will still be running, you can stop it by hitting ctrl+c and confirming.
Now let's turn to the editor. Since python scripts are just plain text, it seems like it shouldn't matter much what you use to write them in and that Microsoft Notepad would do, but you might be surprised by how much more productive you will get if different elements of the code (numbers, text, functions, veriables, operators, ...) are displayed in different, aesthetically pleasing colours and how useful it is if your code is automatically checked for errors and can be sent to the python interpreter without having to copy and paste it.
There are a number of highly functional editors out there, but we will use sublime text 3, which you can download here. If you're using a university managed desktop, you will need to use the portable version of sublime, which requires no installation. Just like for cmder, simply unpack the contents of the zip archive to a folder on your local disk, for example to C:\Users\YourUsername\sublime and pin the sublime_text.exe to the taskbar.
Once sublime is installed, we will need a couple of packages for the editor that will make our life easier. To install them, open sublime and press ctrl+shift+p, which should open up a dialogue box. When you then start typing "install", it should show you an option "Install Package Control" - click on it. Now the package manager for sublime is installed, which you can use to install all the other extensions we need. To do so, hit ctrl+shift+p again, type "install" and click on "Package Control: Install Package". This will open up another dialogue box where you can type in the package you want to install.
These are the packages you will need: Anaconda, Git, GitGutter, SendCode, SublimeLinter-pep8, SublimeLinter-pyflakes and SublimeREPL
On Linux, you should be good to go now. On Windows, the last thing left to do is to tell sublime where the cmder.exe lives, so it can send code to the terminal. To do so, hit ctrl+shift+p again and type in "settings" and click on "Preferences: SendCode settings". This will open a new window with the general settings on the left and the ones specific to you on the right - it should just be an empty pair of braces with an empty line in the middle. Replace that empty line with this one (changing YourUsername appropriately):
"conemuc": "C:\\Users\\YourUsername\\cmder\\vendor\\conemu-maximus5\\ConEmu\\ConEmuC.exe"
and save it (ctrl+s).