{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "If Statements\n", "===\n", "\n", "If statements are at the heart of almost all decision-making in programming. If statements allow you to treat some data differently than the rest of your data." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, devin.\n", "Hello, killian.\n", "Hello, eric.\n", "Hello, matt.\n", " Please, no puns today!\n", "Hello, psalm.\n", "Hello, roger.\n" ] } ], "source": [ "# Example.\n", "names = ['devin', 'killian', 'eric', 'matt', 'psalm', 'roger']\n", "\n", "for name in names:\n", " print(f\"Hello, {name}.\")\n", " if name == 'matt':\n", " print(\" Please, no puns today!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conditional Statements\n", "---\n", "\n", "Conditional statements evaluate to True or False." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "False" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 == 5" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 == 5" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "age = 17\n", "\n", "age == 18\n", "age > 18\n", "age < 18\n", "age >= 18\n", "age <= 18\n", "age != 18 # age is not equal to 18?" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "artists = ['prince', 'frieda kahlo', 'stan lee', 'isaac asimov']\n", "\n", "'stan lee' in artists " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "all_students = [...]\n", "\n", "eligible_voters = []\n", "\n", "for student in all_students:\n", " if student.age >= 18:\n", " eligible_voters.append(student)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple if statements\n", "---\n", "\n", "You're only interested in one condition." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n" ] } ], "source": [ "ages = [16, 18, 11, 17, 16, 17, 19, 18, 21, 45, 89, 1, 7]\n", "\n", "for age in ages:\n", " if age >= 18:\n", " print(\"Hey, did you know you can vote?!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If-else statements\n", "---\n", "\n", "You're interested in one condition, but you also want to do something else with everyone who doesn't meet that condition." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Grow up! And some day you'll be able to vote.\n", "Hey, did you know you can vote?!\n", "Grow up! And some day you'll be able to vote.\n", "Grow up! And some day you'll be able to vote.\n", "Grow up! And some day you'll be able to vote.\n", "Grow up! And some day you'll be able to vote.\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Grow up! And some day you'll be able to vote.\n", "Grow up! And some day you'll be able to vote.\n" ] } ], "source": [ "ages = [16, 18, 11, 17, 16, 17, 19, 18, 21, 45, 89, 1, 7]\n", "\n", "for age in ages:\n", " if age >= 18:\n", " print(\"Hey, did you know you can vote?!\")\n", " else:\n", " print(\"Grow up! And some day you'll be able to vote.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If-elif-else statements\n", "---\n", "\n", "You're interested in several specific conditions." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Grow up! And some day you'll be able to vote.\n", "Hey, did you know you can vote?!\n", "Grow up! And some day you'll be able to vote.\n", "Please get ready to register to vote as soon as you turn 18!\n", "Grow up! And some day you'll be able to vote.\n", "Please get ready to register to vote as soon as you turn 18!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Hey, did you know you can vote?!\n", "Grow up! And some day you'll be able to vote.\n", "Grow up! And some day you'll be able to vote.\n" ] } ], "source": [ "ages = [16, 18, 11, 17, 16, 17, 19, 18, 21, 45, 89, 1, 7]\n", "\n", "for age in ages:\n", " if age >= 18:\n", " print(\"Hey, did you know you can vote?!\")\n", " elif age <= 16:\n", " print(\"Grow up! And some day you'll be able to vote.\")\n", " else:\n", " print(\"Please get ready to register to vote as soon as you turn 18!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If-if-if statements\n", "---\n", "\n", "You want to test a bunch of situations, and any or all conditions can be met." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Adding sardines.\n", "Adding onions.\n" ] } ], "source": [ "philip_pizza = ['sardines', 'onions']\n", "\n", "# Pizza making code.\n", "if 'pepperoni' in philip_pizza:\n", " print(\"Adding pepperoni.\")\n", "if 'sardines' in philip_pizza:\n", " print(\"Adding sardines.\")\n", "if 'mushrooms' in philip_pizza:\n", " print(\"Adding mushrooms.\")\n", "if 'onions' in philip_pizza:\n", " print(\"Adding onions.\")" ] } ], "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }