0. Doing simple things: how do I output something in Python?

You may notice that only the last line of the cell above is visualized. To print both lines you can use the following syntax:

Side note: The hash symbol # is used to indicate comments or non-executable parts of the script that you don't want do remove from the code.

Congrats, you just printed several strings! As in many other languages, strings in Python are indicated with either single or double quotation. In most of the cases, single quotes are enough unless the string contains a single character '. For example:

The special backslash character \ can be used in the single quote setting to avoid errors:

And finally, double quotes:

The backslash character might be useful in cases when quotation inside the string is needed, for example:

To print a multiline string, use three single or three double quotes:

Alternatively:

And finally:

In the example above, the \n character indicates the new line. Another frequently used character is \t that indicates tab. For example:

1. Variables in Python

1.1. Creating variables

Creating variables in Python is simple, the only thing you need to do is to choose a variable name and assign a value or object to it. The variable can store different data types which we are going to discuss in the next section. As compared to other programing languages, Python doesn't require data type specification when a variable is defined.

Side note: It is impotant to use proper variable names and maintain the same style throughout your code. Variable names in Python should start with either a letter or an underscore! Also keep in mind that letter cases are important, so my_variable and My_variable would represent two different variables. To read more on variable names, check out the following links: https://www.w3schools.com/python/python_variables_names.asp and https://realpython.com/python-variables/

For example, we can use the string from the code above, assign it to the variable named my_string and print this variable:

To output the variable content, you may omit the print function:

One can combine custom strings and variables in the print() function:

1.2. Assigning data to multiple variables

If you want to create several variables, you may use the following syntax:

To create variables with the same values, use the following syntax:

1.3. Changing the variable

The variable content can be changed by reassigning it to another value/object, for example:

Side note: If you run the cell above for the second time, both output lines will be the same because the variable has been changed!

2. Data types and operations with them

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:

Excercise 1.

Define the variables x and y, set their values to 2 and 3 respectively. Apply the following operations to x and y, print the output and its type: x + y, x - y, x * y, x / y, x ** y, x // y, x % y.

Side note: if needed, the variable types can be specified in the following way: x = float(2), in this case, python will add a floating point to the number, i.e. x = 2.0. The process of specifying a variable type is called casting, and the variable type can be changes by using the constructor functions, such as int(), float(), etc.

Exercise 2.

  1. Use the same input values x and y as in the Exercise 1, but this time define the variable y as a float. Will output change? If so, how?
  2. Apply the constructor function int() to the output of the expression x / y. Did the result change as compared to the previous exercise? If so how?

If there are a lot of digits after the decimal point, the round() function can be used to round the floating number to the desired number of decimals. The round() function takes as the first argument the number, and as the second argument the number of digits for the rounding precision:

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

Side note: Floating values can be sometimes represented in a scientific form, for example, x = 5e13 which is equivalent to `x = 5 (10 * 13)`

2.2 Booleans

We can compare the numbers with the operators less (<), greater (>), equal (==), not equal (!=) and, of course, less or equal (<=) and greater or equal (>=). For example, let's check if the side note statement is correct!

As we can see, the output of the statement is True, that has the boolean type. Boolean values are either True or False. Note that integers 0 and 1 are equivalent to False and True respectively.

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:

In Python3.9 additional two methods .removeprefix() and .removesuffix() exist for strings. If the substring to be matched is found in either prefix (beginning) or suffix (end) of the string, then it is removed and the truncated version of the string is returned. If there is no match, the output is equal to the initial string. For example,

The above mentioned methods do not change the string inplace, so if you want to save the updated string, store it to a new variable, e.g.

To check if a string contains a paricular a substring, use the in keyword as in the following example:

Side note: to get the list of keywords type import keyword and in the next line print(keyword.kwlist). Remember that keywords cannot be used as variable names.

To repeat the same string n number of times use the multiplication operator *:

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:

Another useful method for strings is .split() which allows splitting by a defined pattern:

Let's use .split() to extract separate words from the string:

Strings in Python are represented as arrays of Unicode characters and therefore, single elements of a string can be accessed through the square brackets in the following way:

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:

To know the length of the string one can use the len() function:

You may have noticed in the cell above the new structure represented with square brackets and comma-separated variables (strings in this example). This stucture is called list and it is one of the data types allowing to store data in Python. To know more about lists, sets and dictionaries, please see the following subsections.

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:

Single elements in lists can be changed by using indeces. For example, let's take the word "cat" (the second element in Python counting system) and replace it with the word "owl".

Great, the replacement worked! Let's now try changing several sequential words "owl" and "dog" to words "pigeon" and "parrot".

Lists and strings can be reversed with [::-1]:

Lists in Python can be modified, namely, you can append, remove, replace, sort objects inside a list. The full list of methods is described here.

Let's try out some methods:

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.

Side note 2: By using the list() constructor, one can create a list from another data type, for example, np.array(), which we will discuss later. Then, the constructor will be used in the following way: list(np.array(['a', 'b', 'c'])).

To check if a list contains a paricular element, use the in keyword as in the following example:

Note that my_list contains a sublist. It's 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:

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:

To add an element to the set, use the .add() method:

Existing sets can be extended with the elements from another set (or list) with the .update() method:

To check if a set contains a paricular element, use the in keyword as in the following example:

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:

As strings, lists and sets, dictionaries have a length.

Exercise 3.

  1. What is the length of my_dict?
  2. What are the data types of dictionary values?

Dictionary elements (or items, can be obtained with .items()) are keys and values, which can be retrieved from the dictionary with .keys() and .values() methods.

Exercise 4.

  1. What are the keys of my_dict?
  2. What are the values of my_dict?

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

Alternatively, one can use the built-in method .get() which will return the value for a key if it is present and None otherwise (unless the alternative is specified):

To add a new item, or several items to the dictionary, one can use either square brackets, or the .update() method in the following way:

Exercise 5.

  1. Add two more keys and respective values of your choice to my_dict using the .update() function. Print the final dictionary.

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

Side note: Dictionaries can be converted to lists and vise versa:

Note that in the example above, elements of the list are tuples, which are defined with round brackets (see next section 2.5. to know more about tuples):

Lists can be converted to dictionaries as well:

2.5. Tuples

Tuples represent another data type that is ordered, immutable and allows duplicates. As in other data types, tuples can contain any data type, tuples have length and tuple elements can be accessed with indexing. Tuples in Python are defined with round brackets (), and the .tuple() constructor allows to create and/or convert other data types to tuples:

2.6 None data type

None is a data type whith is defined with a keyword None and indicates null value, or absence of a value. Therefore, None cannot be interpreted as zero value, False, or an empty object (e.g. string, list, dict).

3. Conditional statements (if, elif, else)

In the subsection 2.2. we introduced boolean variables and discussed logical operators and, or and not in the subsection 2.2.1. Conditional statements in Python allow to test statements and define the code based on that.

! Important: When using Python, one needs to be careful with indents and spacings! One extra space or absence of an indent may cause an error.

In conditional statements, the else statement is not required. In this case, if the if statement is not satisfied, noting will happen:

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.

Python allows for having several consecutive if and/or elif statements. Python will test each if statement but will stop testing at the elif statement that was satisfied.

Side note: Sometimes you might want to have an empty conditional statement. In this case, use the pass statement right after a contition. To read more about it, check out this link.

4. While and for loops

Let's go back to our example with my_list and suppose we don't know its content and we want to store only numerical values to another list. How would you approach this problem? Well, you can, of course, check the length of the list and print each element by accessing it via indexing with square brackets, test whether an element has a numerical type and then add those elements to a new list. However, such approach is not efficient. This is the moment where loops come in hand!

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.

If you want to stop the while loop if a custom condition is satisfied, use the break statement as in the example below:

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.

All three examples above will generate the exact same output, which is:

In the example above we saw how to interupt looping with the break statement. continue is another useful statement that allows to jump over one of the elements if a conditional statement is true.

Let's try it out by skipping the numerical values and printing all other elements while looping:

for loops can be nested, for example, you may have noticed that we have a sublist inside of my_list which contains one numerical value. Let's get is out and save to the numerical list with other values:

4.2.2. List comprehensions

List comprehensions are a compact way of writing for loops. For example, the code from the subsection 4.2.1. can be rewritten as a one-liner:

However, this representation is not esthetically nice and does not follow the PEP8 Python code styling guidelines. To know more about styling your code with PEP8, check out the enclosed link.

If we follow the guidelines, the one-liner will turn into the following structure, which is better structured and allows much easier to navigation through the code:

4.2.3. Iterating over strings and sets

Iteration over strings and sets is similar to iteration over lists. Let's use the variables we have defined above and implement the

Or as a list comprehension:

4.2.4. Iterating over dictionaries

Let's create a dictionary from my_dict that would contain only string values:

Let's now do the same thing but with dictionary comprehension:

Another useful function 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 and merging all functionality together!

Sometimes you will need to execute the same piece of code several times. Let's imagine that you have 10 different lists and you want to know if the largest list element is larger than the sum of all other elements in the list. An efficient way of approaching this task is to first, create a function with a keyword def that would take a list as input and return a boolean for the tested condition. Here is how to do it:

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

Congrats! Now you know the basics of coding with Python!

Of those intereseted, here is the list of useful resourses:

  1. The main Python page with the most recent documentation and links to learning platforms, excercises, cheat sheets and many other things for beginners.
  2. The Rosalind platform -- allows learning Python through Bioinformatics problem solving.