{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The power of *if* statement\n", "In this lecture, we will learn the conditional statement *if*,
\n", "It is a conditional statement that test for some condition, and execute some codes accordingly.
\n", "Let's see some examples.\n", "\n", "**Created and updated by:** John C.S. Lui on August 9th, 2020.\n", "\n", "** Important note:** *If you want to use and modify this notebook file, please acknowledge the author.*" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I hate NETWORKS !!\n", "My favorite subject is: Machine Learning.\n", "I hate AI !!\n", "I hate PROGRAMMING LANGUAGES !!\n", "I hate DISTRIBUTED SYSTEMS !!\n", "I hate THEORY !!\n" ] } ], "source": [ "# Create a list of CS subjects\n", "cs_subjects = ['networks', 'machine learning', 'ai', 'programming languages', 'distributed systems', 'theory']\n", "favorite_subject = 'machine learning'\n", "\n", "for subject in cs_subjects:\n", " if subject == favorite_subject: # condition if. Note that we need the ':'\n", " print('My favorite subject is:', subject.title() + '.')\n", " else: # Takes the 'false' condition, note that we need ':'\n", " print('I hate %s !!' %subject.upper())" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My favorite subject is: Networks.\n", "My favorite subject is: Machine Learning.\n", "I hate Ai !!\n", "I hate Programming Languages !!\n", "My favorite subject is: Distributed Systems.\n", "My favorite subject is: Theory.\n" ] } ], "source": [ "# Create a list of CS subjects\n", "cs_subjects = ['networks', 'machine learning', 'ai', 'programming languages', 'distributed systems', 'theory']\n", "favorite_subject = ['machine learning', 'networks', 'theory', 'distributed systems'] # change it to a list\n", "\n", "for subject in cs_subjects:\n", " if subject in favorite_subject: # condition if. Check whether subject's membership in the list\n", " print('My favorite subject is:', subject.title() +'.')\n", " else: # Takes the 'false' condition, note that we need ':'\n", " print('I hate %s !!' %subject.title())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logical comparison operators\n", "In Python, we have the following:\n", "- equality (==)\n", "- inequlaity (!=)\n", "- greater than (>)\n", "- greater than or equal to (>=)\n", "- less than (<)\n", "- less than or equal to (<=)\n", "- and operator (and)\n", "- or operator (or)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. True\n", "2. False\n", "3. True\n", "4. True\n", "5. False\n", "6. True\n", "7. True\n" ] } ], "source": [ "# Let's try some of this logical comparison and print out their return (Boolean) values\n", "print (\"1.\", 3==3)\n", "print (\"2.\", 5==8)\n", "print (\"3.\", 1==1.0)\n", "print (\"4.\", 'John'=='John')\n", "print (\"5.\", 'John'=='john')\n", "print (\"6.\", 'John'.lower() =='john')\n", "print (\"7.\", '4' == str(4))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. True\n", "2. False\n", "3. True\n" ] } ], "source": [ "# Let's try the \">=\" operator\n", "print (\"1.\", 10 >= 8)\n", "print (\"2.\", 10 >= 80)\n", "print (\"3.\", 10 >= 10)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. False\n", "2. True\n" ] } ], "source": [ "# Let's try \"<\" operator\n", "print (\"1.\", 10 < 8)\n", "print (\"2.\", 1 < 3.0)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. False\n", "2. True\n" ] } ], "source": [ "# Let's try \"<=\" operator\n", "print (\"1.\", 10 <= 8)\n", "print (\"2.\", 1 <= 3.0)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1. True\n", "2. False\n", "3. True\n" ] } ], "source": [ "print(\"1.\", 8 <= 10 and 5 < 10)\n", "print(\"2.\", 8 <= 10 and 5 > 10)\n", "print(\"3.\", 10 <= 8 or 10 > 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's check whether an item is in the list" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Not in the list\n" ] } ], "source": [ "# Define a list\n", "list_number = [0, 1, 3, 'john', 4, 'peter', 'paul']\n", "\n", "if 'John' in list_number:\n", " print ('It is in the list')\n", "else:\n", " print ('Not in the list')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple if statement" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The length of \"list_number\" is at least 3\n", "I am here !!!\n" ] } ], "source": [ "list_number = [0, 1, 3, 'john', 4, 'peter', 'paul']\n", "if len(list_number) >= 3:\n", " print('The length of \"list_number\" is at least 3')\n", "\n", "print('I am here !!!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simpe if-else statement" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The length of \"list_number\" is greater than 3\n" ] } ], "source": [ "list_number = [0, 1, 3, 'john', 4, 'peter', 'paul']\n", "if len(list_number) <= 3:\n", " print('The length of \"list_number\" is less than or equal to 3')\n", "else:\n", " print('The length of \"list_number\" is greater than 3')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The if-elif .... else chain\n", "This is useful if you want to test a series of conditions.
\n", "In an if-elif-else chain, once a test passes, the rest of the conditions are ignored." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There is at least 4 and at most 7 items in the list\n" ] } ], "source": [ "# Define a list\n", "list_number = [0, 1, 3, 'john', 4, 'peter', 'paul']\n", "\n", "if len(list_number) == 0:\n", " print('There is no item in the list')\n", "elif len(list_number) <= 3:\n", " print('There is at least one and at most 3 items in the list.')\n", "elif len(list_number) <= 7:\n", " print('There is at least 4 and at most 7 items in the list')\n", "else:\n", " print('There is greater than 7 items in the list')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question\n", "What is the difference between if-elif and a series of many if ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "Write a program to first prompt the user for an input (hint: look up the function input() ) for a day.
\n", "Then create a list with all 7 days (assume you only use lower case).
\n", " Use if... elif to test whether the input day is a weekday or weekend.
\n", " Print out whether the input is a weekeday or weekend or an input error." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Going through a series of test\n", "What if you want to run a series of test? Let's try." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For Networks, it is John's favorite subject.\n", "For Machine Learning, it is John's favorite subject.\n", "For Ai, it is definitely not John's favorite subject.\n", "For Programming Languages, it is definitely not John's favorite subject.\n", "For Distributed Systems, it is John's favorite subject.\n", "For Theory, it is John's favorite subject.\n" ] } ], "source": [ "# define a function\n", "def print_message (subject, flag):\n", " if flag==True:\n", " print(\"For \" + subject.title() + \", it is John's favorite subject.\")\n", " else:\n", " print(\"For \" + subject.title() + \", it is definitely not John's favorite subject.\")\n", "\n", "# define a list\n", "cs_subjects = ['networks', 'machine learning', 'ai', 'programming languages', 'distributed systems', 'theory']\n", "\n", "for subject in cs_subjects:\n", " if subject == 'networks':\n", " print_message(subject, True)\n", " if subject == 'machine learning':\n", " print_message(subject, True)\n", " if subject == 'ai':\n", " print_message(subject, False)\n", " if subject == 'programming languages':\n", " print_message(subject, False)\n", " if subject == 'distributed systems':\n", " print_message(subject, True)\n", " if subject == 'theory':\n", " print_message(subject, True)\n", " " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For Networks, it is John's favorite subject.\n", "For Machine Learning, it is John's favorite subject.\n", "For Ai, it is definitely not John's favorite subject.\n", "For Programming Languages, it is definitely not John's favorite subject.\n", "For Distributed Systems, it is John's favorite subject.\n", "For Theory, it is John's favorite subject.\n" ] } ], "source": [ "## Let's see how we can write a simpler code\n", "# define a function\n", "def print_message (subject, flag):\n", " if flag==True:\n", " print(\"For \" + subject.title() + \", it is John's favorite subject.\")\n", " else:\n", " print(\"For \" + subject.title() + \", it is definitely not John's favorite subject.\")\n", "\n", "# define a list\n", "cs_subjects = ['networks', 'machine learning', 'ai', 'programming languages', 'distributed systems', 'theory']\n", "\n", "favorite_subjects = ['networks', 'machine learning', 'distributed systems', 'theory']\n", "\n", "for subject in cs_subjects:\n", " if subject in favorite_subjects:\n", " print_message(subject, True)\n", " else:\n", " print_message(subject, False)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## True and False values\n", "In general, any **non-zero** or **non-empty** value will be evaluated as True. Let check." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "For -3 it is True.\n", "For -2 it is True.\n", "For -1 it is True.\n", "For 0 it is False.\n", "For 1 it is True.\n", "For 2 it is True.\n", "For 3 it is True.\n", "----------------\n", "For -3.0 it is True.\n", "For -2.0 it is True.\n", "For -1.0 it is True.\n", "For 0.0 it is False.\n", "For 1.0 it is True.\n", "For 2.0 it is True.\n", "For 3.0 it is True.\n", "----------------\n", "*** It is False\n", "----------------\n", "*** It is True.\n", "----------------\n", "*** It is False.\n" ] } ], "source": [ "# Define a list\n", "number_list1 = [-3, -2, -1, 0, 1, 2, 3]\n", "number_list2 = [-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0]\n", "\n", "# Let's test\n", "for number in number_list1:\n", " if number:\n", " print(\"For\", number, \"it is True.\")\n", " else:\n", " print(\"For\", number, \"it is False.\")\n", "print (\"----------------\")\n", "\n", "# Let's test\n", "for number in number_list2:\n", " if number:\n", " print(\"For\", number, \"it is True.\")\n", " else:\n", " print(\"For\", number, \"it is False.\")\n", "\n", "print (\"----------------\")\n", "\n", "if '':\n", " print (\"*** It is True.\")\n", "else:\n", " print (\"*** It is False\")\n", " \n", "print (\"----------------\")\n", "\n", "if ' ': # This is a space, so it is non-empyt\n", " print (\"*** It is True.\")\n", "else:\n", " print (\"*** It is False\")\n", " \n", "print (\"----------------\")\n", "\n", "if None: # None is a special object in Python. It evaluates to False.\n", " print (\"*** It is True.\")\n", "else:\n", " print (\"*** It is False.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Let's examine the following snippets. You have to **determine** the correct output." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "11\n", "12\n", "13\n" ] } ], "source": [ "# What is the correct output for the following snippet?\n", "\n", "for num in range(10, 14):\n", " for i in range(2, num):\n", " if num%i == 1:\n", " print(num)\n", " break" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "# What is the correct output for the following snippet?\n", "\n", "x = 0\n", "a = 0\n", "b = -5\n", "if a > 0:\n", " if b < 0: \n", " x = x + 5 \n", " elif a > 5:\n", " x = x + 4\n", " else:\n", " x = x + 3\n", "else:\n", " x = x + 2\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# What is the correct output for the following snippet?\n", "\n", "if -5:\n", " print('True')\n", "else:\n", " print(\"False\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 Chair\n", "10 Table\n", "20 Chair\n", "20 Table\n" ] } ], "source": [ "# Whaat is the output for the following snippet?\n", "\n", "numbers = [10, 20]\n", "items = [\"Chair\", \"Table\"]\n", "\n", "for x in numbers:\n", " for y in items:\n", " print(x, y)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-2, -3, -4, " ] } ], "source": [ "# What is the output for the following snippet?\n", "\n", "for num in range(-2,-5,-1):\n", " print(num, end=\", \")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "J, o, h, n, " ] } ], "source": [ "# What is the output for the following snippet?\n", "for l in 'John':\n", " if l == 'o':\n", " pass\n", " print(l, end=\", \")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n" ] } ], "source": [ "# What is the output for the follwing snippet?\n", "\n", "x = 0\n", "while (x < 100):\n", " x += 2 # same as x = x+2\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21\n" ] } ], "source": [ "# What is the output for the follwing snippet?\n", "\n", "var = 10\n", "for i in range(10):\n", " for j in range(2, 10, 1):\n", " if var % 2 == 0:\n", " continue\n", " var += 1\n", " var+=1\n", "else:\n", " var+=1\n", "print(var)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# What is the output for the follwing snippet?\n", "\n", "a, b = 12, 5\n", "if a + b:\n", " print('True')\n", "else:\n", " print('False')\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# What is the output for the follwing snippet?\n", "\n", "x = 0\n", "a = 5\n", "b = 5\n", "if a > 0:\n", " if b < 0: \n", " x = x + 5 \n", " elif a > 5:\n", " x = x + 4\n", " else:\n", " x = x + 3\n", "else:\n", " x = x + 2\n", "print(x)" ] }, { "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }