Commit 0cae17ba authored by Marvin Kastner's avatar Marvin Kastner
Browse files

Add introductory Jupyter Notebooks

parent 5f1d1db7
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contents\n",
"\n",
"* [1 Getting started](#t1)\n",
" * [Basic syntax for statements](#t11)\n",
"* [2 Variables & Values](#t2)\n",
"* [3 Operators](#t3)\n",
" * [3.1 Arithmetic Operators](#t31)\n",
" * [3.2 Relational Operators](#t32)\n",
" * [3.3 Boolean](#t33)\n",
" * [3.4 Regular Operators](#t34)\n",
" * [3.5 Bitwise Operators](#t35)\n",
"* [4 Built-in Functions](#t4)\n",
" * [4.1 Converting values](#t41)\n",
" * [4.2 Mathematical functions](#t41)\n",
" * [4.3 Simplifying Arithmetic Operations](#t41)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1 Getting started <a class=\"anchor\" id=\"t1\"></a>\n",
"\n",
"Python can be used like a calculator. This module will cover the correct syntax expressions to evaluate and perform various operations using Python.\n",
"\n",
"## Basic syntax for statements <a class=\"anchor\" id=\"t11\"></a>\n",
"The basic rules for writing simple statements and expressions in Python are:\n",
"* No **spaces or tab** characters allowed at the start of a statement: Indentation plays a special role in Python (see the section on control statements) and can cause issues when compiling or executing code. For now simply ensure that all statements start at the **beginning of the line**.\n",
"* The '#' character indicates that the rest of the line is a comment\n",
"* Statements finish at the end of the line:\n",
" * Except when there is an open bracket or parenthesis:\n",
"```python\n",
"1+2\n",
"+3 #illegal continuation of the sum\n",
"(1+2\n",
" + 3) # perfectly OK even with spaces\n",
"```\n",
" * A single backslash at the end of the line can also be used to indicate that a statement is still incomplete \n",
"```python\n",
"1 + \\\n",
" 2 + 3 # this is also OK\n",
"```\n",
"The jupyter notebook system for writting Python combines text (like this) with Python statements. Try typing something into the cell (box) below and press the 'run cell' button above (triangle+line symbol) to execute it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1+2+3+4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python has extensive help built in for any questions your have that are not covered in these modules. You can execute **help()** for an overview or **help(x)** for any library, object or type **x** to get more information. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"help()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2 Variables & Values <a class=\"anchor\" id=\"t2\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A name that is used to denote something or a value is called a **variable**. In python, variables can be declared and values can be assigned to it as follows,"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 2 # anything after '#' is a comment\n",
"y = 5 # 'y' is the variable while 5 is the value\n",
"xy = 'Hey' # variables can be text as well as numbers\n",
"print(x+y, xy) # print() can be used to display the values/outputs of the variables specified"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiple variables can be assigned with the same value."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = y = 1\n",
"print(x,y) # not really necessary as the last value in a bit of code is displayed by default"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The basic data types that are built into Python include `float` (floating point numbers), `int` (integers), `str` (unicode character strings) and `bool` (boolean). Some examples of each:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2.0 # a simple floating point number\n",
"1e100 # a googol, the \"e\" denotes the number beforehand muliplied by 10 to the power of the number following it\n",
"-1234567890 # an integer\n",
"True or False # the two possible boolean values\n",
"'This is a string'\n",
"\"It's another string\"\n",
"print(\"\"\"Triple quotes (also with '''), allow strings to break over multiple lines.\n",
"Alternatively \\n is a newline character (\\t for tab, \\\\ is a single backslash)\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3 Operators <a class=\"anchor\" id=\"t3\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.1 Arithmetic Operators <a class=\"anchor\" id=\"t31\"></a>\n",
"These symobols denote what keystroke to use for arithmetic operators."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Symbol | Task Performed |\n",
"|----|---|\n",
"| + | Addition |\n",
"| - | Subtraction |\n",
"| / | division |\n",
"| % | mod (finds remainder after division)|\n",
"| * | multiplication |\n",
"| // | floor division (rounds to nearest significant digit)|\n",
"| ** | to the power of |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1+2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"2-1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1*2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"3/4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In many languages (and older versions of python) 1/2 = 0 (truncated division). In Python 3 this behaviour is captured by a separate operator that rounds down: (ie a // b$=\\lfloor \\frac{a}{b}\\rfloor$)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(3/4)\n",
"print(3//4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"15%10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python natively allows (nearly) infinite length integers while floating point numbers are double precision numbers:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"11**300"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"11.0**300"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.2 Relational Operators <a class=\"anchor\" id=\"t32\"></a>\n",
"These operators return **True or False** depending on the validity of the statement made."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Symbol | Task Performed |\n",
"|----|---|\n",
"| == | True, if it is equal |\n",
"| != | True, if not equal to |\n",
"| < | less than |\n",
"| > | greater than |\n",
"| <= | less than or equal to |\n",
"| >= | greater than or equal to |\n",
"\n",
"Note the difference between `==` (equality test) and `=` (assignment)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z = 2\n",
"z == 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z > 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparisons can also be chained in the mathematically obvious way. The following will work as expected in Python:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"0.5 < z <= 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.3 Boolean <a class=\"anchor\" id=\"t33\"></a>\n",
"\n",
"Boolean expressions are used to combine several requirements which need to be fulfilled.\n",
"If you query a database at a library and you look for articles which include \"cloning\", \"humans\", and \"ethics\", this connection narrows you down to less books.\n",
"You would connect the terms with an `and`.\n",
"![](https://lgimages.s3.amazonaws.com/data/imagemanager/7506/and.gif)\n",
"\n",
"If you on the other side connect the same search terms with an `or`, you broaden the results. \n",
"![](https://lgimages.s3.amazonaws.com/data/imagemanager/7506/or.gif)\n",
"\n",
"## 3.4 Regular Operators <a class=\"anchor\" id=\"t34\"></a>\n",
"\n",
"Regular operators are used to connect Boolean values, i.e. True and False.\n",
"If one of the values is not Boolean, the value is treated as one.\n",
"For this a very specific behavior is used, e.g. the number `0` is treated as False while e.g. the number `42` treated as True.\n",
"More information on how non-Boolean values are interpreted once you connect them with `and` or `or` can be found [here](https://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/boolean.html)\n",
"\n",
"## 3.5 Bitwise Operators <a class=\"anchor\" id=\"t35\"></a>\n",
"\n",
"Bitwise operators breakdown the statements to their binary representation of the data then assess it. \n",
"Some libraries, such as the library \"pandas\", have overwritten that behavior with their own logic."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Meaning | Regular Operator | Boolean Bitwise Operator | \n",
"|:-------------|------------------|--------------------------|\n",
"| Logical and | `and` | & |\n",
"| Logical or | `or` | $\\mid$ |\n",
"| Negation | `not` | ~ |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Boolean operators can be used in conjunction.\n",
"A `not` converts True and False to the opposite meaning, True becomes False and False becomes True.\n",
"An `and` takes two terms and is evaluated as False unless the two terms are both True.\n",
"Finally, an `or` takes two terms and always evaluates to True unless the two terms are both False.\n",
"\n",
"See more [here](https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap03/logical.html)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"not True, True and True, True and False, True or False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"not (True and False), \"==\", not True or not False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4 Built-in Functions <a class=\"anchor\" id=\"t4\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python comes with a wide range of functions. However many of these are part of stanard libraries like the `math` library rather than built-in. Libraries are pre written code created by other Python users that create subroutines and add functionality to Python.\n",
"\n",
"Since Python is constantly evolving, so are its libraries, here is a list of some commonly used libraries in 2018: https://bit.ly/2udZeow"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.1 Converting values <a class=\"anchor\" id=\"t41\"></a>\n",
"\n",
"Conversions from one data form to another, i.e. a integer to string, can be done by various commands because Python considers string '7' and float '7.0' two different things and they cannot be used interchangeably."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**int( )** converts a number to an integer. This can be a single floating point number, integer or a string. For strings the base can optionally be specified:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(int(7.7), int('111',2),int('7'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Similarly, the function **str( )** can be used to convert almost anything to a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(str(True),str(1.2345678),str(-2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.2 Mathematical functions <a class=\"anchor\" id=\"t42\"></a>\n",
"Mathematical functions include the usual suspects like logarithms, trigonometric fuctions, the constant $\\pi$ and so on. `math` is a library that can be imported into Phython as mentioned before:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"math.sin(math.pi/2)\n",
"from math import * # avoid having to put a math. in front of every mathematical function\n",
"sin(pi/2) # equivalent to the statement above"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.3 Simplifying Arithmetic Operations <a class=\"anchor\" id=\"t43\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**round( )** function rounds the input value to a specified number of places or to the nearest integer. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"round(5.6231)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"round(4.55892, 2)"
]
}
],
"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.7.1"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 1
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contents\n",
"\n",
"* [1 The Print Statement](#t1)\n",
"* [2 String Formating](#t2)\n",
"* [3 Other String Methods](#t3)\n",
"* [4 Accessing parts of strings](#t4)\n",
"* [5 Strings are immutable](#t5)\n",
"* [6 Type Function](#t6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with strings\n",
"\n",
"## 1 The Print Statement <a class=\"anchor\" id=\"t1\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As seen previously, The **print()** function prints all of its arguments as strings (another word for text), separated by spaces and followed by a linebreak. If the arguments are separated by commas, the output is separated by a space, otherwise the strings follow one another directly."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Hello World\")\n",
"print(\"Hello\", \"World\")\n",
"print(\"Hello Wo\" \"rld\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Variables or a combination of text and variables can also be entered into the **print()** function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"w = 'World'\n",
"print(\"Hello\", w)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that **print** is different in old versions of Python (2.7) where it was a statement and did not need parenthesis around its arguments.\n",
"\n",
"The print has some optional arguments to control where and how to print. This includes `sep` the separator (default space), which argues what should occupy spaced. Then `end` argument which adds characters to the end of the string. And `file` to specify where to write to the print function. \n",
"\n",
"Optional arguments are parameters within a command with preexisiting defaults that can be changed to customize the command. These 'parameters' can slightly alter a function's output. In this case it changes the separator and end of a print function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [