Newand (2) hit
Untitledyou may give your Notebook a proper name:
SHIFT-ENTER-key and the
CTRL-ENTERkey-strokes. These will be demonstrated in the next section.
In [ ]:prompt is where you type in some Python statements. You will soon learn more about the syntax of Python statements, but for now, we will define a variable,
xthat contains the list of numbers from 0 to 4,
[0, 1, 2, 3, 4], and we will issue a statement,
sum(x), that adds up all numbers in the list:
CTRL-ENTER, that causes the cell to run, i.e. causes the statements in the cell to be executed:
1that appears in
In :indicates that the cell was run as the first one since the Notebook was started, and the
Out : 10provides the output, in this case the sum, . After the
CTRL-ENTERkey-stroke, there is still only one cell in the Notebook. Often, you would, however, like to edit a new cell immediately after running the previous, which is most easily done by hitting instead
ESCthe cell will transition to command mode and will now accept commands. In command mode, hitting
deletes the cell, while
binserts a cell
below the cell. You may hit
ENTERto go back to edit mode, or simply hit the cell with a left-mouse click.
SHIFT-ENTERkeystroke the cell is executed:
DOWN-ARROWkey-strokes no matter whether you are in command or edit mode. Once you reach a cell you want to edit you may enter edit mode with
ENTERand exit to command mode with
ESC. You may also use the left-mouse button to select a cell. Assuming that you select the first cell and type in new values in the list, it may look like this:
xhave been increased by a factor 10, but no output values will change before you hit
CTRL-ENTERthat will trigger the execution of the statements in the cell:
Out :prompts. Notice also, that executing a cell does not affect any other cells. The second and third cells are unchanged, even though we have now changed the value of the variable
ythat are currently visible in the top-part of the notebook, unlike the situation when only the first cell in the notebook was reevaluated after the list was changed, see this figure.
sum, the value that the function returns becomes the output, cf. the first cell in this figure:
mysum = sum(x)does not lead to any output. The reason for this is that the Notebook does not get any value back when Python performs an assignment statement. What has happened is that the variable
mysumnow contains the value that
sum(x)returned (the value 10 that was also the output of the first cell).
mysum, twice the value, and ten times the value. Notice how convenient it is that we may now get multiple output from executing a cell will several statements.
Choose which booklet to go to: