Skip to content

Commit

Permalink
added modules explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
davydenk committed Jan 31, 2023
1 parent ec32995 commit d6b884a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
22 changes: 21 additions & 1 deletion Exercises7-classes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,30 @@
"```"
]
},
{
"cell_type": "markdown",
"id": "349a1e37-b814-43bb-adfc-5187b98e8797",
"metadata": {},
"source": [
"### **Exericise5:**\n",
"\n",
"Create a module that has all the recaman functions implementations from the solutions of the day 1 exercises. Import and use it to call them all."
]
},
{
"cell_type": "markdown",
"id": "d16ba928-fdc0-4869-9bbd-ff2c9bf8423e",
"metadata": {},
"source": [
"### **Exercise6**:\n",
"\n",
"Create a module with the class of the Exercise3."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9309b44-139d-41e2-a262-9a645e1d9343",
"id": "6b1a76fb-cd23-44bb-aa3d-ab489cae1262",
"metadata": {},
"outputs": [],
"source": []
Expand Down
80 changes: 79 additions & 1 deletion Lecture7-classes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,88 @@
"print(var1+var2) "
]
},
{
"cell_type": "markdown",
"id": "b664f70e-e625-44d9-9c7d-78d558849a86",
"metadata": {},
"source": [
"#### **Modules**"
]
},
{
"cell_type": "markdown",
"id": "a56e8486-14e2-417e-9ab2-b878440c2d44",
"metadata": {},
"source": [
"If your program becomes too long or if you want to reuse the functions/classes you've writen, you might want to create own modules. You just need to create a file with those functions/classes, then `import` it the way you do with numpy."
]
},
{
"cell_type": "markdown",
"id": "9b19871e-a57c-4485-ba7f-72054aa50472",
"metadata": {},
"source": [
"The `file` cell magic command lets you create a text file with the contents of the cell (of course you can just create that file in any text editor):"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "59070244-ba5c-4f76-801f-788fe3c1125f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing mymodule.py\n"
]
}
],
"source": [
"%%file \"mymodule.py\"\n",
"\n",
"def add(a,b):\n",
" return a+b"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "44fdb8fe-3b63-4ccf-a2a4-a684d468e9f3",
"metadata": {},
"outputs": [],
"source": [
"import mymodule as mm"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "67e12779-79c7-4f62-b141-4fed44e76fd1",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mm.add(2,2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5c4cb6d-32ed-4efb-9274-153d0b8682fb",
"id": "3751cbaf-43d6-4ec7-b93e-f5b9bbdd9672",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit d6b884a

Please sign in to comment.