{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditionals - flow-control structures\n", "\n", "- conditional statements control the flow of execution of codes\n", " - makes some block of code to skip or execute based on some conditions\n", " - loosely speaking, it helps computer think and make decision\n", "- boolean values - True or False\n", "- boolean expressions - comparison operators are used to compare values that result in True or False value." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Comparison operators\n", "- allow us to compare data resulting in True or False outcome\n", "- ==, !=, >, >=, <, <=" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n", "False\n", "True\n", "True\n", "False\n" ] } ], "source": [ "# Examples of various comparison operators\n", "# x == y => True if x is equal to y\n", "# x != y => True if x is not equal to y\n", "# x > y => True if x is greater than y\n", "# x < y => True if x is less than y\n", "# x >= y => True if x is greater than or equal to y\n", "# x <= y => True if x is less than or equal to y\n", "x = 5\n", "y = 10\n", "print(x==y)\n", "print(x!=y)\n", "print(x>y)\n", "print(x=y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional execution\n", "- Three types of conditional selectors\n", "- One-way, Two-way and Multi-way selectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## One-way selector\n", "- just one if statement by itself\n", "- syntax:\n", "```python\n", "if == True:\n", " # execute this block of code\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "if True:\n", " print('hello')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a equals a\n" ] } ], "source": [ "if 'a' == 'a':\n", " print('a equals to a')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Two-way selector\n", "- if statement followed by else statement\n", "- syntax:\n", "\n", "```python\n", "if == True:\n", " # execute this block of code\n", "else:\n", " # do this\n", " \n", "```" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "print 10 is NO greater than or equal to 20\n" ] } ], "source": [ "if 10 >= 20:\n", " print ('print 10 is greater than or equal to 20')\n", "else:\n", " print ('print 10 is NO greater than or equal to 20')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "if 100.5 >= 100.5:\n", " print('True')\n", "else:\n", " print('False')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multi-way selector\n", "- similar to multiple-choice questions with only one valid answer\n", "- start from first if statement and as soon as a if expression is evaluated True all the rest elif, else are ignored\n", "\n", "```python\n", "if : \n", " # block1\n", " # execute this...\n", "elif :\n", " # block2 \n", " # execute this\n", "... \n", "... \n", "else: \n", " # else block\n", " # execute code in this block\n", " \n", "```" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number between 1-5:10\n", "You entered a number larger than 5\n" ] } ], "source": [ "# Guess the number entered by user\n", "ans = int(input('Enter a number between 1-5:'))\n", "if ans == 1:\n", " print('You entered 1')\n", "elif ans == 2:\n", " print('You entered 2')\n", "elif ans == 3:\n", " print('You entered 3')\n", "elif ans == 4:\n", " print('You entered 4')\n", "elif ans == 5:\n", " print('You entered 5')\n", "elif ans > 5:\n", " print('You entered a number larger than 5')\n", "else:\n", " print('I do not know what you entered! Hmm....')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Visualize using pythontutor.com: https://goo.gl/ZSm9KS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logical Operators\n", "**and**, **or**, and **not** -- allow to build more complex boolean expressions
\n", "\n", "**Truth table for and**\n", "\n", "| a | b | a **and** b |\n", "| --- | --- | --- |\n", "| T | T | T |\n", "|T | F | F |\n", "|F | T | F |\n", "|F | F | F |\n", "\n", "\n", "**Truth table for or**\n", "\n", "| a | b | a **or** b |\n", "| --- | --- | --- |\n", "| T | T | T |\n", "|T | F | T |\n", "|F | T | T |\n", "|F | F | F |\n", "\n", "**Truth table for not**\n", "\n", "|a | not a |\n", "|---|---|\n", "|T | F |\n", "|F | T |\n", "\n", "### Order of Evaluations: Highest to Lowest:
\n", "http://www.informit.com/articles/article.aspx?p=459269&seqNum=11" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 is positive and even\n" ] } ], "source": [ "num = 10\n", "if (num %2 ==0 and num > 0):\n", " print(num, \"is positive and even\")\n", " " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "num = -14\n", "if (num %2 ==0 and num < 0):\n", " print(num, \"is negative and even\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "if not False:\n", " print('True')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises\n", "Exercise 1: Write a program to test whether a given whole number is even, odd or zero" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = input('Enter a whole number:' )\n", "x = int(x)\n", "if x == 0:\n", " print(x, 'is zero')\n", "elif x%2 == 0:\n", " print(x, 'is even')\n", "else:\n", " print(x, 'is odd')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Exercise 2. Improve the above program to tell whether the given whole number is positive or negative." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise 3. Write a program that helps someone decide where to go eat lunch depending on amount of money one has in their pocket." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise 4. Given a day of week as integer (say 1 for Sunday) write a program that tells whether that day is weekend, or weekday and the actual name of the day." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nested conditionals\n", "```python\n", "if condition:\n", " if condition1:\n", " # do something\n", " else:\n", " # do something else\n", " if condition2:\n", " # do something...\n", "else:\n", " # do this...\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise 5: Write a program that determines whether someone is eligible to vote in the US federal election." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise 6:\n", "
\n",
    "Write a function day_name that converts an integer number 0 to 6 into the name of a day. Assume day 0 is “Sunday”. Once again, return None if the arguments to the function are not valid. Here are some tests that should pass:\n",
    "\n",
    "\n",
    "assert day_name(3) == \"Wednesday\"\n",
    "assert day_name(6) == \"Saturday\"\n",
    "assert day_name(42) == None\n",
    "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Exercise 7:\n", "
\n",
    "Write a function that helps answer questions like ‘“Today is Wednesday. I leave on holiday in 19 days time. What day will that be?”’ So the function must take a day name and a delta argument — the number of days to add — and should return the resulting day name.\n",
    "\n",
    "Here are some tests that should pass:\n",
    "assert day_add(\"Monday\", 4) ==  \"Friday\"\n",
    "assert day_add(\"Tuesday\", 0) == \"Tuesday\"\n",
    "assert day_add(\"Tuesday\", 14) == \"Tuesday\"\n",
    "assert day_add(\"Sunday\", 100) == \"Tuesday\"\n",
    "assert day_add(\"Sunday\", -1) == \"Saturday\"\n",
    "assert day_add(\"Sunday\", -7) == \"Sunday\"\n",
    "assert day_add(\"Tuesday\", -100) == \"Sunday\"\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.2" } }, "nbformat": 4, "nbformat_minor": 2 }