{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 5: Program Flow II\n", "**Python for the Energy Industry**\n", "\n", "## Conditionals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `if` statement allows you to specify what to do if a condition is true. Let's say you also want to specify what to do if it is not true:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vessel is named something else\n" ] } ], "source": [ "vessel = \"ROCINANTE\"\n", "\n", "if vessel == \"QUIXOTE\": \n", " print('Vessel is named QUIXOTE')\n", " \n", "else:\n", " print('Vessel is named something else') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple conditions can be checked for using `elif` and `else`. These must immediately follow an `if` statement. `elif` will run the indented code if the previous condition is not true, but some other specified condition is true. `else` will always run if the previous condition is not true." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vessel is named DON\n" ] } ], "source": [ "vessel = \"DON\"\n", "\n", "if vessel == \"QUIXOTE\": \n", " print('Vessel is named QUIXOTE')\n", " \n", "elif vessel == \"ROCINANTE\":\n", " print('Vessel is named ROCINANTE')\n", " \n", "elif vessel == \"DON\":\n", " print('Vessel is named DON')\n", " \n", "else:\n", " print('Vessel is named something else') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, the first check is if the vessel name is 'QUIXOTE'. As it is not, a second check is done to see if the name is 'ROCINANTE'. This is true, so no further check is done. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loops\n", "\n", "A loop is used to run a chunk of code multiple times. The number of times may be specified in advance, or it may depend on some condition being true, such as in this example of a `while` loop:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_num = 1\n", "my_num = 2\n", "my_num = 3\n", "my_num = 4\n", "my_num = 5\n", "done\n" ] } ], "source": [ "my_num = 0\n", "\n", "while my_num < 5:\n", " my_num = my_num + 1\n", " print('my_num =',my_num)\n", " \n", "print('done')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "55\n" ] } ], "source": [ "my_num = 1\n", "my_sum = 0\n", "\n", "while my_num <= 10:\n", " my_sum = my_sum + my_num\n", " my_num += 1\n", " \n", "print(my_sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here the indented code will run over and over, until the condition that `my_num` is less than 5 is no longer true. Because the indented code adds 1 to `my_num` each time, the code block only runs 5 times. Be careful: if you'd forgotten to change the value of `my_num` within the indented code block, the loop would keep on running until you force it to stop!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For Loops\n", "\n", "A `for` loop is a type of loop which iterates over a number of items. For loops are often paired with `range` to iterate over successive numbers:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for x in range(3):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The usefulness of `for` loops will become more clear in the next video." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise:\n", "Use a `for` loop to print the sum of all numbers up to and including 100. You should get 5050." ] }, { "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.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }