learning_python/zero_2_hundred/zero_2_hundred.ipynb

1088 lines
46 KiB
Plaintext
Raw Normal View History

2021-09-25 23:59:32 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "auburn-huntington",
"metadata": {},
"source": [
"# Getting started with Python / Interactive Python with Jupyter Lab\n",
"\n",
"## Installing miniconda\n",
"\n",
"- https://docs.conda.io/en/latest/miniconda.html\n",
"- https://youtu.be/YqUb9XALnDE?t=209"
]
},
{
"cell_type": "markdown",
"id": "labeled-unknown",
"metadata": {},
"source": [
"### Printing and Variable"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "asian-catalog",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World\n"
]
}
],
"source": [
"print(\"Hello World\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "robust-customs",
"metadata": {},
"outputs": [],
"source": [
"# comments are done like this"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "chicken-broadcast",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"number = 10\n",
"string = \"This is a string\"\n",
"\n",
"print(number)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "suburban-august",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is a string\n"
]
}
],
"source": [
"print(string)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "dramatic-brand",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number: 10, and string: This is a string\n"
]
}
],
"source": [
"print(f\"number: {number}, and string: {string}\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "female-spain",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'> and <class 'str'>\n"
]
}
],
"source": [
"print(f\"{type(number)} and {type(string)}\")"
]
},
{
"cell_type": "markdown",
"id": "immediate-worst",
"metadata": {},
"source": [
"### Functions"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "favorite-damages",
"metadata": {},
"outputs": [],
"source": [
"# Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8...\n",
"\n",
"def f(n: int) -> int:\n",
" \"\"\"\n",
" Fibonacci sequence\n",
" ---\n",
" params\n",
" n (int) number requested in sequence\n",
" \n",
" returns\n",
" interger in fibonacci sequence\n",
" \"\"\"\n",
" if n == 0:\n",
" return 0\n",
" if n == 1:\n",
" return 1\n",
" if n == 2:\n",
" return 1\n",
" if n >= 3:\n",
" return f(n-1) + f(n-2)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "reliable-nudist",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Fibonacci sequence\n",
"---\n",
"params\n",
"n (int) number requested in sequence\n",
"\n",
"returns\n",
"interger in fibonacci sequence\n",
"\u001b[0;31mFile:\u001b[0m ~/JupyterlabProjects/Saxon/<ipython-input-20-d0995f361bf4>\n",
"\u001b[0;31mType:\u001b[0m function\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"?f"
]
},
{
"cell_type": "markdown",
"id": "leading-literacy",
"metadata": {},
"source": [
"### More data type - Lists"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "alternate-fault",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range(0, 10)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r = range(10)\n",
"r"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "tutorial-buying",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst = list(r)\n",
"lst"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "south-czech",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst[0]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "administrative-class",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst[1:3]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "grateful-addiction",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst[-1]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "brown-density",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lst[::-1]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "exciting-fundamental",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"4\n",
"0.5\n"
]
}
],
"source": [
"def f1(x: int) -> int:\n",
" return x + 1\n",
"\n",
"def f2(x: int) -> int:\n",
" return x ** 2\n",
"\n",
"def f3(x: int) -> float:\n",
" return 1 / x\n",
"\n",
"funcs = [f1, f2, f3]\n",
"\n",
"for func in funcs:\n",
" print(func(2))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "together-lancaster",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0, 1, 1, 2, 3, 5, 8, 13, 21, 34, "
]
}
],
"source": [
"for i in lst:\n",
" output = f(i)\n",
" print(output, end=\", \")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "governmental-karma",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output = []\n",
"for i in lst:\n",
" output.append(f(i))\n",
"output"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "latter-plumbing",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[f(i) for i in rng]"
]
},
{
"cell_type": "markdown",
"id": "offensive-documentation",
"metadata": {},
"source": [
"### Importing libraries"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "organizational-easter",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"288 ns ± 246 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"302 ns ± 252 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"304 ns ± 204 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"623 ns ± 288 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"957 ns ± 326 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"2.39 µs ± 880 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"2.28 µs ± 424 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"3.56 µs ± 411 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"5.62 µs ± 447 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"9.05 µs ± 598 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"14.4 µs ± 623 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"23.9 µs ± 2.03 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"36.9 µs ± 534 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"61.1 µs ± 3.57 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"96.1 µs ± 506 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"155 µs ± 677 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"252 µs ± 5.16 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"485 µs ± 183 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"807 µs ± 125 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.45 ms ± 245 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"1.84 ms ± 152 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"3.27 ms ± 529 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"4.65 ms ± 601 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"7.25 ms ± 602 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"12 ms ± 1.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"18.7 ms ± 1.36 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"30.7 ms ± 3.21 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"63.5 ms ± 22.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"115 ms ± 38.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"130 ms ± 9.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"251 ms ± 50.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"392 ms ± 73.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"629 ms ± 104 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"times = []\n",
"for i in range(33):\n",
" t = %timeit -n1 -o f(i)\n",
" times.append(t.best)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "demanding-circulation",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>f</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.580001e-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.819999e-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.979997e-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.560002e-07</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>7.710000e-07</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" f\n",
"0 1.580001e-07\n",
"1 1.819999e-07\n",
"2 1.979997e-07\n",
"3 4.560002e-07\n",
"4 7.710000e-07"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df = pd.DataFrame(data=times, columns=[\"f\"])\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "complete-warren",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEWCAYAAABrDZDcAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAfg0lEQVR4nO3de3xcdZ3/8dcnk6RpLr2nkDZtU0oLbbkptbi4K/wA/YFKYRUVVlf5rS6i8lB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LV2sWbRLMzCmWwuRUEgIhKCobiz68Uu1lSF21EMCgIRkVA8f+AIvQPx0PsHQEEgIhKKutYuANYoCERE8lN9axfFsQJOX1gedikKAhGRMNTv62LVqeUUxcL/Gh63AjOrNrN/NLOfmNl2M/utmX3DzN5oZmO+3swuN7PdZtZgZreM8PyNZvaUme0ws0fMbM1kfhgRkWzg7tS1drE2Ah3FME4QmNm3gbuAfuBfgOuADwC/Ai4HHjGz147y2hhwG3AFsAa4boQv+nvd/Wx3Pw/4AvClif8oIiLZYX9XH53d/ZHoHwAoHOf5f3P3p0dY/zTwIzMrBpaO8tr1QIO77wEws03AVUB9agN370rbvgzwTAsXEclWUTmjOGXMIBgpBMxsLrDE3Z90936gYZSXLwaa05ZbgAtG2N8HgZuBYuCSkXZkZjcANwAsXTpa7oiIZIf61i7M4MyQ5xhKyaiXwsx+bWazzGwe8ATwbTMbrxlnpFPlTviL391vc/cVwEeBfxppR+5+h7uvc/d1lZWVmZQsIhJZda1d1Mwvo3zGeI0y0yPT7urZyWacNwPfdvfzgcvGeU0LsCRtuRpoHWP7TcDVGdYjIpK
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"g = df.plot(title=\"F time\")\n",
"g.set_xlabel(\"n\")\n",
"g.set_ylabel(\"time (s)\")\n",
"plt.savefig(\"f.png\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "empirical-tolerance",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"f 3.693825\n",
"dtype: float64"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sum()"
]
},
{
"cell_type": "markdown",
"id": "dress-series",
"metadata": {},
"source": [
"### More data types - Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "fifth-stereo",
"metadata": {},
"outputs": [],
"source": [
"dct = {\n",
" \"key1\": \"value1\",\n",
" \"key2\": [\"value2\", \"value3\"],\n",
" \"random name for a key\": 1,\n",
" \"key4\": (2000, 3000)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "periodic-estate",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2000, 3000)"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dct[\"key4\"]"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "buried-plant",
"metadata": {},
"outputs": [],
"source": [
"#dct['key5'] will error"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "precise-possession",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dct.get(\"key5\") is None"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "perfect-commander",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key: key1, value: value1\n",
"key: key2, value: ['value2', 'value3']\n",
"key: random name for a key, value: 1\n",
"key: key4, value: (2000, 3000)\n"
]
}
],
"source": [
"for k,v in dct.items():\n",
" print(f\"key: {k}, value: {v}\")"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "prompt-maldives",
"metadata": {},
"outputs": [],
"source": [
"cache = {}\n",
"\n",
"def f_better(n: int) -> int:\n",
" \"\"\"\n",
" Fibonacci sequence - !!with caching!!\n",
" ---\n",
" params\n",
" n (int) number requested in sequence\n",
" \n",
" returns\n",
" interger in fibonacci sequence\n",
" \"\"\"\n",
" if all([cache.get(n-1), cache.get(n-2)]):\n",
" return cache[n-1] + cache[n-2] \n",
" elif n == 0:\n",
" return 0\n",
" elif n == 1 or n == 2:\n",
" return 1\n",
" else:\n",
" cache[n] = f_better(n-1) + f_better(n-2) \n",
" return cache[n]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "north-logistics",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>f</th>\n",
" <th>f_better</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.580001e-07</td>\n",
" <td>0.000001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.819999e-07</td>\n",
" <td>0.000001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1.979997e-07</td>\n",
" <td>0.000001</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.560002e-07</td>\n",
" <td>0.000003</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>7.710000e-07</td>\n",
" <td>0.000006</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" f f_better\n",
"0 1.580001e-07 0.000001\n",
"1 1.819999e-07 0.000001\n",
"2 1.979997e-07 0.000001\n",
"3 4.560002e-07 0.000003\n",
"4 7.710000e-07 0.000006"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"times = []\n",
"for i in range(35):\n",
" t = %timeit -n1 -o -q f_better(i)\n",
" times.append(t.best)\n",
" \n",
"df['f_better'] = pd.DataFrame(data=times)\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "honest-rings",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEWCAYAAABrDZDcAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELA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gYhICokNNjeWoykrCEREUkjtGJ86CgoCEZGU0dfn1Da3j+kZQxBwEJjZxWa23cx2mtnqQdb7qJm5mSW8n6aISDZoPNRJZ08fc8dosLmYwILAzHKBO4EVwCLgcjNblGC9IuCLwJ+DqkVEJB3Ezhgay+ElINgjgmXATnff5e5dwFpgVYL1/gH4LtARYC0iIimvtnls70MQE2QQzAbq4qbro/OOMLOzgDnu/rvBNmRmV5tZhZlV6C5kIpKpapvbyc0xZk8bm+GnY4IMgkTnPh25qbCZ5QDfB/5mqA25+xp3X+ruS4uLi0exRBGR1FHT1E7p1ALycsf2PJ4g91YPzImbLgMa4qaLgNOADWa2G3gHsE4dxiKSrcI4dRSCDYJNwAIzm2dm+cBlwLrYQnc/6O4z3b3c3cuB54CV7l4RYE0iIikrEgRje8YQBBgE7t4DXA88BlQDD7p7pZndamYrg9qviEg6OtTRTXNb15hfQwAQ6H3Q3H09sL7fvG8MsO55QdYiIpLKapqiN6zPsKYhERFJUt2RG9YrCEREslJNNAjCaBpSEIiIpICapnamT8qnqCBvzPetIBARSQF1ze2hNAuBgkBEJCXUNLeF0lEMCgIRkdB19/bRcKAjlP4BUBCIiITutTcO09v
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"g = df.plot(title=\"F time\")\n",
"g.set_xlabel(\"n\")\n",
"g.set_ylabel(\"time (s)\")\n",
"plt.savefig(\"f_better.png\")"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "egyptian-seminar",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{3: 2,\n",
" 4: 3,\n",
" 6: 8,\n",
" 7: 13,\n",
" 9: 34,\n",
" 10: 55,\n",
" 12: 144,\n",
" 13: 233,\n",
" 15: 610,\n",
" 16: 987,\n",
" 18: 2584,\n",
" 19: 4181,\n",
" 21: 10946,\n",
" 22: 17711,\n",
" 24: 46368,\n",
" 25: 75025,\n",
" 27: 196418,\n",
" 28: 317811,\n",
" 30: 832040,\n",
" 31: 1346269,\n",
" 33: 3524578,\n",
" 34: 5702887}"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cache"
]
},
{
"cell_type": "markdown",
"id": "loving-jungle",
"metadata": {},
"source": [
"### Classes"
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "parallel-sharp",
"metadata": {},
"outputs": [],
"source": [
"class Car:\n",
" \"\"\"\n",
" Class for cars that people drive\n",
" ---\n",
" params \n",
" make (str): make of the car\n",
" model (str): model of the car\n",
" year (int): year car was made\n",
" \"\"\"\n",
" def __init__(self, make: str, model: str, year: int=2021):\n",
" self.make = make\n",
" self.model = model\n",
" self.year = year\n",
" self.__is_driving = False\n",
" \n",
" def __repr__(self):\n",
" return f'{self.make} - {self.model}'\n",
" \n",
" def drive(self) -> str:\n",
" if not self.__is_driving:\n",
" print(f'{self.make} - {self.model} is now driving')\n",
" self.__is_driving = True\n",
" else:\n",
" print(f'{self.make} - {self.model} is ALREADY driving!')\n",
" \n",
" def stop(self) -> str:\n",
" if self.__is_driving:\n",
" print(f'{self.make} - {self.model} has stopped')\n",
" self.__is_driving = False\n",
" else:\n",
" print(f'{self.make} - {self.model} is ALREADY stopped!')"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "protective-lying",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mInit signature:\u001b[0m \u001b[0mCar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmake\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0myear\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mint\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m2021\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m \n",
"Class for cars that people drive\n",
"---\n",
"params \n",
" make (str): make of the car\n",
" model (str): model of the car\n",
" year (int): year car was made\n",
"\u001b[0;31mType:\u001b[0m type\n",
"\u001b[0;31mSubclasses:\u001b[0m \n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"?Car"
]
},
{
"cell_type": "code",
"execution_count": 135,
"id": "dimensional-shanghai",
"metadata": {},
"outputs": [],
"source": [
"shivan = Car('Toyota', 'Corolla', '2008')\n",
"bruno = Car('Tesla', 'Model3')"
]
},
{
"cell_type": "code",
"execution_count": 136,
"id": "raised-brook",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Tesla - Model3"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bruno"
]
},
{
"cell_type": "code",
"execution_count": 137,
"id": "earlier-diploma",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Toyota - Corolla is now driving\n"
]
}
],
"source": [
"shivan.drive()"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "varied-setting",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Toyota - Corolla is ALREADY driving!\n"
]
}
],
"source": [
"shivan.drive()"
]
},
{
"cell_type": "code",
"execution_count": 139,
"id": "recognized-forwarding",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tesla - Model3 is ALREADY stopped!\n"
]
}
],
"source": [
"bruno.stop()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "executed-cleanup",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}