{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "These notes follow the official python tutorial pretty closely: http://docs.python.org/3/tutorial/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Control Flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To write a program, we need the ability to iterate and take action based on the values of a variable. This includes if-tests and loops.\n", "\n", "Python uses whitespace to denote a block of code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## While loop\n", "\n", "A simple while loop—notice the indentation to denote the block that is part of the loop.\n", "\n", "Here we also use the compact `+=` operator: `n += 1` is the same as `n = n + 1`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 0\n", "while n < 10:\n", " print(n)\n", " n += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This was a very simple example. But often we'll use the `range()` function in this situation. Note that `range()` can take a stride." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "for n in range(2, 10, 2):\n", " print(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(list(range(10)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## if statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`if` allows for branching. python does not have a select/case statement like some other languages, but `if`, `elif`, and `else` can reproduce any branching functionality you might need." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 0\n", "\n", "if x < 0:\n", " print(\"negative\")\n", "elif x == 0:\n", " print(\"zero\")\n", "else:\n", " print(\"positive\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterating over elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "it's easy to loop over items in a list or any _iterable_ object. The `in` operator is the key here." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alist = [1, 2.0, \"three\", 4]\n", "for a in alist:\n", " print(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for c in \"this is a string\":\n", " print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can combine loops and if-tests to do more complex logic, like break out of the loop when you find what you're looking for" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 0\n", "for a in alist:\n", " if a == \"three\":\n", " break\n", " else:\n", " n += 1\n", "\n", "print(n)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(for that example, however, there is a simpler way)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(alist.index(\"three\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "for dictionaries, you can also loop over the elements" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_dict = {\"key1\":1, \"key2\":2, \"key3\":3}\n", "\n", "for k, v in my_dict.items():\n", " print(\"key = {}, value = {}\".format(k, v)) # notice how we do the formatting here\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for k in sorted(my_dict):\n", " print(k, my_dict[k])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "sometimes we want to loop over a list element and know its index -- `enumerate()` helps here:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for n, a in enumerate(alist):\n", " print(n, a)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "

Quick Exercise:

\n", "\n", "`zip()` allows us to loop over two iterables at the same time. Consider the following two\n", "lists:\n", "\n", "```\n", "a = [1, 2, 3, 4, 5, 6, 7, 8]\n", "b = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"]\n", "```\n", "\n", "`zip(a, b)` will act like a list with each element a tuple with one item from `a` and the corresponding element from `b`. \n", "\n", "Try looping over these lists together (using `zip()`) and print the corresponding elements from each list together on a single line.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "

Quick Exercise:

\n", "\n", "The `.split()` function on a string can split it into words (separating on spaces). \n", "\n", "Using `.split()`, loop over the words in the string\n", "\n", "`a = \"The quick brown fox jumped over the lazy dog\"`\n", "\n", "and print one word per line\n", "\n", "
" ] }, { "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.3" } }, "nbformat": 4, "nbformat_minor": 1 }