Python Lessons - 4.4 Raise an exception

Python Lessons – 4.4 Raise an exception

It can happen sometimes that we ourselves want to create and throw an exception. The reasons for doing this can be many. Often you might want to create a more specific exception, which signals a specific error to those who are using our code in the future.

Python lessons - 4.5 Asserts m

Python Lessons – 4.5 Asserts

Assertions (assert) are a very useful control tool to use when writing a code in the test (DEBUG) phase. Each assertion introduced in the code performs a check at a precise time of execution, using a condition. If the condition is satisfied then the execution goes forward, otherwise a Asserterror exception is raised that blocks the execution of the program.

Python Lessons - 4.9 Handling files

Python Lessons – 4.9 Handling files

In the previous sections we have seen how to read and write the data within a file, but there are some precautions to follow. We have seen that it is important to always close the file we are working on while running the program.

Python Lessons - 5.2 Dictionaries m

Python Lessons – 5.2 Dictionaries

Dictionaries are a data structure used to arbitrarily map keys with values. The lists, which we have already seen before, can be thought of as dictionaries where the keys are a sequence of integers, where each number (index) corresponded to a value.

Python Lessons - 5.3 Funtions on dictionaries m

Python Lesson – 5.3 Functions on dictionaries

Dictionaries already have a fairly complex structure and therefore various functions are needed to manage them. However, there are clauses in and not in that allow you to know whether a key is present in a dictionary, returning True in the first case, False in the second.

Python Lessons - 5.4 Tuples m

Python Lessons – 5.4 Tuples

After the lists and dictionaries, another type of structured data used is the tuple. The tuple is very similar to a list, except that once defined this is immutable (i.e. the values contained within cannot be modified). In Python, the tuple is recognizable as it is defined within parentheses ().

Python Lessons - 5.5 Slicing lists m

Python Lessons – 5.5 Slicing Lists

The slicing is a more advanced way of getting multiple items at the same time from a list. In fact if we want to get the elements between two indices of a list we can express the two extreme indexes separated by two points: