Python Crash Course
In this section we dive into the python3 programming language and cover a number of basic data types, their methods, and their usage.
There is an excellent online python visualizer at http://pythontutor.com. This allows you to enter your code and visualize its execution. It is great for debugging code.
Python Access
For instant access to python3 use the online syzygy server provided by PIMS: https://pims.syzygy.ca/. Access is free for anyone with a google account. Use it to try out any of the commands listed in this section.
Arithmetic Operations
We can use python like a calculator to add/subtract/multiply/divide numbers.
Output:
5
When typing input, enter
will jump you down to the next line, whereas shift-enter
gets python to evaluate the code-cell. You can also press the run button in the menu bar instead of shift- enter
.
A bit of terminology: What is typed in a cell is called the source code. When the cell is executed, what python prints to the screen is called the output. So 2+3
in the cell above is the source code, and 5
is the output.
Common arithmetic operations are:
operation | syntax name |
---|---|
addition | + |
subtraction | - |
multiplication | * |
division | / |
remainder | % |
integer quotient | // |
exponentiation | ** |
Output:
31/5
8
225
3
2
Python follows the usual order of operations:
- first evaluate exponents from right to left,
- then multiplication, division, remainder from left to right,
- finally, addition and subtraction from left to right.
The order in which expressions are evaluated can be changed using parenthesis: ( ).
Inserting a New Cell
A new cell will automatically appear below the last cell of your sheet when the contents of the last cell have been evaluated. Sometimes, however, we would like to add a new cell in the middle of our worksheet. To insert a new execution shell in the worksheet, you can click the +
button in the menu. This adds a cell just below your currently selected cell. You can use the arrow keys in the menu to moved the cells up or down. If you just want to add a cell, or a bunch of cells, at the end of the worksheet, just select the last cell and hit shift-enter
to add a new cell.
Working in a Cell: The Semicolon and Comments
Multiple statements can be placed in a single cell. After typing 1+2
, use enter
to bring the cursor down to the next line. When the cell is evaluated (shift-enter
) only the output of the last line of code is displayed.
Output:
1
In python, semicolons can be placed after statements as separators, used to place multiple statements on the same line.
Output:
1
Still only the output of the last command is shown on the screen. To see the both outputs we use the print
command.
Output:
3
1
It will come in handy to be able to add comments directly in the source code. Comments are basically notes to yourself about what you are doing, and are not intended for the computer to execute. Anything followed by the #
symbol is treated as a comment.
Output:
-1
When a comment is too long to fit in one line, we can enclose it in triple quotes:
""" text here """
.
"""here is a really long comment. What the next line does is
evaluate an expression. Nothing special, but my comment is
now pretty long."""
5**4
625