pr3/python/kontrollstrukturen/kontrollstrukturen.ipynb

191 lines
3.4 KiB
Plaintext
Raw Normal View History

2023-06-23 23:05:46 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n"
]
}
],
"source": [
"# Anzahl Lösungen quadratischer Gleichung\n",
"\n",
"def amount_solutions(a: int, b: int, c: int):\n",
" discriminant = b**2 - 4 * a * c\n",
"\n",
" if discriminant < 0:\n",
" return 0\n",
" elif discriminant == 0:\n",
" return 1\n",
" else:\n",
" return 2\n",
"\n",
"print(amount_solutions(5, 6, 1))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"a = 5\n",
"b = 4\n",
"\n",
"max = 0\n",
"\n",
"# normales if\n",
"\n",
"if a > b:\n",
" max = a\n",
"else:\n",
" max = b\n",
"\n",
"#ternäres if\n",
"\n",
"max = a if a > b else b\n",
"\n",
"print(max)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"4\n",
"9\n",
"16\n"
]
}
],
"source": [
"# Ausgabe der ersten 4 Quadratzahlen\n",
"\n",
"i = 1\n",
"\n",
"while i < 5:\n",
" print(i ** 2)\n",
" i = i + 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# Summe von 1-5 mit while-loop\n",
"\n",
"value = 0\n",
"i = 0\n",
"while i <= 5:\n",
" value += i\n",
" i += 1\n",
"\n",
"print(value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n"
]
}
],
"source": [
"# Summe von 1-5 mit while-loop\n",
"\n",
"value = 0\n",
"for i in range(0, 5+1):\n",
" value += i\n",
"\n",
"print(value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Index Value Bar\n",
" 0 19 *******************\n",
" 1 3 ***\n",
" 2 15 ***************\n",
" 3 7 *******\n",
" 4 11 ***********\n"
]
}
],
"source": [
"# Formatierung Werte in Tabelle\n",
"\n",
"values = [19, 3, 15, 7, 11]\n",
"\n",
"print(f\"{'Index':>6} {'Value':>6} {'Bar'}\")\n",
"for index, item in enumerate(values):\n",
" print(f\"{index:>6} {item:>6} {'*' * item}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}