Skip to content

Commit 31eeac7

Browse files
committed
Added example for python notebook
1 parent 4c19aaf commit 31eeac7

File tree

3 files changed

+147
-10
lines changed

3 files changed

+147
-10
lines changed

Lecture-2-Introduction-to-Python-Programming.ipynb

Lines changed: 140 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@
6060
},
6161
{
6262
"cell_type": "code",
63-
"execution_count": 1,
63+
"execution_count": 14,
6464
"metadata": {},
6565
"outputs": [
6666
{
6767
"name": "stdout",
6868
"output_type": "stream",
6969
"text": [
70-
"zsh:1: no matches found: scripts/hello-world*.py\r\n"
70+
"scripts/hello-world-in-swedish.py scripts/hello-world.py\r\n"
7171
]
7272
}
7373
],
@@ -77,14 +77,16 @@
7777
},
7878
{
7979
"cell_type": "code",
80-
"execution_count": 2,
80+
"execution_count": 15,
8181
"metadata": {},
8282
"outputs": [
8383
{
8484
"name": "stdout",
8585
"output_type": "stream",
8686
"text": [
87-
"cat: scripts/hello-world.py: No such file or directory\r\n"
87+
"#!/usr/bin/env python\r\n",
88+
"\r\n",
89+
"print(\"Hello world!\")\r\n"
8890
]
8991
}
9092
],
@@ -94,14 +96,14 @@
9496
},
9597
{
9698
"cell_type": "code",
97-
"execution_count": 3,
99+
"execution_count": 16,
98100
"metadata": {},
99101
"outputs": [
100102
{
101103
"name": "stdout",
102104
"output_type": "stream",
103105
"text": [
104-
"python: can't open file 'scripts/hello-world.py': [Errno 2] No such file or directory\r\n"
106+
"Hello world!\r\n"
105107
]
106108
}
107109
],
@@ -129,14 +131,17 @@
129131
},
130132
{
131133
"cell_type": "code",
132-
"execution_count": 4,
134+
"execution_count": 17,
133135
"metadata": {},
134136
"outputs": [
135137
{
136138
"name": "stdout",
137139
"output_type": "stream",
138140
"text": [
139-
"cat: scripts/hello-world-in-swedish.py: No such file or directory\r\n"
141+
"#!/usr/bin/env python\r\n",
142+
"# -*- coding: UTF-8 -*-\r\n",
143+
"\r\n",
144+
"print(\"Hej världen!\")\r\n"
140145
]
141146
}
142147
],
@@ -146,14 +151,14 @@
146151
},
147152
{
148153
"cell_type": "code",
149-
"execution_count": 5,
154+
"execution_count": 18,
150155
"metadata": {},
151156
"outputs": [
152157
{
153158
"name": "stdout",
154159
"output_type": "stream",
155160
"text": [
156-
"python: can't open file 'scripts/hello-world-in-swedish.py': [Errno 2] No such file or directory\r\n"
161+
"Hej världen!\r\n"
157162
]
158163
}
159164
],
@@ -3573,6 +3578,131 @@
35733578
"* http://www.greenteapress.com/thinkpython/ - A free book on Python programming.\n",
35743579
"* [Python Essential Reference](http://www.amazon.com/Python-Essential-Reference-4th-Edition/dp/0672329786) - A good reference book on Python programming."
35753580
]
3581+
},
3582+
{
3583+
"cell_type": "markdown",
3584+
"metadata": {},
3585+
"source": [
3586+
"## Exercise 1"
3587+
]
3588+
},
3589+
{
3590+
"cell_type": "markdown",
3591+
"metadata": {},
3592+
"source": [
3593+
"<div class=\"alert alert-success\">\n",
3594+
"Implement the function <code>print_routine</code> that iterates over the `days_of_the_week` list and prints the routine in following format:\n",
3595+
"<pre>\n",
3596+
"Today is Sunday\n",
3597+
" Sleep in\n",
3598+
"Today is Monday\n",
3599+
" Go to work\n",
3600+
"Today is Tuesday\n",
3601+
" Go to work\n",
3602+
"Today is Wednesday\n",
3603+
" Go to work\n",
3604+
"Today is Thursday\n",
3605+
" Go to work\n",
3606+
"Today is Friday\n",
3607+
" Go to work\n",
3608+
"Today is Saturday\n",
3609+
" Do chores\n",
3610+
"</pre>\n",
3611+
"</div>"
3612+
]
3613+
},
3614+
{
3615+
"cell_type": "code",
3616+
"execution_count": null,
3617+
"metadata": {},
3618+
"outputs": [],
3619+
"source": [
3620+
"days_of_the_week = [\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\"]\n",
3621+
"\n",
3622+
"def print_routine():\n",
3623+
" \"\"\"Print\n",
3624+
" \n",
3625+
" \"\"\"\n",
3626+
" ### BEGIN SOLUTION\n",
3627+
" raise NotImplementedError()\n",
3628+
" ### END SOLUTION"
3629+
]
3630+
},
3631+
{
3632+
"cell_type": "code",
3633+
"execution_count": null,
3634+
"metadata": {},
3635+
"outputs": [],
3636+
"source": [
3637+
"print_routine()"
3638+
]
3639+
},
3640+
{
3641+
"cell_type": "markdown",
3642+
"metadata": {},
3643+
"source": [
3644+
"---\n",
3645+
"## Excercise: Fibonacci Sequence\n",
3646+
"\n",
3647+
"The Fibonacci sequence is a sequence in math that starts with 0 and 1, and then each successive entry is the sum of the previous two. Thus, the sequence goes *0,1,1,2,3,5,8,13,21,34,55,89,...*\n",
3648+
"\n",
3649+
"A very common exercise in programming books is to compute the Fibonacci sequence up to some number *n*."
3650+
]
3651+
},
3652+
{
3653+
"cell_type": "markdown",
3654+
"metadata": {},
3655+
"source": [
3656+
"<div class=\"alert alert-success\">\n",
3657+
"Implement the function <code>fibonacci</code> Return the Fibonacci sequence of length *sequence_length*\n",
3658+
"</div>"
3659+
]
3660+
},
3661+
{
3662+
"cell_type": "code",
3663+
"execution_count": 11,
3664+
"metadata": {},
3665+
"outputs": [],
3666+
"source": [
3667+
"def fibonacci(sequence_length):\n",
3668+
" \"\"\"Returns the fibonacci sequence for sequence_length\n",
3669+
" \n",
3670+
" * If the sequence_length is invalid (<1) raise a ValueError\n",
3671+
" \n",
3672+
" Parameters\n",
3673+
" ----------\n",
3674+
" sequence_length : number\n",
3675+
" The length of the fibonacci sequence\n",
3676+
" \n",
3677+
" Returns\n",
3678+
" -------\n",
3679+
" fibonacci sequence: list\n",
3680+
" \n",
3681+
" \"\"\"\n",
3682+
" ### BEGIN SOLUTION\n",
3683+
" raise NotImplementedError()\n",
3684+
" ### END SOLUTION(name):"
3685+
]
3686+
},
3687+
{
3688+
"cell_type": "code",
3689+
"execution_count": null,
3690+
"metadata": {},
3691+
"outputs": [],
3692+
"source": [
3693+
"from nose.tools import assert_raises\n",
3694+
"assert_raises(ValueError, fibonacci, 0)\n",
3695+
"assert fibonacci(2) == [0,1]\n",
3696+
"assert fibonacci(12) == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
3697+
"print(\"success\")"
3698+
]
3699+
},
3700+
{
3701+
"cell_type": "code",
3702+
"execution_count": null,
3703+
"metadata": {},
3704+
"outputs": [],
3705+
"source": []
35763706
}
35773707
],
35783708
"metadata": {

scripts/hello-world-in-swedish.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env python
2+
# -*- coding: UTF-8 -*-
3+
4+
print("Hej världen!")

scripts/hello-world.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
3+
print("Hello world!")

0 commit comments

Comments
 (0)