1. Variables in Python

2. Data types and operations

Python uses classes to define data types. The most frequently used data types are: str, int, float, range(), list(), set(), dict(), bool(), None (and there are some more).

2.1. Numeric types in Python

There are three numerical types in Python, which include integers (int), floating point numbers (real values, aka float) and complex numbers (complex). Here are some examples:

You can perform standard arithmetical operations numerical types, including:

Important: to check arguments of functions (e.g. the function named my_function()), use one of the options below:

2.2 Booleans

Boolean values are either True or False. Note that integers 0 and 1 are equivalent to False and True respectively.

For instance, we can compare the numbers with the operators less (<), greater (>), equal (==), not equal (!=) and, of course, less or equal (<=) and greater or equal (>=). This will yield a boolean output:

2.2.1 Logical operators and, or, not

The logical operators and, or, not can be used to test different statements to get a boolean answer, for example:

2.1. Strings

In the previous section you have already used the variables of the type string (str in Python). Let's see what we can do with them!

One can use built-in methods to change the string, i.e. replace a substring (.replace()), make all letters lowercase (.lower()) or uppercase (.upper()), for example:

You can concatenate strings by using the + operator as follows:

To get a string that would be joined with the same separator (it can be comma, semicolon, dot, underscore, etc.), use the .join() method. To split a string, use .split() function, which allows splitting by a defined pattern:

Strings in Python are represented as arrays of Unicode characters and therefore, single elements of a string can be accessed through the square brackets. To acces several elements of the string, use colon and specify the index range in the [start_idx:end_idx] format as shown below. Note that it is not necessary to specify start or end of the slice if start/end match the sequence start/end coordinate.

! Important: Enumeration in Python starts from 0 ! That is why the first index of the "L" letter in the variable my_name is accessed with [0]. When slicing a string keep in mind that the last coordinate is not included. One can access elements from the end of the string/list by using negative indexers starting from -1. In this case, the last element of my_name will be accessed as my_name[-1]. To acces a slice from the end, use the following syntax:

2.2. Lists

Lists in Python are created with square brackets and may contain mixed data types, multiple occurrences of the same element. For example:

Elements in the list are indexed, so to access a single element of the list or a several elements, use the following options:

Note that my_list contains a sublist. Its elements can be accessed by first extracting the list based on its index in my_list, and then extracting an element inside the sublist by its index starting from 0 and to the length of the sublist:

Side note 1: Note that there are two methods for sorting list: my_list.sort() orders elements inplace (changes the initial list), and sorted(my_list) returns a sorted copy of the list.

2.3. Sets

Sets in Python represent a non-repetitive collection of elements, so it is the set in a mathematical sense. Therefore, sets allow various set-like operations including built-in methods, such as .intersection(), .union(), .difference(). Similarly to lists, you can add/remove elements to/from a set, but sets cannot be sorted, set elements cannot be changed because sets are not indexed.

Side note: To create an unchangeable set, use the frozenset() method. To read more about it, check out the documentation.

You can create a set with curly braces by listing the elements inside:

Even though the element 'flowers' is repeated twice, the set definition with {} will automatically remove this repetition.

Alternatively, sets can be created from lists, for example, by using the set() constructor.

Below you can find several examples of set operations, such as intersection, union, difference:

To know more about set methods in Python, check out this link.

2.4. Dictionaries

The last, but not less useful data type is dictionary. Dictionaries are powerful since they allow to store different data types in the {key: value} format, which allows efficient hashing of elements. Dictionaries represent ordered (since Python3.6) structures with no repetitive keys, but editable elements. Note that keys can be represented only by immutable data types, check out this link to know more about mutable and immutable data types.

Dictionaries are represented with curly brackets and the following syntax:

To get values for a particular key of the dictionary, use the square brackets as follows:

To get more information on dictionary methods, check out this link.

3. Conditional statements (if, elif, else)

To test several statements, you can use the elif statement, which will tell the interpreter that when the if statement is not satisfied, try another contition, and if it works -- great! Otherwise, proceed either to the else statement, or execute the following lines of code (if any). Note that conditional statements in Python can be nested.

4. While and for loops

4.1. Writing loops with while

While is a conditional looping statement that allows you to perform an operation (or many operations) until the while statement is satisfied. Let's try solving the problem of finding all numerical values in my_list and storing them to a new list.

4.2. Writing loops with for

The for loop is useful when you need to iterate over a sequence of elements, which can be represented as a string, set, list, tuple or dictionary. Loops can be combined with conditional statements and various python functionalities. Please see the subsections below for more examples.

4.2.1. Iterating over lists

As in the subsection above, we will use my_list to find numerical data types.

4.2.4. Iterating over dictionaries

One of the useful functions is zip(), which takes two or more iterable objects (e.g., lists) and joins them into tuple. Let's see how we can create a dictionary from two lists with the zip() function:

5. Creating custom functions

Let's try out these functions! But first, we need to create some input data:

Great, let's continue to the next notebook!