Lecture 7

Notebooks; variadic functions

MCS 275 Spring 2021
Emily Dumas

Lecture 7: Notebooks; variadic functions

Course bulletins:
  • Project 1 posted. Deadline 6pm CST on Fri Feb 5.
  • Project 1 autograder opens on Monday.
  • Work on Worksheet 3.

Python interfaces

  • REPL -- Run one command at a time. Result is printed. Code is lost when you exit.
  • Script mode -- Run all commands in a file. Nothing printed unless explicitly requested (e.g. print(...)). Code saved in a file (by design).

Python interfaces

  • REPL -- Run one command at a time. Result is printed. Code is lost when you exit.
  • Notebook -- Create and run groups of commands (cells) in a browser interface. Last result is printed. Can mix code and formatted text. Can save to a file.
  • Script mode -- Run all commands in a file. Nothing printed unless explicitly requested (e.g. print(...)). Code saved in a file (by design).

What notebooks look like

MCS 275 uses notebooks for quizzes, worksheets, and project descriptions, so you've seen these before. But you usually see a version converted to HTML.

What notebooks look like

MCS 275 uses notebooks for quizzes, worksheets, and project descriptions, so you've seen these before. But you usually see a version converted to HTML.

What notebooks look like

MCS 275 uses notebooks for quizzes, worksheets, and project descriptions, so you've seen these before. But you usually see a version converted to HTML.

How to use notebooks

Several options:
  • Google Colab -- Web tool to create, edit, run notebooks. Need a Google account. Can save or download notebooks.
  • Other online provider, e.g. Kaggle, CoCalc
  • Jupyter/iPython -- Software you install locally to create, edit, run notebooks. Browser shows the UI.
  • VS Code -- Has an extension for handling notebook files.

Jupyter, iPython, ipynb

Jupyter is software that provides a notebook interface to various languages.

iPython adds Python support to Jupyter.

Often, installing Jupyter will also install iPython.

Jupyter saves notebooks in .ipynb format, which most notebook software supports.

Jupyter install instructions

Most users can install Jupyter and iPython using pip:

python -m pip install jupyter

Then run the interface with:

python -m jupyter notebook

Of course, you need to replace python with your interpreter name.

Using Colab / Jupyter

A few of the many keyboard shortcuts:

  • shift-enter -- run the current cell
  • escape -- switch from cell editing to navigation
  • a -- in nav mode, add a new cell ABOVE this one
  • b -- in nav mode, add a new cell BELOW this one
  • dd -- in Jupyter, in nav mode, delete current cell (colab has a delete button, and a different shortcut)
  • m -- in Jupyter, in nav mode, make current cell a Markdown (text) cell

Markdown

Text cells (Colab) or markdown cells (Jupyter) contain formatted text. When editing, formatting is specified with a language called Markdown.

# Heading level 1
## Heading level 2
### Heading level 3

* Bullet list item
* Another bullet list item

1. Numbered list item
1. Another numbered list item

Links: [text to display](https://example.com)
    

Variadic functions

A function is variadic if it can accept a variable number of arguments. This is general CS terminology.

Python supports these. The syntax

def f(a,b,*args):
means that the first argument goes into variable a, the second into variable b, and any other arguments are put into a tuple which is assigned to args

Variadics and keyword arguments

The syntax

def f(a,b,**kwargs):
or
def f(a,b,*args,**kwargs):
puts extra keyword arguments into a dictionary called kwargs.

It is traditional to use the names args and kwargs, but it is not required.

Argument unpacking

Take arguments from a list or tuple:

L = [6,11,16]
f(1,*L) # calls f(1,6,11,16)

Take keyword arguments from a dict:

d = { "mcs275": "fun", "x": 42 }
f(1,z=0,**d) # calls f(1,z=0,mcs275="fun",x=42)

Think of * as "remove the brackets", and ** as "remove the curly braces".

Application

Including ...,*args,*kwargs is especially useful on higher-order functions that accept a function as an argument, and which accept arguments on behalf of that function.

Typically you don't know in advance how many extra arguments to accept, so a variadic function is needed.

References

  • Google Colab offers notebook creation, editing, execution (can use netid@uic.edu google account).
  • Some other online services allowing free use of Python notebooks: Kaggle, CoCalc
  • See Lutz, Chapter 18 for more about function arguments (including variadic functions).
  • Beazley & Jones, Chapter 7 has examples of variadic functions.
  • A Markdown guide from GitHub.

Revision history

  • 2021-01-26 Initial publication