Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

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()

We will use the 3.6.x version. 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 and Live Share Extension Pack.
  • Live Share will be used to share your screen with the teacher in case you are stuck and need help.

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.0 and numpy 3.0 for a second course, and again an environment with Python 3.0 and 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.

Virtual environments can easily be setup with Visual Studio Code, or other IDEs.

A single environment should be 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.

Other walkthroughs

  • MS Windows quick guide for setting up your IDE (DrPython or IDLE) under MS Windows.
  • Work with files quick introduction to working with File Systems in Python.
  • faq Problems which, according to our experience are quite common. We continuously update the list.

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

courses/be5b33kui/tutorials/python.txt · Last modified: 2022/02/10 13:05 by gamafili