Skip to content

Instantly share code, notes, and snippets.

@jbarnoud
Created April 23, 2018 12:04
Show Gist options
  • Select an option

  • Save jbarnoud/febaae9cf28b4b17b07139f6d5394d45 to your computer and use it in GitHub Desktop.

Select an option

Save jbarnoud/febaae9cf28b4b17b07139f6d5394d45 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T06:47:02.999457Z",
"start_time": "2018-04-23T06:47:02.933112Z"
},
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"import importlib\n",
"import turtle"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Functions\n",
"\n",
"Jonathan Barnoud `<[email protected]>`"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Variables"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
">>> size = 4000\n",
">>> bases = \"ATGC\"\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
">>> size = 4000\n",
">>> size = 30\n",
">>> other = size\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
">>> bases = \"ATGC\"\n",
">>> print(bases[1])\n",
"T\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
">>> my_list = [4, 'apple', 2.5]\n",
">>> print(my_list[1])\n",
"apple\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
">>> print(my_list[1][3])\n",
"l\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Flow control"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"* `for`\n",
"* `while`\n",
"* `if/elif/else`\n",
"* `break/continue`"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Functions?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"$$\n",
"f(x) = 3x + 4\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"* len\n",
"* max\n",
"* min\n",
"* sum\n",
"* print\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
"def <name>(<arguments>):\n",
" \"\"\"\n",
" <Documentation>\n",
" \"\"\"\n",
" <content>\n",
" <content>\n",
" <content>\n",
" return <output>\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:23:05.226151Z",
"start_time": "2018-04-23T07:23:05.210303Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"40"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def calculate_f(x):\n",
" \"\"\"\n",
" Do something useless with x\n",
" \"\"\"\n",
" result = 3 * x + 4\n",
" return result\n",
"\n",
"calculate_f(12)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Don't Repeat Yourself"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:25:06.429593Z",
"start_time": "2018-04-23T07:24:57.742010Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"turtle = importlib.reload(turtle)\n",
"\n",
"# Setup the turtle\n",
"window = turtle.Screen()\n",
"peter = turtle.Turtle()\n",
"\n",
"# Draw a square\n",
"for _ in range(4):\n",
" peter.forward(90)\n",
" peter.left(90)\n",
"# Move away\n",
"peter.penup()\n",
"peter.forward(300)\n",
"peter.pendown()\n",
"# Draw a square\n",
"for _ in range(4):\n",
" peter.forward(90)\n",
" peter.left(90)\n",
"\n",
"window.mainloop()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:27:08.651421Z",
"start_time": "2018-04-23T07:27:02.343766Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"turtle = importlib.reload(turtle)\n",
"\n",
"def draw_square(animal):\n",
" \"\"\"\n",
" Squares are pointy\n",
" \"\"\"\n",
" for _ in range(4):\n",
" animal.forward(90)\n",
" animal.left(90)\n",
"\n",
"# Setup the turtle\n",
"window = turtle.Screen()\n",
"peter = turtle.Turtle()\n",
"\n",
"draw_square(peter)\n",
"\n",
"# Move away\n",
"peter.penup()\n",
"peter.forward(300)\n",
"peter.pendown()\n",
"\n",
"draw_square(peter)\n",
"\n",
"window.mainloop()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:28:05.140044Z",
"start_time": "2018-04-23T07:27:55.958747Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"turtle = importlib.reload(turtle)\n",
"\n",
"def draw_square(animal, size):\n",
" for _ in range(4):\n",
" animal.forward(size)\n",
" animal.left(90)\n",
"\n",
"# Setup the turtle\n",
"window = turtle.Screen()\n",
"peter = turtle.Turtle()\n",
"\n",
"draw_square(peter, 200)\n",
"\n",
"# Move away\n",
"peter.penup()\n",
"peter.forward(300)\n",
"peter.pendown()\n",
"\n",
"draw_square(peter, 400)\n",
"\n",
"window.mainloop()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:28:56.303240Z",
"start_time": "2018-04-23T07:28:47.496804Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"turtle = importlib.reload(turtle)\n",
"\n",
"def draw_square(animal, size):\n",
" for _ in range(4):\n",
" animal.forward(size)\n",
" animal.left(90)\n",
"\n",
"# Setup the turtle\n",
"window = turtle.Screen()\n",
"peter = turtle.Turtle()\n",
"bart = turtle.Turtle()\n",
"peter.pencolor('hotpink')\n",
"bart.pencolor('green')\n",
"\n",
"draw_square(peter, 200)\n",
"draw_square(bart, 400)\n",
"\n",
"window.mainloop()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### Structure your code"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:29:57.290958Z",
"start_time": "2018-04-23T07:29:57.258939Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n"
]
}
],
"source": [
"long_months = [1, 3, 5, 7, 8, 10, 12]\n",
"year = 2018\n",
"day = 1\n",
"month = 1\n",
"\n",
"while month <= 12 and day <= 31:\n",
" print(day, end=' ')\n",
" day += 1\n",
" if ((day > 31) or (month not in long_months and day > 30)\n",
" or (month == 2\n",
" and (day > 29\n",
" or (not (year % 4 == 0\n",
" and (year % 100 != 0 or year % 400 == 0))\n",
" and day > 28)))):\n",
" day = 1\n",
" month += 1\n",
" print('')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:29:59.510510Z",
"start_time": "2018-04-23T07:29:59.502357Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def is_leap_year(year):\n",
" return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)\n",
"\n",
"\n",
"def is_end_of_month(year, month, day):\n",
" long_months = [1, 3, 5, 7, 8, 10, 12]\n",
" if day > 31 or (month not in long_months and day > 30):\n",
" return True\n",
" if month == 2 and (day > 29 or (not is_leap_year(year) and day > 28)):\n",
" return True\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:30:56.183163Z",
"start_time": "2018-04-23T07:30:56.161814Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 \n",
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 \n"
]
}
],
"source": [
"year = 2016\n",
"day = 1\n",
"month = 1\n",
"\n",
"while month <= 12 and day <= 31:\n",
" print(day, end=' ')\n",
" day += 1\n",
" if is_end_of_month(year, month, day):\n",
" day = 1\n",
" month += 1\n",
" print('')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
"def <name>(<arguments>):\n",
" \"\"\"\n",
" <Documentation>\n",
" \"\"\"\n",
" <content>\n",
" <content>\n",
" <content>\n",
" return <output>\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"The name **MUST**:\n",
"\n",
"* only contain letters, numbers, and `_`\n",
"* start with a letter or `_`\n",
"\n",
"The name **SHOULD**:\n",
"\n",
"* use lowercase letters\n",
"* use `_` to separate words\n",
"\n",
"A piece of advice:\n",
"\n",
"* use **explicit** names\n",
"* use **verbs** to name functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```\n",
"def <name>(<arguments>):\n",
" <content>\n",
" <content>\n",
" <content>\n",
" return <output>\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-21T14:46:08.910304Z",
"start_time": "2018-04-21T14:46:08.891883Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Return\n",
"\n",
"* gives the result out\n",
"* exists the function\n",
"* by default a function returns `None` at the end"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
"def <name>(<arguments>):\n",
" \"\"\"\n",
" <Documentation>\n",
" \"\"\"\n",
" <content>\n",
" <content>\n",
" <content>\n",
" return <output>\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Arguments\n",
"\n",
"* Variables going in\n",
"* Any number, but keep it to the minimum\n",
"\n",
"```python\n",
"def is_end_of_month(year, month, day):\n",
"```\n",
"\n",
"* Optional arguments\n",
"\n",
"```python\n",
"def draw_square(animal, size=100):\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:37:42.034866Z",
"start_time": "2018-04-23T07:37:42.021743Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"def my_function(a, b):\n",
" c = a + b\n",
" return c\n",
"\n",
"e = 2\n",
"f = 3\n",
"g = my_function(e, f)\n",
"print(g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-22T15:38:12.521730Z",
"start_time": "2018-04-22T15:38:12.494618Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def my_function(a, b):\n",
" c = a + b\n",
" return c\n",
"\n",
"b = 6\n",
"c = 2\n",
"a = my_function(b, c)\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T07:55:36.573465Z",
"start_time": "2018-04-23T07:55:36.565091Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def my_function(a, b):\n",
" for i in b:\n",
" a[i] = a[i] * 2\n",
"\n",
"e = [3, 4, 5, 1, 3]\n",
"f = [2, 0]\n",
"my_function(e, f)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T12:01:38.909471Z",
"start_time": "2018-04-23T12:01:38.906075Z"
}
},
"outputs": [],
"source": [
"#print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"```python\n",
"def <name>(<arguments>):\n",
" \"\"\"\n",
" <Documentation>\n",
" \"\"\"\n",
" <content>\n",
" <content>\n",
" <content>\n",
" return <output>\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T12:01:46.194203Z",
"start_time": "2018-04-23T12:01:46.190159Z"
},
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def calculate_time_to_seconds(hours, minutes, seconds):\n",
" \"\"\"\n",
" Convert a time in hours, minutes and seconds to seconds.\n",
" \n",
" Parameters\n",
" ----------\n",
" hours: int\n",
" The number of hours.\n",
" minutes: int\n",
" The number of minutes.\n",
" seconds: int\n",
" The number of seconds.\n",
" \n",
" Returns\n",
" -------\n",
" int\n",
" The time converted to seconds.\n",
" \"\"\"\n",
" return hours * 3600 + minutes * 60 + seconds"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2018-04-23T12:01:46.871453Z",
"start_time": "2018-04-23T12:01:46.864215Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function calculate_time_to_seconds in module __main__:\n",
"\n",
"calculate_time_to_seconds(hours, minutes, seconds)\n",
" Convert a time in hours, minutes and seconds to seconds.\n",
" \n",
" Parameters\n",
" ----------\n",
" hours: int\n",
" The number of hours.\n",
" minutes: int\n",
" The number of minutes.\n",
" seconds: int\n",
" The number of seconds.\n",
" \n",
" Returns\n",
" -------\n",
" int\n",
" The time converted to seconds.\n",
"\n"
]
}
],
"source": [
"help(calculate_time_to_seconds)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"![Document all the things!](https://i.imgflip.com/28vfsw.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## What you must remember\n",
"\n",
"* Functions avoid repetitions\n",
"* Functions structure code\n",
"* Functions improve readability\n",
"* Use docstrings to document functions\n",
"* Be careful about your variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"hide_input": false,
"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.6.4"
},
"toc": {
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment