{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 0. Python Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Launching python:\n", "1. Open a terminal window (find the program 'Terminal' on your ubuntu machine and open it).\n", "2. Type `python` to open a python session.\n", "3. Close the session by typing `Ctl+D` or `quit()` or `exit()`\n", "\n", "Launching an ipython notebook:\n", "1. Open a terminal window\n", "2. Type 'jupyter notebook'. This will open an interactive ipython notebook in your web browser (like this one).\n", "3. Close by typing `Ctl+C` in your terminal window, or via the dropdown menu within the notebook: `File > Close and Halt`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Useful resources for python:\n", "- [Python Documentation](https://docs.python.org/3.6/) (tutorials, library reference, ...)\n", "- [CodeAcademy Python tutorial](https://www.codecademy.com/learn/learn-python)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A first command in python:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Hello world')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`.\n", "\n", "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](https://docs.python.org/3.6/howto/pyporting.html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 0.1:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A first python script:\n", "1. Open a text editor (eg. gedit) (type `gedit` into a terminal and press enter; the text editor will open in a new window)\n", "2. type the above command into the editor and save the file as 'hello.py' in the home directory\n", "3. Close gedit\n", "4. Open a terminal window\n", "5. Type `python hello.py` to run the script" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 0.2:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, print the phrase \"This is the second command\" to output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 0.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A note on python comments\n", "\n", "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.\n", "\n", "It is considered good practice to comment your code in order to make it more readable for yourself and any future users." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Review\n", "\n", "We have:\n", "- used a function, `print`\n", "- written our first script\n", "- run the script using `python`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 1 }