Jupyter and Python example

Download exercises zip

Browse files online

Most complex example of a notebook with exercises both in Jupyter and Python files, and ‘advanced’ features

What to do

  • unzip exercises in a folder, you should get something like this:

jup-and-py-example
   jup-and-py-example.ipynb
   jup-and-py-example_sol.ipynb
   lab.py
   lab_test.py
   lab_sol.py
  • open the editor of your choice (for example Visual Studio Code, Spyder or PyCharme), and edit lab.py file

  • Go on reading this notebook, and follow instuctions inside.

Let’s begin

You are going to program a simulator of bouncing clowns. To do so, we are going to load this module:

[2]:
import local
[3]:
local.gimme(5)
It was a 5 indeed

Download test data

Local file:

Global image

cc-by-5754

Local exercise image

leaves-98231

Python tutor

[4]:
x = 5
y = 6
z = x + y

jupman.pytut()
[4]:
Python Tutor visualization

Exercise in Jupyter

Implement this function:

Show solution
[5]:
def hello(s):
    raise Exception('TODO IMPLEMENT ME !')

hello_db = hello("Guybrush")

hello_db[:10]
[5]:
['hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush']

Full expected output is in file expected_output_db.py, if you can’t manage to solve the exercise, as a last resort you can type: from expected_hello_db import * (DO NOT copy-paste file content, it would probably mess Jupyter up)

[6]:
from expected_hello_db import *
expected_hello_db[:10]
[6]:
['hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush',
 'hello',
 'Guybrush']

Other example:

[7]:
hello_db2 = hello("Threepwood")

hello_db2[:10]
[7]:
['hello',
 'Threepwood',
 'hello',
 'Threepwood',
 'hello',
 'Threepwood',
 'hello',
 'Threepwood',
 'hello',
 'Threepwood']

Exercise using previous output

Write some code which says hello 3 times using previous functionand

[8]:
print(hello('Guybrush')[:6])
['hello', 'Guybrush', 'hello', 'Guybrush', 'hello', 'Guybrush']

Question in Jupyter

QUESTION: Why learn coding?

Show answer

Exercise in Python

Start editing lab.py in text editor

[9]:
from lab_sol import *

add

Implement add function:

[10]:
add(3,5)
[10]:
8

sub

Implement sub function

[11]:
sub(7,4)
[11]:
3

Fine grained purging

This cell input will be completely removed
[13]:


print("This cell output will be completely removed")
[ ]: