{
"cells": [
{
"cell_type": "markdown",
"id": "aa21b02c",
"metadata": {},
"source": [
"# Python - Introduction\n",
"In the following Notebooks, the key concepts of computer programs are discussed.\n",
"\n",
"\n",
"# Python-programs consist of statements, which are executed one by one\n",
"What is a Python-program? More generally, what is a (computer) program? All Python programs consist of a series of\n",
"statements or instructions. And these statements are executed one by one. (This order of execution can be changed e.g. by\n",
"conditions, which will be discussed later.) An example of a statement is the multiplication of two numbers.\n",
"Multiplication is represented in Python by the character `*`.\n",
"\n",
"If just one statement is entered, the Python program can be used as a simple calculator. In the following cell, there is\n",
"actually a complete Python program consisting of only one statement. The result of this operation, the output, is\n",
"written under the cell, after the cell has been executed. (The cell can be executed by clicking on it and entering\n",
"Shift-Return or CTRL-Return).\n",
"\n",
"If you change the values or the operator (i.e. the `*` or the `+`), you can do simple calculations."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c631533e",
"metadata": {},
"outputs": [],
"source": [
"5 + 7"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81faf770",
"metadata": {},
"outputs": [],
"source": [
"12 * 14"
]
},
{
"cell_type": "markdown",
"id": "046e08c7",
"metadata": {},
"source": [
"# Mathematical Operators\n",
"The `*` is called an operator in Python. The following table contains a list of frequently used arithmetic operators.\n",
"\n",
"| Description | Operator | Example | Result |\n",
"| ---------------- | -------- | -------- | ------------------ |\n",
"| Addition | + | 2 + 3 | 5 |\n",
"| Subtraction | - | 2 - 3 | -1 |\n",
"| Multiplication | * | 2 * 3 | 6 |\n",
"| Division | / | 7 / 3 | 2.3333333333333335 |\n",
"| Integer Division | // | 7 // 3 | 2 |\n",
"| Modulo | % | 7 % 3 | 1 |\n",
"| Exponentiation | ** | 2 ** 0.5 | 1.4142135623730951 |\n",
" \n",
"Python uses the operators precedence known from math. Multiplication and division are performed before addition and\n",
"subtraction. Parenthesis can be used to influence the order of execution.\n",
" \n",
"Additionally, a brief explanation of integer division and modulo: In primary school, you first learned about integer\n",
"division. If you divide 7 by 3 (and do not yet know any decimal numbers), the result is 2 with a remainder of 1. Integer\n",
"division and modulo lead to exactly these results. The integer division of `7 // 3` results in `2`. The result of the\n",
"modulo operation `7 % 3` is `1`, the remainder of the division. Especially the modulo operation is **frequently** used\n",
"in programming!\n",
"\n",
"In comparison to a calculator, it is possible to enter bigger formulas into a cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17ab570d",
"metadata": {},
"outputs": [],
"source": [
"1 + 2 * 3 * 4 * 5 - 6 / 7 % 8 ** 9 * 100"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2b8fb37",
"metadata": {},
"outputs": [],
"source": [
"1 + 2 * (3 * 4 * 5 - 6) / (7 % 8) ** 9"
]
},
{
"cell_type": "markdown",
"id": "a0fc7648",
"metadata": {},
"source": [
"## Fun Fact: Python can handle really large numbers\n",
"Unlike the calculator and unlike many other programming languages, Python can handle really large numbers. For integer\n",
"values (i.e. for whole numbers) there is no size constraint in Python apart from the internal memory of the machine\n",
"Python is running on. Because of this, you can multiply or potentiate pretty large numbers without problems."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52463ddc",
"metadata": {},
"outputs": [],
"source": [
"16485345486715646548961354165486 * 4561234354986145456455156446456"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1aacbd1",
"metadata": {},
"outputs": [],
"source": [
"2 ** 800000"
]
}
],
"metadata": {
"interpreter": {
"hash": "ac59ebe37160ed0dfa835113d9b8498d9f09ceb179beaac4002f036b9467c963"
},
"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"
}
},
"nbformat": 4,
"nbformat_minor": 5
}