Python

This page provides basic walkthroughs and tutorials for the programing language Python.

We do not cover all aspects of programming in Python. We provide some starting points and expect you find more. We recommend [Wentworth2012] as your main teaching material. The more advanced book [Pilgrim2009] may perhaps be more interesting for experienced programmers

Installing Python

If you are using Linux, Python is most likely already installed. Otherwise, install it using your SW manager (like Synaptic). Python is also quite likely to be already installed in Mac OS X. Try running terminal and write which python. In the case of MS Windows, it will not be installed. You will have to install it.

In a terminal window (Linux or OSX) you can check:

[182] python3.2
Python 3.2.2 (default, Mar 15 2012, 17:46:27) 
[GCC 4.5.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

[53] python3
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can quit by Ctrl-d, or typing exit()

During automated evaluation, at least version 3.10 will be used. Please be aware that on many machines Python 2.7.x is still often the default Python interpreter. Python 3 is not fully compatible with Python 2.x. There is a code converter which makes the possible transition easier. Many important libraries have been already ported to Python 3.

Visual Studio Code

  • Integrated Development Environment (IDE)
  • Programming in VS Code is a crucial part of the PRG course
  • Install Python (as described above) before trying to install VS Code.
  • Then, install VS Code: https://code.visualstudio.com/.
  • Once installed, run VS Code. Click 'Extensions' on the vertical bar on the left side and install Python.
  • If there is ever the need to share your screen with the teacher in case you are stuck and need help, and there is no more practical alternative, the Live Share Extension Pack can also be installed.

Python virtual environments

You might also be interested by python virtual environments that can be used by installing virtualenv, as well as virtualenvwrapper to make it more practical to use.

Virtual environment enable you to create an environment for each assignment, or each course, so that each python environment is independent from the others, and the installed libraries do not conflict, keeping them also from conflicting with your system's python default version (in particular if you are on linux). For exemple, you could create an environment with Python 2.7 and numpy 3.0 for a course, then another environment with Python 3.6 and numpy 3.0 for a second course, and again an environment with Python 3.6 but with numpy 3.2 for a third course. Without virtual environment, this would be difficult to manage (especially the case where you need different versions of the same library for different projects). With virtual environments, it is extremely easy.

After you install both virtualenv and virtualenvwrapper you can create a new environment (you'll need to add parameters if you want the virtual environment to use a different version of python than your system default's, check the documentation of virtualenvwrapper) using:

mkvirtualenv ENVIRONMENT_NAME
Or delete one using:
rmvirtualenv ENVIRONMENT_NAME
Access a virtual environment from a terminal using:
workon ENVIRONMENT_NAME

Once inside the virtual environment, you can use:

pip install LIBRARY_NAME
To install whatever library you need for that project, eventually specifying the version of that library too if needed.

Do not use / “sudo” pip install /, as it'll install it on the host machine, rather than the virtual environment

Virtual environments can easily be run from Visual Studio Code, or other IDEs.

A single environment is enough for all of this course's assignments.

Be careful to never use sudo when installing python libraries from inside a virtual environment, as it will instead install them on your OS' python.

Jupyter Notebooks

While not very useful for this course, as it is not really adapted to the kind of assignments we'll give you, we'd like to present you with a tool that might be useful in your future, or in other courses: Jupyter Notebooks. This is especially interesting to do data analysis, when you try to plot graphs, and might want to instantly see the outcome of your python code and the graphs generated, while being able to quickly edit it if necessary.

PyTest

pytest is a framework for writing tests in Python. (Python includes built-in modules doctest and unittest; but pytest allows you to write tests with a minimal amount of “redundant” code.)

The parts of the documentation you'll probably need the most:

Credits to the 'be5b33prg' course for parts of this tutorial page.

courses/be5b33kui/tutorials/python.txt · Last modified: 2023/04/24 15:56 by gamafili