Skip to content

Commit

Permalink
more matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLeoni committed Oct 12, 2018
1 parent 5906c06 commit 8946414
Show file tree
Hide file tree
Showing 7 changed files with 2,151 additions and 662 deletions.
161 changes: 157 additions & 4 deletions commandments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@
"\n",
"<div class=\"alert alert-info\" >\n",
"\n",
"**COMMANDMENT 6: You shall never ever assign values to method calls**\n",
"**COMMANDMENT 6: You shall never ever assign values to function nor method calls**\n",
"</div>\n",
"\n",
"_WRONG WRONG STUFF:_\n",
"_WRONG WRONG:_\n",
"\n",
"```python\n",
"\n",
Expand All @@ -162,7 +162,7 @@
"\n",
"```\n",
"\n",
"_CORRECT STUFF:_ \n",
"_CORRECT:_ \n",
"\n",
"With the assignment operator we want to store in the left side a value from the right side, so all of these are valid operations:\n",
"\n",
Expand All @@ -171,7 +171,7 @@
"y = my_fun()\n",
"z = []\n",
"z[0] = 7\n",
"d = {}\n",
"d = dict()\n",
"d[\"a\"] = 6\n",
"```\n",
"\n",
Expand All @@ -189,6 +189,159 @@
"\n",
"If there is no `return` in function description, the function is intended to return `None`. In this case you don't even need to write `return None`, as Python will do it implicitly for you."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\" >\n",
"\n",
"**COMMANDMENT 8: You shall never ever redefine system functions**\n",
"</div>\n",
"\n",
"Python has system defined function, for example `list` is a Python type. As such, you can use it for example as a function to convert some type to a list:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'list' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-c63add832213>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"ciao\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'list' object is not callable"
]
}
],
"source": [
"list(\"ciao\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"when you allow the forces of evil to take the best of you, you might be tempted to use reserved words like `list` as a variable for you own miserable purposes: "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"list = ['my', 'pitiful', 'list']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python allows you to do so, but we do **not**, for the consequences are disastrous.\n",
"\n",
"For example, if you now attempt to use `list` for its intended purpose like casting to list, it won't work anymore:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python\n",
"list(\"ciao\")\n",
"```\n",
"```\n",
"---------------------------------------------------------------------------\n",
"TypeError Traceback (most recent call last)\n",
"<ipython-input-4-c63add832213> in <module>()\n",
"----> 1 list(\"ciao\")\n",
"\n",
"TypeError: 'list' object is not callable\n",
"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-info\" >\n",
"\n",
"**COMMANDMENT 9: Whenever you introduce a variable with a for cycle, such variable must be new**\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you read carefully Commandment 4 you should not need to be reminded of this Commandment, nevertheless it is always worth restating the Right Way.\n",
"\n",
"If you defined a variable before, you shall not reintroduce it in a `for`, since it is as confusing as reassigning function parameters.\n",
"\n",
"So avoid this sins:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"i = 7\n",
"for i in range(3): # sin, you lose i variable\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"def f(i):\n",
" for i in range(3): # sin again, you lose i parameter\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for i in range(2): \n",
" for i in range(3): # debugging hell, you lose i from outer for\n",
" print(i)"
]
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,6 @@
" \"\"\" Return D\"\"\"\n",
" raise Exception(\"TODO IMPLEMENT ME !\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 8946414

Please sign in to comment.