Modes

You have 2 modes:

- command: the cell is highlighted blue 
- edit: the cell is highlighted green

You switch from command to edit with Enter.

You switch from edit to command with Esc.

In [ ]:
 

There are lots of keyboard shortcuts. See the Help menu for a list. There is a command palette accessible with P in command mode. There are actually 3 shortcuts for the command palette! P, Cmd/Ctrl+Shift+P, Cmd/Ctrl+Shift+F. The last 2 are useful to open the palette while in edit mode.

Markdown for comments

Cells can contain code or text in Markdown format: https://www.markdownguide.org/

This is useful to leave nice comments with your code. You can even use LaTeX formatting for maths equations.

In command mode, you can switch the cell type with m or y

Why use a notebook?

  1. Nice comments. Useful when learning a language
  2. Easy debugging: can run command per command and print all outputs right there! The output will be recorded with the notebook
  3. Inline help: we'll see that later once we introduce Python
  4. Keeps code and outputs together: easier to remember what code made what figure !

Inline help

That means you can get some help/documentation on a Python object right here and there, you might not need to flip through web pages.

You can use ? in front of a variable to know the type of this variable.

You can use tab on a variable to list all possible methods. Using tab on a function call will give you the docstring that lists the purpose of a function and its arguments.

In [6]:
a = "hello"
In [10]:
?a

If you are not using a notebook, you can access the same help in this way:

In [9]:
print(type(a))
print()
print(dir(a))
print("")
print(a.count.__doc__)
<class 'str'>

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are
interpreted as in slice notation.

More than Python

You can do more than Python in a notebook. Notably, notebooks use the IPython Magic commands. We'll see a few in this training. But others can be found here: https://ipython.readthedocs.io/en/stable/interactive/magics.html#

They allow you to do a lot of different things: time the execution of a line of code or a cell, run an external script, run some code in another language than Python etc.

In [ ]: