{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of contents\n", "\n", "1. [Introduction](#Introduction )\n", "2. [Variables](#Variables)\n", "3. [Values](#Values)\n", "4. [Operators](#Operators) \n", "5. [Functions](#Functions)\n", "6. [Encryption and Decryption](#Encryption_Decryption)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Introduction\n", "In this tutorial, the topics **variables**, **values**, **operators**, **functions** and **encryption and decryption** will be treated. Mainly, the basics of each topic will be introduced eventhough there would be a lot more to learn in each topic. However, if you are new to coding in Python, this tutorial is suited for you as it should help you understand the basics, which is key, before going into more advanced coding. Bear in mind that one could go into more detail for these concepts: If you are interested in learning more to a topic either refer to our links which lead you to external, more in-depth sources or use Google as you can find mostly anything related to Python online. However, before you do this: Make sure you understand the basics presented in this tutorial and that you can apply them. \n", "\n", "To the content of this tutorial: First, **variables** are looked at. Variables are key in any programming language and often serve as placeholders so that algorithms or functions can be coded and later on used for different values. \n", "An algorithm is a step-by-step approach on how to solve a problem. A function relates input to output. Without variables, the code would need to be rewritten for any new value which is rather cumbersome. You can assign different values to a variable as for example a number or a string which is a list of numbers and characters. \n", "\n", "Second, **values** are introduced. Values are whatever is assigned to a variable. There are several types of variables, such as: strings, numbers or booleans. A boolean is the result of a logical statement, hence a boolean either takes the value *true* or *false*. \n", "\n", "Third, **operators** are treated. Operators are used to perform mathematical operations on one or more variables or values. They are nothing else than functions which are already built in in Python. Like values, operators are classified into different types: For example, arithmetic operators such as **+** and **-**.\n", "\n", "Fourth, **functions** are introduced. Functions generally make coding more efficient as they can be used for solving similar problems for different inputs. There are different kinds of functions which can be used in Python: Either those which you code and later on call (=use) yourself or the ones that are already built-in in Python: as for example the function `print()`, which returns whatever is written in parantheses. Additionally, there also exist functions within so-called libraries or packages which first need to be imported into Python. \n", "\n", "Fifth, **encryption and decryption** are treated. Encryption and decryption are cryptographic algorithms which can be used for sending secret messages that can only be read by the recipient provided he or she is knowledgeable about the applied algorithm. Encryption is the algorithm which transforms a plain, legible text into an encrypted, unintelligible text. The algorithm which makes encrypted text again intelligible is referred to as decryption. \n", "\n", "In the end, a conclusion is provided which sums up the key takeaways of this tutorial such that you can check whether you understood the tutorial's content or whether you have to go over certain parts again." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Variables\n", "\n", "A **variable** can be thought of as reserved memory space in the kernel which stores a value. Remember from the previous tutorial; the kernel is where the code gets executed and where variables are stored. In other words, when a variable is defined, some space in the memory of the kernel is reserved for that particular variable. When this variable is called, whatever is saved within the related memory space is returned. For a start, how to define a variable and then how to call/use a variable is explained. \n", "\n", "*(Note: This chapter contains verbatim quotes from: https://www.programiz.com/python-programming/variables-constants-literals)*\n", "\n", "## Define a variable\n", "\n", "Use the equality sign = to define variables: The value to the left of the equality sign is used as the name of the variable and the value on the right of the operator is used as the value stored in the variable (or more exact in the memory space which is referred to as the variable). Example:\n", "\n", "a = 1\n", "\n", "The variable would be named a and the value assigned to it would be 1." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "1 -3\n" ] } ], "source": [ "# You can run the code in this cell as an example.\n", "# Change the values for the variables a, b and c and check whether the output of the code changes\n", "\n", "a = 1\n", "b = 2\n", "c = -3\n", "\n", "print(a) # As you can see: print() simply returns whatever is given within the parentheses\n", "\n", "print(b)\n", "\n", "print(a, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Name a variable\n", "When naming a variable or in other words, when chosing whatever is written on the left-hand side of the equation, you are relatively free. However, the following rules should be adhered to: \n", "* Use lowercase characters, \n", "* avoid non-letter characters (like !,&, %,,...),\n", "* underscore and numbers can be used, but not at the beginning of the name, \n", "* keywords (e.g. if, while, return,...) cannot be used as a name,\n", "* be as explicit as possible." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiple assignment\n", "\n", "It is possible to assign a single value to several variables simultaneously. Example:\n", "\n", "x = y = z = 5\n", "\n", "This assigns the value 5 to the variables x, y, and z." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5\n", "5\n", "15\n" ] } ], "source": [ "x = y = z = 5\n", "\n", "print(x)\n", "\n", "print(y)\n", "\n", "print(z)\n", "\n", "print(x+y+z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pay attention: If, for example, the value 5 gets assigned to x, and x gets assigned to f. Then if a different value gets assigned to x, f still refers to the memory space that x previously referred to and not to the new memory space. This is because Python runs code line by line." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 5\n", "10 5\n", "10 10\n" ] } ], "source": [ "x = 5 # 5 is assigned to the variable x\n", "f = x # x is assigned to the variable f\n", "\n", "print(x, f) # both variables refer to the same value (5), as f refers to the memory space of x\n", "\n", "x = 10 # 10 is newly assigned to x\n", "\n", "print(x, f) # x equals newly 10, however, f still refers to the old memory space of x, hence to 5\n", "\n", "# This can be avoided by rewriting f = x after newly defining x:\n", "\n", "X = 10\n", "f = x\n", "\n", "print(x, f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to define several variables at once, in one line of code. To do so, separate the individual variables and the values assigned to them by commas." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "6\n", "7\n", "18\n" ] } ], "source": [ "x, y, z = 5, 6, 7\n", "\n", "print(x)\n", "\n", "print(y)\n", "\n", "print(z)\n", "\n", "print(x + y + z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Call a variable\n", "\n", "The value that is assigned to a variable can be called by using `print()` and in parantheses the name of the variable. \n", "The advantages of using variables instead values are the following:\n", "* Makes code more readable,\n", "* Avoids repetition of values,\n", "* Functions can be re-used for different values,\n", "* Complex formula can be read easier.\n", "\n", "**Example:**\n", "capital = 10 000, \n", "interest = 0.05, \n", "periods = 12\n", "\n", "$future value = capital \\cdot (1 + interest)^{periods}$\n", "\n", "In this example, the variables are chosen in a way that they help to understand a function or formula better. The values assigned to the variables can be changed quickly such that the formula can be re-used.\n", "\n", "Here an example on how you can use `print()` to call your variable:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.0\n" ] } ], "source": [ "a = 2\n", "\n", "print(a + a ** a + (a / a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Delete variables\n", "With the command `del` followed by a variable name, the variable can easily be deleted. It is also possible to delete multiple variables simultaneously." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Declare a variable\n", "maths_grade = 6\n", "print(maths_grade)\n", "\n", "# Delete the variable \n", "del maths_grade" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Values\n", "\n", "Every program written in Python (or in any programming language) operates on data. A single piece of information or data can be referred to as a **value**. \n", "In Python, values are grouped into different data types or so-called classes. The function `type()` returns you the class a value belongs to. Not all possible types of values will be discussed here, however, the most important ones are looked at:\n", "* Strings\n", "* Numbers\n", "* Boolean\n", "* Lists\n", "* Tuples\n", "* Dictionaries\n", "\n", "*(Note: This chapter contains verbatim quotes from: https://realpython.com/python-data-types/ and https://developer.rhino3d.com/guides/rhinopython/python-datatypes/ and http://dataanalyticsedge.com/2018/05/08/basics-of-python/)*\n", "\n", "## Types of values\n", "\n", "### Strings\n", "\n", "The value type string refers to lists of characters, spaces and numbers that can form words and sentences. However, they do not need to make linguistic sense to be strings. They are written within quotation marks: Pairs of single or double quotes are usually used. Examples of strings are: \"Hello World!\", \"I am 20 years old.\" and \"alskdfj2983\".\n", "\n", "Here are a few basic operations for strings (in the code below, it can be seen how they are applied):\n", "\n", "The plus sign + is used to add up different elements of the string and the asterisk * is the repetition operator: To indicate how often a string should be repeated. Multiple strings can either be combined with the + sign, commas or with the function `.format()`.\n", "\n", "To break up a string over more than one line, include a backslash \\ before each new line. Inversely, to receive a string that goes over multiple lines, one can make a line break in Python by adding a backslash with a small n \\n at the respective point.\n", "\n", "A convenient way to create a string with both single and double quotes in it, is to create a triple-quoted string: In this way, new lines can be created." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n", "Numbers such as 1234 can also be printed as part of a string.\n", "We can also separate text by using commas as shown in this line of text.\n", "We can also use plus signs to add text fragments to each other.\n", "Pay attention to spaceswhen adding text so your out put does not contain any typos.\n", "Number a is 5. Number b is 4. Number c is 3.\n", "abc\n", "a\n", "b\n", "c\n", "This\n", "is a \"really\"\n", "convenient 'way' of\n", "adding multiple lines.\n", "HelloHello\n", "HelloHelloHello\n", "S\n", "Switzerland\n" ] } ], "source": [ "# We use the print() function to tell Python to output strings \n", "print(\"Hello World\")\n", "print(\"Numbers such as 1234 can also be printed as part of a string.\")\n", "print(\"We can also separate text by using commas\", \" as shown in this line of text.\")\n", "print(\"We can also use plus signs\" + \" to add text fragments to each other.\")\n", "print(\"Pay attention to \", \" spaces\" + \"when adding text\", \"so your out \" + \" put does not contain any\", \"typos.\")\n", "\n", "# Format function\n", "a = 5\n", "b = 4\n", "c = 3\n", "print(\"Number a is {}. Number b is {}. Number c is {}.\".format(a,b,c))\n", "\n", "# Splitting up a string over multiple lines\n", "print(\"a\\\n", "b\\\n", "c\")\n", "\n", "# Output multiple lines with a one-line string\n", "print(\"a\\nb\\nc\")\n", "\n", "# Triple Quoted String\n", "print(\"\"\"This\n", "is a \"really\"\n", "convenient 'way' of\n", "adding multiple lines.\"\"\")\n", "\n", "# Addition and multiplication of strings\n", "text = \"Hello\"\n", "print(text + text) # This will output the sentence twice\n", "print(text * 3) # This will output the sentence three times\n", "\n", "# How to subset a string\n", "text = \"Switzerland is so small!\"\n", "print(text[0]) # creates the subset S which is the character position 0\n", "print(text[0:11]) # shows the subset Switzerland, the character position 0 to 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Functions for string operations\n", "\n", "\n", "In Python there are several built-in functions (what a built-in functions is, will be treated in a upcoming section called *functions*) which are useful for operations with strings. However, as these so-called methods are type-specific, these functions can only be used for strings.\n", "\n", "The most important methods are depicted in the following list:\n", "\n", "\n", "| Function | Description |\n", "|---|---|\n", "| strip() | removes any whitespace from the beginning or end |\n", "| len() | returns the length of a string |\n", "| lower() | returns string in lower case |\n", "| upper() | returns string in upper case |\n", "| replace() | replaces string with another string |\n", "| split() | splits tring into substring if it finds instrances of the seperator |\n", "\n", "Here are examples showing how to apply these functions:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n", "13\n", "hello, world!\n", "HELLO, WORLD!\n", "Hello, Zorld!\n", "['Hello', ' World!']\n" ] } ], "source": [ "a = \" Hello, World!\"\n", "print(a.strip( )) # takes away / \"strips away\" any superfluous spaces\n", "\n", "b = \"Hello, World!\"\n", "print(len(b)) # counts elements within string (elements = characters, spaces, signs, numbers)\n", "print(b.lower())\n", "print(b.upper())\n", "print(b.replace(\"W\",\"Z\"))\n", "print(b.split(\",\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numbers\n", "Another value type or class of Python are numbers. Python 3 supports three different numerical types:\n", "* int (integers)\n", "* float (floating point real values)\n", "* complex (complex numbers)\n", "\n", "Normally, Python will convert a number automatically from one type to another if needed and this is fine most of the time. But under certain circumstances if a specific numeric type is needed, the format can be forced by using additional syntax from the table below:\n", "\n", "| Type | Format | Descrption |\n", "|---|---|---|\n", "| int() | a = 10 | Integer |\n", "| float() | a = 45.67 | (.) Floating point real values |\n", "| complex() | a = 4-3j | (j) Complex (imaginary) number |\n", "\n", "#### Integers \n", "Integers are whole numbers, positive or negative, without decimals, of unlimited length such as: 2, 6, 2368, -78, -9746286.\n", "\n", "\n", "#### Floats\n", "Floats, or \"floating point number\" are numbers, positive or negative, containing one or more decimals such as: 1.1, 0.00025, 134.56, -78.64, -12000.7, 5.0\n", "\n", "Floats can also be scientific numbers with an \"e\" to indicate the power of 10.\n", "\n", "#### Complex Numbers\n", "A complex number consists of an ordered pair of real floating-point numbers denoted by x + yj, where x and y are the real numbers and j is the imaginary unit. Examples: 2+45j, 9.322e-36j\n", "\n", "#### Built-in mathematical functions and math package\n", "Python supports many built-in funtions of which the most useful ones for this topic are presented in the following overview table:\n", "\n", "| Function | Description |\n", "|---|---|\n", "| abs() | Returns absolute value of the value; (positive) distance between the value and zero |\n", "| divmod() | Returns quotient and remainder of integer division |\n", "| max() | Returns the largest of the given arguments or items in an iterable |\n", "| min() | Returns the smallest of the given arguments or items in an iterable |\n", "| pow() | Raises a number to a power |\n", "| round() | Rounds a floating-point value |\n", "\n", "\n", "Other functions performing useful calculations in this context may also be appropriated by making use of the math library in Python. However, this must be imported into the code beforehand with `import math`. We will cover library in more detail in the part *functions* later on.\n", "\n", "Consequently, an overview of important functions of the math-package is provided:\n", "\n", "| Function | Description |\n", "|---|---|\n", "| math.ceil() | Returns the ceiling of the value; smallest integer not less than the value |\n", "| math.exp() | Returns the exponential of the value; the euler number to the power of the value |\n", "| math.floor() | Returns the floor of the value; largest integer not greater than than the value |\n", "| math.log() | Returns the natural logarithm of the value for value > 0 |\n", "| math.log10() | Returns the base-10 logarithm of value of value > 0 |\n", "| math.sqrt() | Returns the square root of the value for value > 0 |\n", "| math.cos() | Returns the cosine of the value radians |\n", "| math.sin() | Returns the sine of value radians |\n", "| math.tan() | Returns the tangent of value radians |" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "\n", "80\n", "\n", "4.2\n", "\n", "0.00042\n", "\n", "(4+8j)\n", "\n", "5\n", "(2, 5)\n", "9\n", "2\n", "16\n", "15\n", "6\n", "54.598150033144236\n", "4\n", "5.0106352940962555\n", "2.0969100130080562\n", "1.4142135623730951\n", "-0.9880316240928618\n", "-0.9843766433940419\n", "0.5578517393521941\n" ] } ], "source": [ "# Integer numbers\n", "print(10)\n", "print(type(10))\n", "print(0x50)\n", "print(type(0x50))\n", "\n", "# Float numbers\n", "print(4.2)\n", "print(type(4.2))\n", "print(4.2e-4)\n", "print(type(4.2e-4))\n", "\n", "# Complex numbers\n", "print(4+8j)\n", "print(type(4+8j))\n", "\n", "# Built-in Mathematical Functions of Python\n", "print(abs(-5))\n", "print(divmod(17,6))\n", "print(max(4,3,7,4,9,2,3))\n", "print(min(4,3,7,4,9,2,3))\n", "print(pow(4,2))\n", "print(round(14.7))\n", "\n", "# Math package functions\n", "import math\n", "print(math.ceil(5.6))\n", "print(math.exp(4))\n", "print(math.floor(4.9))\n", "print(math.log(150))\n", "print(math.log10(125))\n", "print(math.sqrt(2))\n", "print(math.sin(30))\n", "print(math.cos(85))\n", "print(math.tan(22.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For further information on the value type numbers, click __[here](https://www.tutorialspoint.com/python/python_numbers.htm)__ or __[here](https://www.w3schools.com/python/python_numbers.asp)__." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean\n", "Boolean values are either `True` or `False`. They are usually the output of a function that includes a logical operator. Expressions in Python are often evaluated in a Boolean context, meaning, they are interpreted to represent truth or falsehood. We will explain later in the notebook what exactly a logical operator is, but simply put, it tests if a statement is true or not." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "\n" ] } ], "source": [ "a = True\n", "b = False\n", "print(a)\n", "print(type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists\n", "Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets `[]`. In Python, any data type can be added to a list. Once a list is created using only integers as its elements, it is possible to add other value types, such as strings or booleans.\n", "\n", "The values stored in a list can be accessed using the slice operator `[]` and `[:]` with indexes starting at 0 in the beginning of the list and working their way to end -1. The plus sign + is the list concatenation operator, and the asterisk * is the repetition operator.\n", "\n", "To add elements to a list, we use the very useful method `.append()` that will attach a certain value to the end of the respective list.\n", "\n", "Lists are not limited to a single dimension; a so-called list of lists is possible. Multiple dimensions can be declared by seperating them with commas. For instance, in a two-dimensional list, the first number is always the number of rows where the second number is the number of column. \n", "\n", "For further information, click __[here](https://www.w3schools.com/python/python_lists.asp)__." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "['Hello', ',', 'how', 'are', 'you', '?']\n", "[1, 'I', 5.6, 'goodbye']\n", "goodbye\n", "[6, 7, 8, 9, 10]\n", "['Hello', ',', 'how']\n", "[1, 'I']\n", "[1, 'I', 5.6, 'goodbye', 'XYZ', (4-5j)]\n", "[[5, 3], [2, 3]]\n", "[5, 3] [2, 3]\n" ] } ], "source": [ "# You can run the code in this cell to create lists, print them and to subset them.\n", "\n", "number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "string_list = [\"Hello\", \",\", \"how\", \"are\", \"you\", \"?\"]\n", "mixed_list = [1, \"I\", 5.6, \"goodbye\"]\n", "\n", "print(number_list)\n", "print(string_list)\n", "print(mixed_list)\n", "print(mixed_list[3])\n", "print(number_list[5:])\n", "print(string_list[:3])\n", "print(mixed_list[0:2])\n", "\n", "# Method .append()\n", "mixed_list.append(\"XYZ\")\n", "mixed_list.append(4-5j)\n", "print(mixed_list)\n", "\n", "# Example of a two-dimensional list\n", "my_list = [[5,3],[2,3]]\n", "print(my_list)\n", "print(my_list[0],my_list[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples\n", "A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.\n", "\n", "The main differences between lists and tuples are: Lists are enclosed in square brackets `[]` and their elements and size can be changed, while tuples are enclosed in regular parentheses `()` and cannot be updated. Tuples can be thought of as read-only lists." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n", "('Hello', ',', 'how', 'are', 'you', '?')\n", "(1, 'I', 5.6, 'goodbye')\n", "I\n", "(6, 7, 8, 9, 10)\n", "('Hello', ',', 'how', 'are', 'you')\n" ] } ], "source": [ "# You can run the code in this cell to create some tuples, print them and subset them\n", "# In this example, the tuples behave exactly as lists would\n", "\n", "number_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n", "string_tuple = (\"Hello\", \",\", \"how\", \"are\", \"you\", \"?\")\n", "mixed_tuple = (1, \"I\", 5.6, \"goodbye\")\n", "\n", "print(number_tuple)\n", "print(string_tuple)\n", "print(mixed_tuple)\n", "print(mixed_tuple[1])\n", "print(number_tuple[5:])\n", "print(string_tuple[0:5])\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n" ] }, { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mnew_tuple\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_tuple\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# The difference between lists and tuples becomes clearer, when we try to update the data contained therein\n", "\n", "new_list = [1, 2, 2, 4, 5]\n", "\n", "new_tuple = (1, 2, 2, 4, 5)\n", "\n", "new_list[2] = 3\n", "print(new_list)\n", "\n", "new_tuple[2] = 3\n", "print(new_tuple)\n", "\n", "# It is possible to update the list, but not the tuple. Tuples are read-only.\n", "# When running this cell, Python will display an error message, highlighting row 10\n", "# Add a hashtag # symbol to row 10 and re-run the cell" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4, 5)\n" ] } ], "source": [ "# To update the tuple from the previous exercise, we need to recreate the whole tuple with the new values\n", "\n", "new_tuple = (1, 2, 2, 4, 5)\n", "\n", "new_tuple = (1, 2, 3, 4, 5)\n", "\n", "print(new_tuple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionaries\n", "Dictionaries are lists of Key:Value pairs. They are very helpful when related information is held that is associated through keys. The main operation here is to extract a value based on the key name.\n", "\n", "Unlike lists, where index numbers are used, dictionaries allow for the use of a key to access its members.\n", "\n", "A dictionary key can be almost any value type but is usually a number or string. Values, on the other hand, can be any arbitrary Python object. The Keys must, however, be unique in the respective dictionary.\n", "\n", "Dictionaries are enclosed by curly brackets `{}` with different pairs being seperated by a comma `,` and associated with a colon `:`. The values can be assigned and accessed using square braces `[]`. Inversely however, the keys cannot be accessed that when using the respective corresponding values.\n", "\n", "Dictionaries have no concept of order among elements. It is incorrect to say that the elements are \"out of order\"; they are simply unordered.\n", "\n", "Useful methods in the context of dictionaries are for example `.keys()` that prints only the keys of a certain dictionary or `.values()` that prints only the values of a certain dictionary." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Schweiz': 'Zürich', 'UK': 'London', 'Frankreich': 'Paris'}\n", "London\n", "dict_keys(['Schweiz', 'UK', 'Frankreich'])\n", "dict_values(['Zürich', 'London', 'Paris'])\n" ] } ], "source": [ "# You can run the code in this cell to create a simple dictionary\n", "\n", "d1 = {\"Schweiz\" : \"Zürich\", \"UK\" : \"London\", \"Frankreich\" : \"Paris\"}\n", "\n", "print(d1)\n", "\n", "print(d1[\"UK\"])\n", "\n", "print(d1.keys()) # Print only the keys of the dictionary\n", "\n", "print(d1.values()) # Print only the values of the dictionary\n", "\n", "# print(d1[\"Paris\"])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Hallo': 'Hello', 'Auf Wiedersehen': 'Goodbye', 'Gut': 'Good', 'Schlecht': 'Bad'}\n" ] } ], "source": [ "# Here is another example of a dictionary\n", "\n", "GerEngDictionary = {\"Hallo\" : \"Hello\", \"Auf Wiedersehen\" : \"Goodbye\", \"Gut\" : \"Good\", \"Schlecht\" : \"Bad\"}\n", "\n", "print(GerEngDictionary)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Type Conversion / Variable Type Specification\n", "Sometimes, conversions between the different data types or - more generally - value type specifications are necessary. To convert between types or specifcy variable types, you simply use the type name as a function.\n", "\n", "There are several built-in functions to perform this task which will return a new object representing the final (converted) value.\n", "\n", "| Function | Description |\n", "|----------|-------------|\n", "| int() | Converts to an integer |\n", "| float() | Converts to a floating-point number |\n", "| complex() | Converts to a complex number |\n", "| str() | Converts to a string representation |\n", "| tuple(l) | Converts l to a tuple |\n", "| list (l) | Converts l to a list |\n", "| dict(t) | Creates a dictionary, t must be a sequence of (key, value) tuples |\n", "\n", "Keep in mind, that not all conversions are possible, since they do not make sense. Converting a string into a float will not work through these in-built functions." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", "\n" ] } ], "source": [ "# Run the code in this cell to convert the data type of the variables below\n", "# The function type() checks the data type of a variable. We use type() to show that the data type conversion worked\n", "\n", "a = 4.0\n", "\n", "print(type(a))\n", "print(type(int(a)))\n", "\n", "b = 5.0\n", "\n", "print(type(b))\n", "print(type(str(b)))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] }, { "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'four'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'four'" ] } ], "source": [ "# Below is an example of a data type conversion that does not work\n", "# The value assigned to the variable a is the string \"four\"\n", "# Python does not understand the meaning behind the word four, so cannot complete the conversion\n", "\n", "a = \"four\"\n", "\n", "print(type(a))\n", "print(type(int(a)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Operators\n", "\n", "Operators are used when you want to perform mathematical operations on one or more variables or values. Operators are nothing else than functions which are already built-in in Python. Operators can be categorized into different types: Arithmetic, boolean, assignment, logical, membership and identity operators that are each discussed in more detail below. Another important aspect of operators, is how they are ranked, meaning if several operators are used in one line of code, which operators are executed first.\n", "\n", "*(Note: This chapter contains verbatim quotes from: https://www.tutorialspoint.com/python/python_basic_operators.htm)*\n", "\n", "## Types of Operators\n", "\n", "### Arithmetic Operators\n", "Arithmetic operators are basic mathematical functions. They work with number values, but some also work with strings and other values.\n", "\n", "| Symbol | Name | Description | Example |\n", "|--------|------|-------------|--------------|\n", "| + | Addition | Adds values on either side of the operator | 1 + 1 = 2 |\n", "| - | Subtraction | Subtracts values on right side of the operator from left side of operator | 2 - 1 = 1 |\n", "| * | Multiplication | Multiplies the value on the right side of the operator with the value on the left side | 2 * 3 = 6 |\n", "| / | Division | Divides the value on the left side of the operator by the value on the right side | 6 / 3 = 2 |\n", "| % | Modulus | Divides the value on the left side of the operator by the value on the right side and returns the remainder | 7 % 3 = 1 |\n", "| ** | Exponent | Performs exponential (power) operation on the value on the left of the operator | 2 ** 3 = 8 |\n", "| // | Floor Division | The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity) | 9 // 2 = 4 |" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "1\n", "6\n", "2.0\n", "1\n", "8\n", "4\n" ] } ], "source": [ "# You can run the code in this cell to perform some basic math in Python\n", "\n", "print(1 + 1)\n", "\n", "print(2 - 1)\n", "\n", "print(2 * 3)\n", "\n", "print(6 / 3)\n", "\n", "print(7 % 3)\n", "\n", "print(2 ** 3)\n", "\n", "print(9 // 2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n", "10\n", "26\n", "125\n" ] } ], "source": [ "# It is also possible to combine these operators\n", "# We will cover below the precedence the operators take. Keep in mind that expressions within parentheses are executed first.\n", "\n", "print(2 + 3 * 2)\n", "\n", "print((2 + 3) * 2)\n", "\n", "print(5 ** 2 + 1)\n", "\n", "print(5 ** (2 + 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Boolean Operators\n", "Boolean operators compare the values on either sides and decide the relation among them. They are also called Comparison operators or Relational operators. The output of an operation with a Boolean operator will be a Boolean value `True` or `False`.\n", "\n", "| Symbol | Description | Example |\n", "|--------|-------------|---------|\n", "| == | Checks if values on either side are equal | 2 == 2 returns True |\n", "| != | Checks if values on either side are not equal | 2 != 3 returns True |\n", "| > | Value on the left greater than value on the right | 5 > 2 returns True |\n", "| < | Value on the left smaller than value on the right | 5 < 2 returns False |\n", "| >= | Value on the left greater or equal to value on the right | 3 >= 2 returns True |\n", "| <= | Value on the left smaller or equal to value on the right | 2 <= 2 returns True |\n", "\n", "Boolean operators cannot be used between all values. While it makes sense to compare an integer to a float, comparing a string to a float will lead to an error." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "False\n", "True\n", "True\n" ] } ], "source": [ "# You can run the code in this cell to use Boolean operators in practise\n", "\n", "print(2 == 2)\n", "\n", "print(2 != 3)\n", "\n", "print(5 > 2)\n", "\n", "print(5 < 2)\n", "\n", "print(3 >= 2)\n", "\n", "print(2 <= 2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] }, { "ename": "TypeError", "evalue": "'>' not supported between instances of 'str' and 'int'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2.0\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"three\"\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'str' and 'int'" ] } ], "source": [ "# In this code, Python can perform the comparison between a float and an integer\n", "# However, the relation between a string and an integer will lead to an error\n", "\n", "print(2.0 > 5)\n", "\n", "print(\"three\" > 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Assignment Operators\n", "Assignment operators are used to define variables or to update the value of variables. We have already used the most basic one, the equality sign =, to define functions.\n", "\n", "| Symbol | Description | Example |\n", "|--------|-------------|---------|\n", "| = | Assigns value on the right side of the operator to the variable on the left side | a = b assigns value of b to a |\n", "| += | Adds value on right side of operator to value on the left and assigns the result to the value on the left | a += b returns the same as a = a + b |\n", "| -= | Subtracts value on the right side of the operator from the value on the left and assigns the result to the value on the left | a -= b returns the same result as a = a - b |\n", "| *= | Multiplies value on the right with the value on the left side of the operator and assigns the result to the value on the left | a *= b returns the same result as a = a * b |\n", "| /= | Divides the value on the left side of the operator with the value on the right and assigns the result to the value on the left | a /= b returns the same result as a = a / b |\n", "| %= | Divides the value on left side of the operator by the value on the right side and assigns the remainer to the value on the left side | a %= b returns the same result as a = a % b |\n", "| \\**= | Performs exponential (power) operation on value on the left side and assigns the result to the value on the left side | a \\**= 2 returns the same result as a = a \\** 2 |\n", "| //= | It performs floor division on operators and assigns value to the left operand | c //= a returns the same result as c = c // a |" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# Run the code in this cell to use the Addition/Assignment operator\n", "\n", "a = 1\n", "\n", "b = 2\n", "\n", "a += b\n", "\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.0\n" ] } ], "source": [ "# Run the code in this cell to use the Division/Assignment operator\n", "\n", "a = 15\n", "\n", "b = 5\n", "\n", "a /= b\n", "\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Logical Operators\n", "Logical Operators are used to reverse the logical state of its operand. They can also add conditions that need to be met for a statement to be true.\n", "\n", "| Symbol | Description | Example |\n", "|--------|-------------|---------|\n", "| and | Adds a condition that needs to be met | a < 4 and a > 2 only returns True if integer value of a is 3 |\n", "| or | Adds alternative condition | a < 4 or a < 2 will return True, if either condition is fulfilled |\n", "| not | Reverses logical state | Not (a < 4 and a > 2) will return False if integer value of a is 3 |" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "# You can run the code in this cell to use the logical operators\n", "\n", "a = 3\n", "\n", "print(a < 4 and a > 2)\n", "\n", "print(a < 4 or a < 2) # This statement returns True, since either condition can be fulfilled to return True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Membership Operators\n", "Membership operators test if a certain value is part of a string, list or tuple.\n", "\n", "| Symbol | Description | Example |\n", "|--------|-------------|---------|\n", "| in | Checks if a certain value is part of a certain sequence. Returns True if it is, otherwise it returns False | a in b returns True if a is part of sequence b |\n", "| not in | Checks if a certain value is not part of a certain sequence. Returns True if it is not, otherwise (i.e. if the value is part of the sequence) it returns False | a not in b returns False, if a is part of sequence b |" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "True\n", "False\n", "True\n", "True\n", "False\n", "True\n", "False\n" ] } ], "source": [ "# You can run the code in this cell to create a list and then check if certain values are part of the list\n", "\n", "list = [1, 3, 5, 7, 9]\n", "\n", "print(1 in list)\n", "\n", "print(2 in list)\n", "\n", "print(3 in list)\n", "\n", "print(4 in list)\n", "\n", "print(5 in list)\n", "\n", "print(6 not in list)\n", "\n", "print(7 not in list)\n", "\n", "print(8 not in list)\n", "\n", "print(9 not in list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Identity Operators\n", "\n", "Identity Operators compare the memory location of two objects.\n", "\n", "| Symbol | Description | Example |\n", "|--------|-------------|---------|\n", "| is | Checks if the values on either side point to the same location in memory. If they do it returns True, otherwise it returns False | a is b will return True if id(a) equals id(b) |\n", "| is not | Checks if the values on either side point to a different location in memory. If they do it returns True, otherwise (i.e. if they point to the same location in memory) it returns False | a is not b will return True if id(a) does not equal id(b) |" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "True\n", "True\n" ] } ], "source": [ "# You can run the code in this cell to create variables and check them against each other\n", "\n", "a = 2\n", "\n", "b = 3\n", "\n", "c = 4\n", "\n", "d = 2\n", "\n", "e = 3\n", "\n", "print(a is d)\n", "\n", "print(a is b)\n", "\n", "print(c is 2 * a)\n", "\n", "print(b ** a is e ** d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ranking of Operators\n", "Operators are executed in a certain order. Being aware of the priority that operators take when Python executes code is crucial. The table below shows which operations are executed first in descending order.\n", "\n", "| Precedence | Operator | Description |\n", "|------------|----------|-------------|\n", "| 1 | ** | Exponentiation (Raise to the power) |\n", "| 2 | * / % // | Multiplication, Division, Modulo and Floor Division |\n", "| 3 | + - | Addition and Subtraction |\n", "| 4 | > < >= <= | Comparison operators |\n", "| 5 | == != <> | Equality operators |\n", "| 6 | = += -= *= /= %= \\**= //= | Assignment operators |\n", "| 7 | 'is' 'is not' | Identity operators |\n", "| 8 | 'in' 'not in' | Membership operators |\n", "| 9 | 'not' 'or' 'and' | Logical operators |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Functions\n", "\n", "## What are functions?\n", "\n", "A **function** is a block of code which relates input to output and can be used for different inputs (variables, values). \n", "\n", "One can either use **self-created functions** or already **built-in functions** in Python. Self-created functions refer to functions that are defined and coded by a person, while built-in functions are already pre-coded and can simply be applied. \n", "Some examples for built-in functions are:\n", "* `print()` - which returns whatever is written within the parantheses,\n", "* `type()` - which was used before to find the class of a value,\n", "* and all the operators (such as +,-,OR,etc.). \n", "\n", "These examples for built-in functions can simply be called/used. However, some functions are not already built-in in the Python interpreter, for those a library with pre-coded algorithms first needs to be imported. An algorithm describes a step-by-step approach on how to solve a problem. However, in this tutorial, the terms functions and algorithms can be used interchangeably.\n", "\n", "## Self-created functions\n", "How self-created functions can be defined and later on called will be looked at and as well why functions make coding more efficient.\n", "\n", "### Define a function\n", "A function is structured as follows: \n", "\n", "The abbreviation def stands for define. What comes after def is the name of the function(function_name). In parentheses (input) a parameter/placeholder is added for whatever value or variable will later on be plugged into the function. Then below this, the actual definition of the function follows which is referred to as \"documentation string\" or \"doc string\" for short. The doc string is used to give a brief explanation of what the function does. This allows other users (or the same user, if using the function later one) to get a quick summary of a function's purpose. The definition of the function is ended with return which gives back the output of the function." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def function_name(input):\n", " \"documentation string\"\n", " return output" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# To look up the doc string of a function, enter the function name followed by ?\n", "# The information will be displayed when you run the code in this cell\n", "\n", "function_name?" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function function_name in module __main__:\n", "\n", "function_name(input)\n", " documentation string\n", "\n" ] } ], "source": [ "# Another option is to use the built-in function help followed by the object in parentheses\n", "\n", "help(function_name)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(...)\n", " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", " \n", " Prints the values to a stream, or to sys.stdout by default.\n", " Optional keyword arguments:\n", " file: a file-like object (stream); defaults to the current sys.stdout.\n", " sep: string inserted between values, default a space.\n", " end: string appended after the last value, default a newline.\n", " flush: whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "# More complex functions require more documentation\n", "\n", "help(print)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function which calculates the square of its input and call it `square_function()`:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "def square_function(x):\n", " \"function that calculates the second power\"\n", " result = x**2\n", " return result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Name a function\n", "For naming a function the same rules like for naming a variable apply: \n", "* Use lowercase characters, \n", "* avoid non-letter characters (like !,&, %,,...),\n", "* underscore and numbers can be used, but not at the beginning of the name, \n", "* keywords (e.g. if, while, return,...) cannot be used as a name,\n", "* be as explicit as possible.\n", "\n", "### Call a function\n", "To call or in other words to use a function, the name of the function together with the desired input in parentheses should be run:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "square_function(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Why functions are efficient\n", "To illustrate why functions make programming more efficient, the `square_function()` is used to calculate the square of all numbers form 0 to 100:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mylist = range(100)\n", "result_list = []\n", "i=0\n", "for i in mylist:\n", " result_list.append(square_function(i))\n", "print(result_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can be observed that the square_function not only saves a lot of time, as it would have been cumbersome to calculate the square of every number individually, it also helped to avoid errors like missing out a number.\n", "\n", "Here is a short guide helping to understand the code above as not all that is used has been treated yet:\n", "* `range(x)` creates a list of consecutive integeres from 0 to x,\n", "* `for i in mylist` is a loop which goes through every element in mylist (loops will be treated in a later tutorial), \n", "* `result_list = []` means that the list of all results at the beginning is empty,\n", "* `result_list.append(square_function(i))` means that every output of the `square_function()` is appended to the `result_list`,\n", "* `print(result_list)` shows the list of all results.\n", "\n", "### Difference between defining variables inside and outside of functions\n", "There is one important feature of defining a variable on the top of your script. Such a variable is called global and can be accessed from anywhere in the script after the variable was defined. Here is an example of defining a global variable." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "2\n" ] } ], "source": [ "a = 2\n", "\n", "def function(): \n", " print(a)\n", " \n", "function()\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When instead of only defining a variable at the beginning of a function, we define a variable inside the function, the relation and with it the priorities change. This can be seen in the following example." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n" ] } ], "source": [ "a = 2\n", "\n", "def function():\n", " a = 3\n", " print(a)\n", " \n", "function()\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a built-in keyword in python that gives us more versatility in chosing when to use a globally defined variable or not. To use it, simply type global before the assigned variable name. Here is an example to illustrate the usage of the global keyword. The variable that we are referring to inside the function is now the global c, defined at the beginning of the script. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n", "40\n" ] } ], "source": [ "c = 20\n", "\n", "def change_value(new_c): \n", " global c \n", " c = new_c\n", " \n", "print(c)\n", "\n", "change_value(40)\n", "\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is another keyword that can be used when working with nested functions. We can use the keyword nonlocal when we want to avoid saving the variable locally, as in inside a function, but we want it saved a level higher. So instead of saving the variable inside the function, we save it at the beginning of the function, but also not globally, because there could be another function in between them. \n", "Let us first set up an example where we can illustrate the usage of nonlocal. " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "from inner: 40\n", "from outer: 30\n", "gloablly: 20\n" ] } ], "source": [ "d = 20\n", "\n", "def outer(): \n", " d = 30\n", " def inner():\n", " d = 40\n", " print('from inner:', d)\n", " \n", " inner()\n", " print('from outer:', d)\n", " \n", "outer()\n", "print('gloablly:', d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we now use the nonlocal keyword in the inner function, we will see that the value of the variable 'e' we will be using in this particular function will be 40, as defined in the inner function. So that when calling the function outer, we will have 'e' defined as 40 and not 30. " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "from inner: 40\n", "from outer: 40\n", "globally: 20\n" ] } ], "source": [ "e = 20 \n", "\n", "def outer(): \n", " e = 30\n", " def inner():\n", " nonlocal e\n", " e = 40\n", " print('from inner:', e)\n", " \n", " inner()\n", " print('from outer:', e)\n", " \n", "outer()\n", "print('globally:', e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if for example we want to refer to the global value of the variable in the inner function, without affecting the outer function, we could just set our variable f to global before defining it in the inner function. This way, the global variable will be changed to 40, but the definition of our variable in the outer function will not be affected. " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "from inner: 40\n", "from outer: 30\n", "gloablly: 40\n" ] } ], "source": [ "f = 20\n", "\n", "def outer(): \n", " f = 30\n", " def inner(): \n", " global f\n", " f = 40\n", " print('from inner:', f)\n", " \n", " inner()\n", " print('from outer:', f)\n", " \n", "outer()\n", "print('gloablly:', f)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Built-in functions\n", "Although self-created functions can be defined in Python, whenever possible, built-in functions should be used as they are better designed than self-created functions. There exist two types of built-in functions in Python: Those within Python which can be simply called and those within libraries which first need to be imported.\n", "\n", "## Built-in functions within Python\n", "For a start, built-in functions which form part of Python are looked at. These built-in functions can simply be called by using the name of the function and adding an input into its parentheses. Refer to the list below for common built-in functions (some might look familiar since we have encountered them in previous sections). This list is not exclusive, a more comprehensive list on built-in functions can be found __[here](https://data-flair.training/blogs/python-built-in-functions/)__.\n", "\n", "| Function | Description |\n", "|----------|------------------------------------------------------|\n", "|abs() | Returns the absolute value |\n", "|append() | Adds single elements to a list |\n", "|count() | Returns number of occurences of an element in a list |\n", "|len() | Returns length of a list |\n", "|lower() | Returns lowercase string |\n", "|upper() | Returns uppercase string |\n", "|max() | Returns largest element of a list |\n", "|min() | Returns smallest element of a list |\n", "|print() | Returns what is given within parantheses |\n", "|return() | Like print, used for defining functions |\n", "|sum() | Adds up items of a list |\n", "|round() | Returns rounded value |\n", "|help() | Shows information about function |\n", "\n", "## Built-in functions within libraries\n", "Secondly, we consider built-in functions for which first a library needs to be imported. A library contains one or more algorithms which are already pre-coded, in order that they can be called as soon as the library is imported. Note that the terms modules, packages and libraries are used interchangeably for the purposes of this tutorial.\n", "\n", "### How to import libraries\n", "The following three sets of code import libraries into Python:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random #imports the entire library\n", "from numba import jit #imports parts of a library\n", "import numpy as np #imports a library and gives it a shorter name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### How to install libraries\n", "When running into an error whilst importing a library, it could be because the library has not been installed yet into your Python. When coding in Jupyter Notebook, some libraries are already installed and can simply be imported if needed, as for example numpy, scipy or random. Others need to be installed first. This is done by using **pip**. The pip is an installer program which is included in any later version than Python 3.4.\n", "\n", "Remember from [Tutorial 2](https://nbviewer.jupyter.org/github/drarnau/Programming-for-Quantitative-Analysis/blob/master/01_Preliminaries.ipynb) that any pip command can only be run outside Jupyter Notebook, meaning in the command line as follows (let us assume we want to install the library \"theano\"):\n", "\n", "\n", "(base) C:\\users\\username pip install theano\n", "\n", "Further information on how to install libraries can be found __[here](https://packaging.python.org/tutorials/installing-packages/#requirements-for-installing-packages)__.\n", "\n", "### Main libraries\n", "Here is an overview on the most common libraries and packages in Python. Further libraries can be found __[here](https://wiki.python.org/moin/UsefulModules)__.\n", "\n", "| Library | Description |\n", "|:-- |--------------------------------------------------------------------------------------------|\n", "|**Math** | | \n", "| math | Arithmetic functions |\n", "|**Data Science** | |\n", "| numpy | How to deal with data |\n", "| random | Generates random numbers |\n", "| scipy | Manipulating and visualizing data |\n", "| numba | Translates Python code to optimized and faster machine code |\n", "|**Machine-learning** | |\n", "| scikit-learn | Data mining and machine learning tasks |\n", "| theano | Uses GPU for intensive computation |\n", "| TensorFlow | Goolge framework for machine learning based projects |\n", "|**Web scraping and natural numbers processing** | |\n", "| scrapy | Web scraping tasks |\n", "| NLTK | Natural number processing (analyzing texts and finding correlations) |\n", "| pattern | Includes all web scraping and natural numbers processing libraries in one |\n", "|**Plotting and visualization** | |\n", "| matplotlib | Data visualization |\n", "| seaborn | Based on matplotlib library, to generate graphs in an easier way than matplotlib |\n", "| bokeh | Interactive, zoomable graphs. Based on modern web browser, uses javascript to render graph |\n", "| ggplot | Data visualization using grammar of graphics method (derivation of R's ggplot2) |\n", "\n", "### Matplotlib\n", "Discussing all the above libraries individually would go beyond the scope of this tutorial, however, the library **matplotlib** is introduced in more detailed as it is very useful for visualizing data.\n", "\n", "Matplotlib is a Python 2D plotting library which can be used to visualize data as graphs, barcharts, histograms, timelines as well as further sorts of illustrations. This library is increasingly popular as it is considered a favorable alternative to the popular software **MATLAB**. \n", "\n", "MATLAB (*matrix laboratory*) is a programming language developed by MathWorks for solving mathematical problems and providing graphical representation of the results. Matlab is mainly used in the fields of engineering, science and economics. \n", "The Python library Matplotlib together with the Python libraries numpy and scipy (see: [Main libraries](#link_table)) is a good alternative to MATLAB as Python is free, whilst a license for MATLAB is rather expensive. \n", "\n", "The goal is to plot the function: $y = 2x$. How this is achieved is looked at step-by-step:\n", "1. Generate the frame for the graph: \n", "*Notice that after every step `plt.show()` is used so that you can see how the graph looks like so far*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import matplotlib:\n", "import matplotlib.pyplot as plt \n", "\n", "# Generate the frame:\n", "fig = plt.figure() # plots figure (can be thought of as area in which all needed for plot incl. axes, graph, labels is contained)\n", "ax = plt.axes() # without axes the figure is not bounded and would not be displayed\n", "\n", "#Show the graph:\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Define the length of the axes and label them:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import matplotlib:\n", "import matplotlib.pyplot as plt \n", "\n", "# Generate the frame:\n", "fig = plt.figure()\n", "ax = plt.axes() \n", "\n", "\n", "# Define length of axes and label them:\n", "plt.axis([0,10, 0,20]) # the first two values define from where to where x-axis goes, the second two values for the y-axis\n", "plt.grid() # adds a grid s.t. points on graph can be read off easier\n", "plt.ylabel('Y-axis')\n", "plt.xlabel('X-axis')\n", "\n", "#Show the graph:\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Plot the function: *Notice that for this step we also need to make use of the library numpy.*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import matplotlib and numpy:\n", "import matplotlib.pyplot as plt \n", "import numpy as np\n", "\n", "# Generate the frame:\n", "fig = plt.figure()\n", "ax = plt.axes() # without axes the figure cannot be displayed\n", "\n", "# Define length of axes and label them:\n", "plt.axis([0,10, 0,20]) # the first two values define from where to where x-axis goes, the second two values for the y-axis\n", "plt.grid() # adds a grid, so that points on graph can be read off easier\n", "plt.ylabel('Y-axis')\n", "plt.xlabel('X-axis')\n", "\n", "# Define x and y such that they adhere to the function and then plot them:\n", "x = np.arange(0,10) # np.arange defines x as a range of evenly arranged inputs from 0 to 10\n", "y = 2*x\n", "ax.plot(x, y)\n", "\n", "#Show the graph:\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Change linestyle and color of graph:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# Import matplotlib and numpy:\n", "import matplotlib.pyplot as plt \n", "import numpy as np\n", "\n", "# Generate the frame:\n", "fig = plt.figure()\n", "ax = plt.axes() \n", "\n", "# Define length of axes and label them:\n", "plt.axis([0,10, 0,20]) # the first two values define from where to where x-axis goes, the second two values for the y-axis\n", "plt.grid() # adds a grid, so that points on graph can be read off easier\n", "plt.ylabel('Y-axis')\n", "plt.xlabel('X-axis')\n", "\n", "# Define x and y such that they adhere to the function and then plot them:\n", "x = np.arange(0,10) # np.arange defines x as a range of evenly arranged inputs from 0 to 10\n", "y = 2*x\n", "ax.plot(x, y, 'r-') # solid red\n", "ax.plot(x, y+1, 'k:') # dotted black\n", "ax.plot(x, y+2, 'g-.') # dash-dot green\n", "ax.plot(x, y+3, 'c:') # dotted blue\n", "\n", "#Show the graph:\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is one way of how to plot a function with matplotlib. For further insights into matplotlib go to __[Matplotlib.org](https://matplotlib.org/)__ or __[Python online handbook](https://jakevdp.github.io/PythonDataScienceHandbook/04.00-introduction-to-matplotlib.html)__." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Encryption and Decryption\n", "Encryption is an algorithm for transforming information in order to make it unintelligible. Decryption is an algorithm which transforms encrypted information so that it is intelligible again.\n", "Both encryption and decryption are socalled cryptographic algorithms. In most cases, for encryption and decryption two related algorithms are applied.\n", "\n", "With most modern cryptography, the ability to keep encrypted information secret is not necessarily dependent on the cyptographic algorithm as this is widely known, but on a number called a key that must be used with the algorithm to produce an encrypted result or to decrypt previously encrypted information. Decryption with the correct key is simple. Decryption without the correct key is very difficult and in some cases impossible.\n", "\n", "![Encryption Decryption](imgs/03_EncryptionDecryption.png)\n", "Source image: https://stackoverflow.com/questions/4948322/fundamental-difference-between-hashing-and-encryption-algorithms\n", "\n", "## Encryption\n", "When encrypting a plain text, output and input of the encryption algorithm are defined as follows:\n", "\n", "* **Input** = A string of characters and spaces.\n", "* **Output** = A list of numbers and spaces.\n", "\n", "To make an example on how to relate the above input to its output, the following **encryption algorithm** is defined:\n", "* Go through the elements in the string:\n", " * If the element is a **space**: Append it to the output list.\n", " * If the element is **not a space**: \n", " 1. Find position in English alphabet, \n", " 2. Square position,\n", " 3. Append it to the output list.\n", "\n", "The code below shows how to apply the encryption algorithm to encrypt a plain text." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "import string\n", "\n", "def encrypt(mystring): # as before with functions, we define the encryption algorithm and give it the name encrypt\n", " mystring = mystring.lower() # lower to transform all characters into lowercase\n", " mylist = [] # mylist is the output list, which is empty in the beginning\n", "\n", " for character in mystring: # for character in mystring is a loop (treated in later tutorial) which goes through all character within mystring\n", " if character.isspace(): # checks whether character under scrutiny is a space\n", " mylist.append(character) # If the character is a space: add it to mylist (output list)\n", " else: \n", " mynumber = string.ascii_lowercase.index(character) # If character is not a space, find position in English alphabet\n", " # call this position mynumber\n", " mynumber = (mynumber)**2 # Square the position mynumber\n", " mylist.append(mynumber) # add it to the output list\n", " return mylist # return the output list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As an example, the plain text: \"python is fun\" is encrypted. " ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[225, 576, 361, 49, 196, 169, ' ', 64, 324, ' ', 25, 400, 169]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "encrypt(\"python is fun\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that if you use any non-letter characters (like ?, . , !, ...), you will run into an error as these do not form part of the English alphabet.\n", "\n", "## 5.2. Decryption\n", "When decrypting an encrypted text, output and input are defined as follows:\n", "* **Input** = A list of numbers and spaces.\n", "* **Output** = A string of characters and spaces.\n", "\n", "Notice that this is the reverse of the encryption, as with decryption we try to get back from the encrypted text to the plain text. Therefore, the decryption algorithm needs to be the reverse of what was used to encrypt the text:\n", "\n", "* Go through all elements in the list:\n", " * If the element is a space: Append it to the output string.\n", " * If the element is not a space:\n", " 1. Take square root,\n", " 2. Find what letter it is assigned to in the English alphabet,\n", " 3. Append it to the output string.\n", "\n", "The code below shows how to apply the decryption algorithm to decrypt an encrypted code:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "import string\n", "\n", "def decrypt(mylist):\n", " mystring = \"\" # as it is a string we use \"\" and not [] to show that there is nothing assigned yet to mystring\n", " \n", " for element in mylist: # as before: loop which goes through each element within mylist\n", " if element == \" \": \n", " mystring = mystring + element # if the element is a space, it gets added to the output list\n", " else: # if the element is a number,\n", " element = int(element**0.5) # the square root is taken of that element (to the power of 0.5)\n", " # then int() converts any floats to integers by rounding, as only for integers position in English alphabet can be found\n", " myletter = string.ascii_lowercase[element] # find associated letter in British alphabet, call it myletter\n", " mystring = mystring + myletter #add my letter to mystring\n", " return mystring # return the output string (mystring)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check whether the decryption algorithm is correct, the encrypted text **mylist** is decrypted and it should return the plain text \"python is fun\"." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python is fun'" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mylist = [225, 576, 361, 49, 196, 169, ' ', 64, 324, ' ', 25, 400, 169]\n", "decrypt(mylist) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To double-check: If we encrypt a plain text and then decrypt it again, we should get the plain text: " ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'python is a programming language'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "decrypt(encrypt(\"python is a programming language\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, the two functions `decrypt()` and `encrypt()` can be used to communicate secret messages. Make sure that the recipient is knowledgable about the algorithms that were applied in order to encrypt the secret message." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }