sq_01_python_intro/jupyter_book/04_bedingungen_variablen.ipynb

196 lines
3.9 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "markdown",
"id": "c17b2056-f461-4edd-bcbc-c53a1e8aafe4",
"metadata": {},
"source": [
"# Bedinungen & Variablen\n",
"## Bedingungen\n",
"\n",
"Achtet auf Doppelpunkte und Einrückung!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ac3cb812-7fb5-40ed-9328-864d9dd7a1b8",
"metadata": {},
"outputs": [],
"source": [
"def my_min(a, b):\n",
" if a < b:\n",
" return a\n",
" else:\n",
" return b"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "be0a975d-2fb9-4be2-ad94-46de2819c72f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-5"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_min(-2, -5)"
]
},
{
"cell_type": "markdown",
"id": "e1f7fc12-e2e3-4c7e-a949-61d3ca9360dc",
"metadata": {},
"source": [
"## Kommentare\n",
"\n",
"Programme können durch Kommentare leichter verstanden werden.\n",
"\n",
" \"\"\" Das ist ein\n",
" mehrzeiliger\n",
" Kommentar \"\"\"\n",
" \n",
" sin(0) # Kommentar bis Ende der Zeile\n",
" \n",
" Vernünftige Funktionsnamen sind aber noch wichtiger!"
]
},
{
"cell_type": "markdown",
"id": "d77d67a3-6161-46bf-bb3f-754391b729c8",
"metadata": {},
"source": [
"## Variablen\n",
"\n",
"In Funktionen habt ihr schon Variablen verwendet.\n",
"Diese lassen sich aber auch direkt zuweisen:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4055653c-cbec-464b-aeac-a8d140fdb815",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"35"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 7\n",
"b = 5\n",
"c = a * b\n",
"# welchen Wert hat c?\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f07ba1f6-9a39-43e4-813e-c3901b1ae350",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"35"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = 8\n",
"# welchen Wert hat c jetzt?\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ce519d1a-05d9-4307-a2c0-b6dad33d6053",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"56"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# c ist immer noch 35 weil es noch nicht neu berechnet wurde\n",
"c = a * b\n",
"# jetzt sollte es 56 sein\n",
"c"
]
},
{
"cell_type": "markdown",
"id": "e9505ef6-2904-475b-837b-bba119c16b7a",
"metadata": {},
"source": [
" Warum muss `c` eingegeben werden um das Ergebnis zu sehen?\n",
"\n",
"Ein Ergebnis wird nur angezeigt, wenn der Aufruf irgendetwas zurück gibt:\n",
" \n",
"* Berechnung wie 1 + 2 -> Ergebnis\n",
"* Funktionaufruf -> das return-te\n",
"* Zuweisung a = b -> nix\n",
"* Variablenname -> Wert der Variable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65aea755-4369-4985-8dfd-c239a015dd03",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}