{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Supramolecular and Cellular Simulations (Prof. Fischer)
Center for Computational and Theoretical Biology - CCTB
Faculty of Biology, University of Würzburg

\n", "\n", "



We are looking forward to your comments and suggestions. Please send them to sabine.fischer@uni.wuerzburg.de

\n", "\n", "

Python basics

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python\n", "

\n", "Python is an intuitive programming language. It allows writing smaller programmes without to much prior knowledge on programming. It is widely used in the machine learning community. Major machine learning packages like e.g. keras or tensorflow can easily be incorporated.\n", "\n", "#### Further information on Python: https://www.python.org/doc/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Jupyter Notebooks\n", "### What is a Jupyter Notebook?\n", "

Interactive environment for writing and testing code as well as presenting the results. A notebook consists of cells, which can contain text, formula, code, results, plots and other types of visualisation.

\n", " \n", "

\n", " \n", "

\n", "\n", "\n", "#### Further information on Jupyter: https://jupyter-notebook.readthedocs.io/en/stable/\n", "\n", "### Exercise:\n", "- Generate a new cell, enter code (e.g. 3+4) and evaluate the cell (Shift+Enter) \n", "- generate a second cell, tranform it into markdown, enter text and format it (for ideas on formating options double click on this cell or have a look on the internet)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"Hello world, I am a piece of Python code\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python basics\n", "### Different data types\n", "

There are different data types in Python. Some functions are only defined for certain data types. Other functions provide different output depending on the data type. Therefore, it is important to be aware of the data types of varaiables.

\n", "\n", "Examples for data types \n", "- int: integer\n", "- float: floating point numbers\n", "- str: character strings (text)\n", "- list: list of variables, strings, numbers, ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise:\n", "> - Insert different items in the brackets to obtain their data type (e.g. type(5) or type('5'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables\n", "

Variables are containers for storing data values. Hence, for example a variable t for time can get the value 5.
Once a variable has an assigned value, it can always be accessed by calling that varianble. In Jupyter, the assignment to the variable is global. Hence, it is accessible in every cell in the notebook. The order of cells in the notebook does not play a role, only the order in which the cells are evaluated. Names of variables can be short like x or y or descriptive like cellCentroid or cell_volume. For a better readability of your code, make sure that you choose the names of variables carefully. Python is case sensitive. Hence, world and World are two different variables.

\n", " \n", "```Python\n", "t = 5\n", "v = -9\n", "variable = 59\n", "\n", "t #to display the value stored in the variable t\n", "```\n", "\n", "### Operators\n", "Operators are basic commands like summation or testing equality of two values.\n", "
Mathematical operators:\n", "```Python\n", "+ - * /\n", "3**5 #three to the power of five\n", "```\n", "Logical operators:\n", "```Python\n", "== #is equal to\n", "> #less than\n", "< #greater than\n", "```\n", "Logical chain:\n", "```Python\n", "a == x and b == x #a and b have the same value as x\n", "a == x or b == x #a or b has the same values as x\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises:\n", "> - Conduct the following operations. Guess the output before you execute the cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 4\n", "b = 5\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2**5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 1 + a\n", "y = b - 7\n", "z = 9 * a\n", "x, y, z" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b == 7" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "7 > b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "8 > 5 and 7 > b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "4 > b and 7 > 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "4 > 5 or 7 > b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Do your own calculations \n", "
(hint: `+` also works for two strings)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "A list is a compound data type that groups together several values. The values in a list can be of different data types. Lists can also be stored in variables. Defining a list and storing it in a variable:\n", "```Python \n", "x = [1,2,3]\n", "```\n", "To extract one element of a list, use the index of the element in square brackets. Please note, indexing of lists in Python starts with 0: \n", "```Python\n", "[1,2,3][2] #element at the third position in list\n", "#or more conviniently by using the variable \n", "x[2] \n", "```\n", "Lists can be concatenated with `+`:\n", "```Python \n", "[1,2,3]+[4,5,6]\n", "```\n", "Also, single values or whole lists can be appended to an existing list:\n", "```Python\n", "liste.append(variable)\n", "liste.append(liste2)\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises:\n", "> - Generate a list that contains the integers 5,7,8,9,2 and 3 (in this order), save it in a variable and display the value saved in the variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Extract position 2 and 5 of the list and display the result in one line." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Generate a list containing numbers and strings. Display it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Generate an empty list and concatenate with the other two." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Generate an empty list and append the other two." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Built-in functions\n", "

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
\n", "Python has a large number of built-in functions. Function names start with a lower case letter. To call a function use the function name followed by two round brackets, which may contain the values of function parameters. You have already come accross the two built-in functions `print` and `type`.

\n", " \n", "#### Commonly used buil-in functions:\n", "```Python \n", " print()\n", " type()\n", " min() \n", " max()\n", " help()\n", " ...\n", "```\n", "\n", "

It is also possible to define your own functions. For further information see e.g. https://docs.python.org/3/tutorial/controlflow.html#defining-functions

\n", " \n", "\n", "### Exercise:\n", "> - Use the function `print`to display the text: I am a string!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Use the function `help` to obtain information about the functions `min` and `max`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Evaluate the functions `min`and `max` for the lists that you defined above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For-Loop\n", "

Often you want to repeat the same calculation several times. Instead of repeating the same bits of code in the notebook, it is easier to use a for-loop.\n", "
The first line of the command states how often the evaluation of the code should be repeated. This is followed by the code itself which needs to be indented. There are two possibilities to set the number of repetitions. Either you conduct the for-loop for all elements in a list l

\n", " \n", "```Python\n", "for variable in l: # please note the colon at the end\n", " print(variable)\n", "```\n", "\n", "

or you provide a specific number of iterations using the range function

\n", " \n", "```Python\n", "for variable in range(5):\n", " print(variable)\n", "```\n", "\n", "\n", "```Python\n", "range()``` \n", "starts at 0 until the predecessor of the integer inserted as an argument, i.e. range(5) generates the sequence 0,1,2,3,4\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises:\n", "> - Execute the two exemplary for-loops above. (Please note: for the first one you need to define a list l)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Write a for-loop that takes each element in l, adds 2 and displays the results." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conditional statement\n", "

In some cases, a piece of code should only be evaluated if a certain condition is fulfilled. In this case, the if-statement can be used. The first line contains the statement, which is followed by the code. This can be followed by an else-statement and the code that should be executed if the original condition is not fulfilled. The pieces of code that should be executed always need to be indented.

\n", "\n", "```Python\n", "if x == 5:\n", " print(x)\n", "else:\n", " print('nö')\n", "```\n", "\n", "For further information on how to deal with more than two alternative conditions, see https://docs.python.org/3/tutorial/controlflow.html#if-statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise:\n", "> - Write code that performs the following task: if the number 5 is in a previously defined list, print the string 'yes', otherwise print 'no'." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Packages\n", "

The programming language Python has a modular structure. Hence, only few functions are contained in the core version, while a large number of additional functions are contained in different packages. Typically, the functions of one package are optimised for a certain type of taks.
Commonly used packages are pandas (for data processing), numpy (for numerical caluclations), stats (for statistics) and matplotlib (for plotting).

\n", "\n", "### Importing packages\n", "To be able to use the functions of a specific package, it has to be imported before the function is used for the first time. For a better overview, it is best to put these import statements at the beginning of a notebook.\n", "```Python\n", "import package #import the package with the name package\n", "# in this case a function with the name func from that package would be called by package.func\n", "\n", "import package as pk #import the package with the name package and save it in the variable pk\n", "# in this case a function with the name func from that package would be called by pk.func (hence this approach saves ty-ping and is therefore particularly useful if the package is used very often or has a long name)\n", "```\n", "\n", "### Exercise:\n", "> - Look at the line of code below. Which statement do you need to evalaute before that line for it to work? (Hint: you need to load a package as pd and it is one of the four example packages mentioned above) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pd.DataFrame({'Spalte1': [1,2,3,4,5,6], 'Spalte2':[6,5,4,3,2,1]}) #this line of code generates a table" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Advanced exercise\n", "Hint: for some of these exercises, you might need to search for further information on the internet\n", "> - Write code that:
\n", " 1) generates a list with five numbers
\n", " 2) multiplies the first entry in the list with 2, the second with 3 and so on
\n", " 3) saves the results in a new list
\n", " 4) determines and saves the largest and smallest entry in both lists
\n", " 5) displays all lists and values in a comprehensible manner and displays a string, if one of the maxima is larger than 25
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Generate a table with the coloumns: 'R1','R2','R3','R4','R5', 'min', 'max' to display your results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> - Write the task above in a user-defined function and execute it for several different lists." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 2 }