{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 4: Program Flow I\n", "**Python for the Energy Industry**\n", "\n", "This is a Jupyter Notebook. A Notebook is composed of cells of Python code which can be run separately. When a cell runs, each line of Python code is run sequentially, and any outputs are displayed below the cell. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_num = 5\n", "my_num = 7\n", "my_num = 14\n" ] } ], "source": [ "# A simple example of running a cell\n", "my_num = 5\n", "print('my_num =',my_num)\n", "\n", "my_num = my_num + 2\n", "print('my_num =',my_num)\n", "\n", "my_num = my_num * 2\n", "print('my_num =',my_num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Understanding Indentation\n", "\n", "In Python, indendation is used to control the order in which lines of code are run. Consider the below:\n", "\n", "```python\n", "control line with colon:\n", " indented code block line\n", " indented code block line\n", " indented code block line\n", "\n", "code block ends when indent ends\n", "```\n", "\n", "The 'control line' determines if, and how many times, the indented code block runs. Once this finishes, the next line of code that is not indented will run. \n", "\n", "This will make more sense when we see some examples. So let's consider the two main types of control line: conditions, and loops." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conditionals\n", "\n", "A conditional control line will allow you to run some lines of code, only if a particular statement is true. The following is an example of the `if` statement." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_num = 6\n" ] } ], "source": [ "my_num = 2\n", "\n", "if my_num == 2: # is my_num equal to 2?\n", " my_num = my_num * 3 # if so, multiply it by 3\n", " \n", "print('my_num =',my_num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here `==` is used to check whether two things are equal. So in this example, the indented code line which multiplies `my_num` by 3, will only run if `my_num` is equal to 2. Note that `==` is different from `=`, the 'assignment' operator, which sets the value of a variable.\n", "\n", "`==` is an example of a 'comparative' operator. Other common comparative operators in Python are:\n", "\n", "| operator | meaning |\n", "|---|---|\n", "| `<` | less than |\n", "| `>` | greater than |\n", "| `<=` | less than or equal to |\n", "| `>=` | greater than or equal to |\n", "| `!=` | not equal to |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Modify the above code block to:\n", "\n", " a) Change the initial value of my_num\n", " b) Change the operator used in the control line\n", "\n", "Check that the printed output changes as you would expect." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }