{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#if,elif,else Statements\n", "\n", "if Statements in Python allows us to tell the computer to perform alternative actions based on a certain set of results.\n", "\n", "Verbally, we can imagine we are telling the computer:\n", "\n", "\"Hey if this case happens, perform some action\"\n", "\n", "We can then expand the idea further with elif and else statements, which allow us to tell the computer:\n", "\n", "\"Hey if this case happens, perform some action. Else if another case happens, perform some other action. Else-- none of the above cases happened, perform this action\"\n", "\n", "Let's go ahead and look at the syntax format for if statements to get a better idea of this:\n", "\n", " if case1:\n", " perform action1\n", " elif case2:\n", " perform action2\n", " else: \n", " perform action 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First Example\n", "\n", "Let's see a quick example of this:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It was true!\n" ] } ], "source": [ "if True:\n", " print 'It was true!'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's add in some else logic:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I will be printed in any case where x is not true\n" ] } ], "source": [ "x = False\n", "\n", "if x:\n", " print 'x was True!'\n", "else:\n", " print 'I will be printed in any case where x is not true'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiple Branches\n", "\n", "Let's get a fuller picture of how far if, elif, and else can take us!\n", "\n", "We write this out in a nested structure. Take note of how the if,elif,and else line up in the code. This can help you see what if is related to what elif or else statements.\n", "\n", "We'll reintroduce a comparison syntax for Python." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to the bank!\n" ] } ], "source": [ "loc = 'Bank'\n", "\n", "if loc == 'Auto Shop':\n", " print 'Welcome to the Auto Shop!'\n", "elif loc == 'Bank':\n", " print 'Welcome to the bank!'\n", "else:\n", " print \"Where are you?\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note how the nested if statements are each checked until a True boolean causes the nested code below it to run. You should also note that you can put in as many elif statements as you want before you close off with an else.\n", "\n", "Let's create two more simple examples for the if,elif, and else statements:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome Sammy!\n" ] } ], "source": [ "person = 'Sammy'\n", "\n", "if person == 'Sammy':\n", " print 'Welcome Sammy!'\n", "else:\n", " print \"Welcome, what's your name?\" " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome George!\n" ] } ], "source": [ "person = 'George'\n", "\n", "if person == 'Sammy':\n", " print 'Welcome Sammy!'\n", "elif person =='George':\n", " print \"Welcome George!\"\n", "else:\n", " print \"Welcome, what's your name?\" " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Indentation\n", "\n", "It is important to keep a good understanding of how indentation works in Python to maintain the structure and order of your code. We will touch on this topic again when we start building out functions!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }