Launching python:
python
to open a python session.Ctl+D
or quit()
or exit()
Launching an ipython notebook:
Ctl+C
in your terminal window, or via the dropdown menu within the notebook: File > Close and Halt
.Useful resources for python:
A first command in python:
print('Hello world')
The print
function takes an input value (some text, in quotation marks) and prints the text as output (without the quotation marks). One should note that Python is case-sensitive - ie. we can't use Print
or PRINT
.
Note: As of python 3, the inputs to the 'print' function must be placed within parantheses. If you use code developed for python 2.7 or lower, you may notice that this requirement is not followed and therefore will cause issues if run using python 3+. There are other key differences between python 3 and previous versions, some of which will be covered in this tutorial. For a more detailed look into them, the following resource may be of use: Porting Python 2 Code to Python 3
A first python script:
gedit
into a terminal and press enter; the text editor will open in a new window)python hello.py
to run the scriptIn this notebook, print the phrase "This is the second command" to output
# Exercise 0.2
You will have noticed that the "# Exercise 0.2" in the above box does not affect the notebook output. In python in-line code comments are indicated using the # symbol. Any text that comes after this is considered to be a comment and is ignored by the python interpreter. Other means of commenting python code, such as docstrings, also exist, however their use is beyond the scope of this tutorial and will not be covered.
It is considered good practice to comment your code in order to make it more readable for yourself and any future users.