Python Basics

I started writing those as a way to learn and remember python basic stuff. I found myself searching for the same commands, functions, and such very frequently so I decided to compile a few files with basic command with example.

I put them online to be able to access them easyly and to make them accessible for whoever would want or need theem.

It is not as complete as the documentation by any mean or even as python tips but it is a nice and easy way to refresh your memory or even learn a thing or two.


Statistics

Principal Component Analysis

PCA stands for Principal Component Analysis. It is a statistical technique which primary goal is to identify the underlying structure in the data by creating a new coordinate system that emphasizes the most important features of the data.

It isoften used to reduce the dimensions of a large data set by transforming it into a smaller set of uncorrelated variables called principal components.

Notebook

Lorenz curve and Gini coefficient

Lorenz curve and Gini coefficients are statistical tools generaly used to measure inequalities in the income distribution of a population.

Notebook

Control flow

Basic control flow

When writing complex code, it almost never can be done in one unique sequence. Often some operations need to be repeated or executed conditionnaly. Control flow in python work like most languages, using a collection of statements introduced by specific key-word.

It is easier than it sounds and is best explained with example.

Notebook

Context manager

Context managers allow to manage external ressources. It usually works with ressources that need to be opened, managed and then closed. The with statement allow to work specific objects for which a context manager exists.

Notebook

Error handling

In any given language, errors are a feature that let you know that something that you might not have expected occured. It is important to know how to handle and use them correctly.

Notebook

Collections

Collections

Collections in Python are containers that are used to store collections of data, for example, list, dict, set, tuple etc. These are built-in collections. Several modules have been developed that provide additional data structures to store collections of data.

Notebook

Lists

Lists are mutable sequences, typically used to store collections of homogeneous items (where the precise degree of similarity will vary by application).

They are one of the most commonly used collection in python. They are extremely flexible, intuitive and easy to use.

So much so that other more specialized collections tend to be overlooked and lists drawbacks too easly forgotten.

Notebook

Tuples

Tuples are immutable sequences, typically used to store collections of heterogeneous data. Tuples are also used for cases where an immutable sequence of homogeneous data is needed.

Notebook

Sets

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

The set type is mutable — the contents can be changed using methods like set.add and set.remove. Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.

Notebook

Dictionnaries

A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary.

A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.).

Notebook