MCS 275 Spring 2021
Emily Dumas
print(...)
). Code saved in a file (by design).print(...)
). Code saved in a file (by design).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.
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.
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.
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.
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.
A few of the many keyboard shortcuts:
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)
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
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.
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".
...,*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.