Python is a versatile and easy-to-learn programming language. It has been in existence for almost 30 years, but it remained quite confidential for many years and is now a big success—to the point of being one of the most widely taught programming languages today. The main advantage of Python is its simplicity and time saving for the user: with Python, I achieve in one day what I would program in three days in Java and a week in C. Python allows a significant gain of productivity.
Python is an open source software, and it is available for free. It runs on virtually all existing operating systems (Linux PC, Windows PC, Mac, Android, etc.). There are historically two versions of Python: version 2.x (no longer supported but still used by old programs) and version 3.x (currently supported and recommended). Owlready requires version 3.x, so we’ll use this one in this book. However, the differences between the two versions are minimal.
In this chapter, we will quickly introduce the basics of the Python language and its syntax. However, if you have no programming skill yet, we advise you to first consult a book entirely devoted to learning Python. On the contrary, if you already know the Python language, you can go directly to section 2.11 for installing Owlready.
2.1 Installing Python
Under Linux, almost all distributions offer packages for Python (often these packages will even be already installed). You can check that they are present in the package manager of your distribution and install the package python3 if necessary. Also, install the python3-pip and python3-idle packages if your distribution distinguishes them from the main python3 package.
On Windows, it is necessary to install Python. You can download it from the following address:
On Mac OS, Python is probably already installed; you can verify it by running the command “python3 -v” in a terminal. Otherwise, please install it from the preceding website.
2.2 Starting Python
To program in Python, you can either use an integrated development environment (IDE) or use a text editor and a terminal. If you’re new to Python, the first option is probably the simplest; we suggest the IDLE environment that is usually installed with Python 3.
The “shell” mode, in which the computer interprets one by one the lines of code entered by the programmer, as they are entered. This mode is convenient for performing quick tests. The default “Shell” window opened by IDLE corresponds to this mode (see the following example). The “>>>” sign at the beginning of the line is Python’s command prompt: the interpreter prompts you to enter a new line of code.
Attention, in “shell” mode, the lines of code entered are not saved and will be lost when closing the terminal or IDLE!
The “program” mode, in which the user writes a multiline program, and then the computer executes the entire program. This mode allows you to perform complex programs. With IDLE, you can create a new program with the File ➤ New file menu. A new window will appear, in which you will write the program (see the following example). The file will then be saved (with the extension .py) and can be executed with the Run ➤ Run module menu (or by pressing the F5 key).
To have a “shell” mode, execute the command “python3” in the terminal:
To run a program, run the command “python3 file_name.py” in the terminal (obviously replacing file_name.py with the name of the file where you saved your program, with the path if necessary).
This is an “end of command” prompt. As before, the “...” should not be entered.
The first line just indicates the filename; it does not have to be entered in the program.
2.3 Syntax
2.3.1 Comments
2.3.2 Writing on screen
2.3.3 Help
Then, in the “shell” mode, you may exit the man page by pressing the “Q” key on the keyboard.
2.3.4 Variables
A variable is a name to which a value is associated. Often, the value will only be known when the program is executed (e.g., when it is the result of a calculation).
The name of a variable must start with a letter or an underscore “_”, and it can contain letters, numbers, and underscores. Python 3 accepts accented characters in variable names, but spaces are forbidden.
2.3.5 Indentation
In addition, it is recommended that you do not mix spaces and tabs when indenting Python programs.
2.4 Main datatypes
2.4.1 Integer (int) and floating-point numbers (float)
Integers are numbers without a decimal part. There are no limits to integer values in Python.
In Python, floats actually have a precision equivalent to “double” numbers found in many other programming languages (including C, C++, and Java).
Be careful, 10.0 is a float, while 10 is an integer.
Algebraic operations | Examples |
---|---|
Addition | >>> 2 + 2 4 |
Subtraction | >>> 4 - 2 2 |
Multiplication | >>> 3 * 4 12 |
Division | >>> 10 / 3 3.3333333333333 |
Integer division | >>> 10 // 3 3 |
Power | >>> 3 ** 2 9 |
2.4.2 Booleans (bool)
Booleans can take two values, which are written True (true, integer value 1) and False (false, integer value 0) in Python.
2.4.3 Character strings (str)
Special characters | Escape codes |
---|---|
Line break |
|
Tab |
|
Backslash | \ |
Simple quote | ' |
Double quote | " |
In particular, on Windows, backslashes in filenames and paths must be doubled, for example, “C:\directory\file.py”.
Single quotes can also be used for long character strings.
String operations | Examples |
---|---|
Get the length of a string (= the number of characters) | >>> s = “Goodbye” >>> len(s) 7 |
Get a character in a string (be careful, the first character is zero and not one; negative numbers are counted from the end) | >>> s[0] “G” # First character >>> s[-1] "e" # Last character |
Get a part of the string | >>> s[0:4] "Good" |
Find if a string is included in another | >>> s.find("bye") 4 # Found in position 4 # (return -1 if not found) |
Search from the end of the string (R stands for right) | >>> s.rfind("o") 2 # Found in position 2 # (return -1 if not found) |
Split a string according to a separator | >>> "alpha;beta;gamma".↲ split(";") ["alpha", "beta", "gamma"] |
Cut a string according to white spaces (spaces, line breaks, and tabs) | >>> "alpha beta gamma".↲split() ["alpha", "beta", "gamma"] |
Replace a part of a string by another string | >>> "Come here!".↲replace("here", "there") "Come there!" |
Concatenate two strings (= put them end to end); be careful you have to add a space if you want one | >>> "JB" + "LAMY" "JBLAMY" |
Format a string with values | >>> last_name = "LAMY" >>> first_name = "JB" >>> "Hello %s!" %↲ first_name "Hello JB!" >>> "Hello %s %s!" %↲ (first_name, last_name) "Hello JB LAMY!" >>> rate = 90 >>> "Success rate: %s %%"↲% rate "Success rate: 90 %" |
2.4.4 Lists (list)
In a list of n elements, the elements are numbered from zero to n − 1. By convention, the lists often receive a plural variable name, for example, “animals” for a list of animals.
List operations | Examples |
---|---|
Create a list | >>> animals = ["elephant", ... "giraffe", ... "rhinoceros", ... "gazelle"] |
Get the length of a list (= the number of elements) | >>> len(animals) 4 |
Get an element from the list (be careful, lists are numbered from zero and not one) | >>> animals[0] "elephant" # First >>> animals[-1] "gazelle" # Last |
Get a part of the list | >>> animals[0:2] ["elephant", "giraffe"] |
Add an element at the end | >>> animals.append("lion") |
Add an element to a given position (0: first position, etc.) | >>> animals.insert(0, "lynx") |
Concatenate two lists | >>> [1, 2] + [3, 4, 5] [1, 2, 3, 4, 5] |
Remove a given element | >>> animals.↲remove("gazelle") |
Remove the element at a given position | >>> del animals[-2] |
Find if an element is present in a list | >>> "lion" in animals True |
Sort a list (ascending/alphabetical order by default) | >>> animals.sort() |
Get the highest element from a list, or the lowest | >>> max([2, 1, 4, 3]) 4 >>> min([2, 1, 4, 3]) 1 |
2.4.5 Tuples (tuple)
2.4.6 Dictionaries (dict and defaultdict)
In the previous example, the keys are “fruit”, “apple”, and “orange”, and the values are the definitions. Each key has one and only one value.
The keys of a dictionary must be immutable (i.e., nonmodifiable). Therefore, we cannot use a list as a key (a tuple is commonly used instead).
Dict operations | Examples |
---|---|
Get the number of keys (or values) in the dictionary | >>> len(my_dict) 3 |
Get the value associated with a key | >>> my_dict["apple"] "a fleshy fruit with a red or green skin" |
Add or modify the value for a given key | >>> my_dict["clef"] = "value" |
Delete a key (and its associated value) | >>> del my_dict["clef"] |
Search if a key is present in the dictionary | >>> "apple" in my_dict True |
Recover all the keys | >>> for key in my_dict: ... or >>> keys = list(my_dict.↲keys()) |
Recover all the values | >>> for value in my_dict.↲values(): ... or >>> values = list(my_dict.↲values()) |
Collect all (keys, values) pairs (as tuples) | >>> for key, value in↲my_dict.items(): ... or >>> pairs = list(my_dict.↲items()) |
Python also offers a default dictionary, called defaultdict, which is often useful. It is defined in the collections module (we will see modules later; in the following example, the first line corresponds to the import of the module; see 2.10.1). When you get a value from a default dictionary and the key is not present in the dictionary, it is automatically added with a default value. When it is created, the defaultdict takes a parameter that is the default datatype (it can be a datatype, a function, or a class, which we will see later).
2.4.7 Sets (set)
Note that the duplicate (the second 1) has been removed.
The add() method allows you to add an element to a set (it replaces the append() method of lists) and the remove() method to remove an element.
Classical set operations (union, intersection, etc.) are available via methods and operators (“&” for the intersection, “|” for the union). Immutable sets (frozenset) are used as keys in dictionaries, instead of sets. They are to sets what tuples are to lists.
2.4.8 Files (open)
“r” to read a text file (default value)
“w” to write a text file
“rb” to read a binary file
“wb” to write a binary file
File operations | Examples |
---|---|
Read the whole content of the file, as a string | >>> content = my_file.read() |
Write to a file | >>> my_file.write("content") |
Close the file (automatically called when the file object is destroyed by Python) | >>> my_file.close() |
2.4.9 Conversion between datatypes
Convert to | Syntax |
---|---|
Integer | int(x) |
Float | float(x) |
Boolean | bool(x) |
List | list(x) |
Tuple | tuple(x) |
Set | set(x) |
Immutable set | frozenset(x) |
Dictionary | dict(x) # x is of the form [(key1, value1), (key2, value2)...] |
String | str(x) # String for displaying to the user repr(x) # String for displaying to the programmer |
2.5 Conditions (if)
elif is the contraction of else and if. The “elif” and “else” parts are optional, and several “elif” parts may be present. The indentation (i.e., the white space at the beginning of the line) is important because it indicates where the condition ends. The number of spaces is the choice of the programmer but must remain constant, and it is recommended to avoid mixing space characters and tabs.
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
== (equal to, not to be confused with the simple “=” used for defining variables)
!= (different from)
is (test the identity between two objects)
2.6 Loops (for)
The continue instruction interrupts the current iteration and immediately moves to the next element. The break instruction interrupts the loop and exits immediately from the loop. Finally, the else part is executed only if the loop has gone to the end (i.e., it has not been interrupted by break). Of course, the presence of continue, break, and else is not mandatory in a given loop.
Be careful, the range() function of Python has nothing to do with the “range” of an OWL property, which we will see later!
The lists are not paired. In this case, we will use nested loops, as in the following example:
The lists are paired, two by two (or three by three, etc., that is to say that the first element of list 1 is associated with the first element of list 2, the second element of list 1 with the second element in list 2, etc.). The zip() function allows you to loop on two (or more) paired lists. In the following example, we have a list of animals and a list of environments paired, that is, animal #1 goes with environment #1, animal #2 with environment #2, and so on:
2.7 Generators
A generator makes it possible to browse a series of elements (in the manner of a list); however, it does not store in memory all the elements like a list: the generator produces the elements one by one, and these must be immediately processed (e.g., using a loop). The generator therefore allows a gain in performance, especially when working on large volumes of data. This is why a number of Owlready methods return generators and not lists.
2.8 Functions (def)
Functions can receive multiple parameters, and each can have a default value.
The return statement indicates the return value of the function, and interrupts it.
Note that the function parameters are not typed. That’s why in the previous example we were able to use our twofold() function on both an integer and a string.
2.9 Classes (class)
2.9.1 Classes and instances
Classes are the basis of object-oriented programming. A class represents a template for creating objects, for example, we may have a class for creating animals or books. A class can also be seen as a general category of objects, for example, a “book” is a general category, and many different books exist with different titles, authors, and so on.
By convention, class names always start with a capital letter (e.g., “Book”). The class defines the available properties for each object of this class (e.g., for the class Book: title, author, and price) and the methods that can be applied to each object (e.g., for the class Book: format a book citation).
The class will then create objects of the class, called “instances”, for example, “The Lord of the Rings” and “Nine Princes in Amber” will be two instances of the same “Book” class. The class therefore makes it possible to “factorize” the part common to the instances: the property definitions and the methods, while the values of the properties are specific to each instance.
In Python, classes are created with the class statement. The methods are created inside classes with the def statement (as for functions); the first parameter represents the object on which the method is applied (it is called self by convention; it is equivalent to the keyword this in Java or C++ but appears explicitly in the method parameters). Attributes are not typed, just like variables. They are defined by giving them a value, with the syntax “self.attribute_name = value”.
In the body of methods, the “self” active object must always be specified when one wants to obtain or modify its attributes (self.attribute) or to call its methods (self.method(parameters...)).
__init__() is a special method called a “constructor”. If present, the constructor is automatically called when a new instance is created. The constructor can receive parameters, whose values will be given when the instance is created.
In the previous definition, we defined the Book class from the object class, which is the most general class in Python.
2.9.2 Inheritance
Inheritance is a fundamental mechanism in object-oriented programming. This mechanism allows you to create new classes that share a similar blueprint to a given class, that is to say, to define subcategories within a class. For example, comics are a particular subcategory of books: the Comic class is a subclass that inherits from Book. The general class (here, Book) is called the “superclass” or “parent class”, and the more specific class (here, Comic) is called the “subclass” or the “child class”.
The child class inherits all the attributes and methods of its parent class(es): just like the instances of the Book class, those of the Comic class have a title, an author, and a price (attributes), and it is possible to format a citation (method). However, the child class may have additional attributes and methods. For example, a comic book is characterized by its author (or scriptwriter) but also by its illustrator: we can therefore add an “illustrator” attribute to the Comic class. Inheritance makes it possible to “factorize” the source code and to simplify it by avoiding repeating the attributes and methods common to the parent class and its children classes.
The constructor method __init__() and the format_citation() method have been redefined in the Comic child class. The new constructor definition supports the illustrator attribute and delegates to the parent class method for managing the title, author, and price attributes.
Note that we can call the format_citation() method without knowing if the object on which we call it is a Book or a Comic. Python will automatically choose the right method, depending on the class of the object. This mechanism is called polymorphism.
Python also allows multiple inheritance: several parent classes can be given when defining a child class, separated by commas.
2.9.3 Special method names
__init__(self, parameters...): Constructor
__del__(self): Destructor
__repr__(self): Returns a string for displaying to the programmer
__str__(self): Returns a string for displaying to the final user
2.9.4 Functions and operators for object-oriented programming
object.__class__ returns the class or the type of an object, for example:
isinstance(object, Class) tests whether the given object belongs to the given class (including child classes, grandchild, etc.), for example:
issubclass(Class, parent_class) tests whether the given class inherits from parent_class, for example:
hasattr(object, attribute_name) tests whether the object has an attribute named attribute_name.
getattr(object, attribute_name) returns the value of the attribute named attribute_name for the object.
setattr(object, attribute_name, value) defines the value of the attribute named attribute_name for the object.
delattr(object, attribute_name) deletes the attribute named attribute_name from the object.
These methods are particularly useful for introspection, that is, for manipulating objects in a generic way, without knowing their class or their attributes.
2.10 Python modules
Python modules define additional functions and classes in a specific domain (such as mathematics, bioinformatics, 3D graphic, etc.). Owlready2 is an example of Python module. The functions and classes contained in these modules are not available by default in Python; it is mandatory to import the corresponding modules before accessing and using them.
2.10.1 Importing a module
- 1.Importation of the module with its name. With this method, it is necessary to mention the name of the module followed by a “.” in front of each of the functions and classes of the module. Here is an example with the math module:>>> import math>>> math.cos(0.0)1.0
- 2.
Import the contents of the module. With this method, the functions and classes of the module can be used directly, without having to mention the name of the module at each call. On the other hand, if several modules define functions or classes having the same name, this could be problematic: in this case, the last import will overwrite the previous one. Here is another example with the math module:
The Python language includes a large number of “standard” modules, which are installed with Python itself. The official Python documentation describes each of these modules; it is available online at the following address:
https://docs.python.org/3/py-modindex.html
Other modules can be installed from PyPI (Python Package Index), available at
2.10.2 Installing additional modules
2.11 Installing Owlready2
Owlready version 2 can be installed from the Internet with the “pip3” tool; the corresponding module is called “owlready2” (be careful not to forget the version number 2).
In addition, Owlready offers a version optimized in Cython, a language derived from Python compiling in code C. In order to benefit from this optimized version, it is necessary to install beforehand the “cython” module. However, if the installation of Cython went wrong, or if you do not have a C compiler (especially on Windows), you can install Owlready without Cython, at the price of (slightly) reduced performances when loading ontologies.
Finally, the following Python modules will also be used in the rest of the book: “Flask”, “MyGene”, and “RDFlib”.
2.11.1 Installing Owlready2 from terminal
2.11.2 Installing Owlready2 from IDLE or Spyder (or any Python shell)
2.11.3 Manual installation of Owlready2
- 1.
Download the compressed sources from PyPI: https://pypi.org/project/Owlready2/#files.
- 2.
Decompress the compressed sources, for example, under “C:” under Windows.
- 3.
The source directory is named “Owlready2-0.xx” where “xx” is the version number (e.g., “Owlready2-0.25”). Rename this directory as “owlready2”, for example, “C:owlready2”.
- 4.Add the directory containing the source directory (“C:” in our example) in your PYTHONPATH; this can be done in Python as follows (NB: do not forget to double any backslash!):>>> import sys>>> sys.path.append("C:\")
- 5.
You can now import Owlready2!
2.12 Summary
In this chapter, we have seen how to perform basic programming in Python, including the language syntax, control structures such as conditions and loops, and object-oriented programming. We also reviewed the main Python datatypes, such as character strings or lists. Finally, we have seen how to install Python modules and in particular Owlready and the other modules needed for the examples in the rest of this book.