python-kurs-softed/notebooks/basictypes.ipynb

267 lines
5.8 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Grundlegende Datentypen und Operationen"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Numerische Typen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Ganzzahlen (Integers)\n",
"\n",
"a = 42\n",
"b = 23\n",
"print(a)\n",
"print(b)\n",
"print(a + b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Fließkommazahlen (Floats)\n",
"\n",
"pi = 3.14159\n",
"e = 2.71828\n",
"print(pi)\n",
"print(type(pi))\n",
"print(e)\n",
"print(pi * e)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Implizite Umwandlung von numerischen Typen\n",
"\n",
"i = 3\n",
"f = 1.5\n",
"summe = a + f\n",
"print(i, f, summe)\n",
"print(type(e))\n",
"z = 1\n",
"n = 6\n",
"print(z / n)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Grundrechenarten\n",
"\n",
"# Integer\n",
"print(1 + 1)\n",
"print(1_000 - 1)\n",
"print(12 * 12) # Vorsicht: Asterisk (\"Sternchen\"), nicht x)\n",
"print(64 / 8)\n",
"\n",
"# Floats\n",
"print(0.5 + 3.3)\n",
"print(1.99 - 0.99)\n",
"print(20.0 * 1.19)\n",
"print(1.0 / 3.0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# X hoch Y\n",
"\n",
"x = 8\n",
"x2 = x ** 2\n",
"print(x2)\n",
"print(x ** 3)\n",
"print(pow(x, 3))\n",
"\n",
"print(x ** e)\n",
"print(pow(x, e)) # pow akzeptiert integer und floats"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Modulo\n",
"\n",
"x = 10\n",
"y = 3\n",
"rest = x % 3\n",
"print(rest)\n",
"print(type(rest))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = \"Dies ist ein String!\"\n",
"print(s)\n",
"s2 = 'Man kann einfache oder doppelte Anführungszeichen verwenden.'\n",
"print(s2)\n",
"s3a = 'Innerhalb der Anführungszeichen kann man den jeweils \"anderen Typ\" verwenden'\n",
"print(s3a)\n",
"s3b = \"Oder 'andersherum'.\"\n",
"print(s3b)\n",
"s4 = \"Alternativ kann man eine Backslash als \\\"Escape\\\"-Zeichen verwenden\"\n",
"print(s4)\n",
"s5 = \"String-Literale\" \", die direkt hintereinander\" \", durch Leerzeichen getrennt, stehen\" \"werden zusammengefügt.\"\n",
"print(s5)\n",
"s6 = (\"Mit Klammerung klappt das auch \"\n",
" \"über mehrere Zeilen \"\n",
" \"hinweg\")\n",
"print(s6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Längere Strings\n",
"\n",
"long_string = \"\"\"Dies ist ein langer String (\"triple quoted string\").\n",
"Er kann Zeilenumbrüche enthalten.\n",
"\n",
"Und Leerzeilen.\n",
"\n",
"Und auch 'einfache' oder \"doppelte\" Anführungszeichen.\n",
"\n",
"Er endet mit drei weiteren Anführungszeichen des gleichen Typs.\n",
"\"\"\"\n",
"print(long_string)\n",
"print(repr(long_string))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Escape Sequenzen\n",
"\n",
"# Zeilenumbrüche und Tabs\n",
"print(\"Erste Zeile\\r\\nZweite Zeile\") # Zeilenumbruch nach Windows-Art \n",
"print(\"Eine Zeile\\nNoch eine Zeile\") # Rest der Welt ;-)\n",
"print(\"Eins\\tZwei\\tDrei\")\n",
"print(\"1\\t2\\t3\")\n",
"\n",
"# Null und Hexadezimal- und Unicode-Sequenzen\n",
"print(repr(\"Dieser String enthält ein\\0Null-Zeichen\"))\n",
"print(\"Hexadezimal-Codes können wir druckbare (e.g. '\\x40') und nicht druckbare Zeichen (e.g. \\x7F) verwendet werden.\")\n",
"print('\\u2764') # Unicode"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# String-Operationen\n",
"\n",
"s1 = \"Dies ist der erste String.\"\n",
"s2 = \"Und dies der zweite.\"\n",
"s3 = s1 + s2\n",
"print(s3)\n",
"s4 = \"Ich habe es dir schon dreimal gesagt! \"\n",
"print(s4 * 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"klein = \"schrei nicht so laut!\"\n",
"print(klein.upper())\n",
"print(\"hallo\" == \"HALLO\".lower())\n",
"print(\"hallo\" == \"Hallo\".lower())\n",
"print(\"straße\".upper() == \"STRASSE\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hallo = \"Hallo\"\n",
"print(hallo)\n",
"print(hallo[1])\n",
"print(hallo[0] + 'e' + hallo[2:])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Aber das geht nicht!:\n",
"\n",
"hallo[1] = 'e'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "softed",
"language": "python",
"name": "softed"
},
"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.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}