Skip to content

Instantly share code, notes, and snippets.

@simonpeterdev
Created June 10, 2021 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonpeterdev/1443952f9979f59ed7f20db6e7513b63 to your computer and use it in GitHub Desktop.
Save simonpeterdev/1443952f9979f59ed7f20db6e7513b63 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Conditions in Python\n",
"\n",
"Estimated time needed: **20** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"* work with condition statements in Python, including operators, and branching.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li>\n",
" <a href=\"#cond\">Condition Statements</a>\n",
" <ul>\n",
" <li><a href=\"comp\">Comparison Operators</a></li>\n",
" <li><a href=\"branch\">Branching</a></li>\n",
" <li><a href=\"logic\">Logical operators</a></li>\n",
" </ul>\n",
" </li>\n",
" <li>\n",
" <a href=\"#quiz\">Quiz on Condition Statement</a>\n",
" </li>\n",
" </ul>\n",
"\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"cond\">Condition Statements</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"comp\">Comparison Operators</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparison operations compare some value or operand and based on a condition, produce a Boolean. When comparing two values you can use these operators:\n",
"\n",
"<ul>\n",
" <li>equal: <b>==</b></li>\n",
" <li>not equal: <b>!=</b></li>\n",
" <li>greater than: <b>></b></li>\n",
" <li>less than: <b>&lt;</b></li>\n",
" <li>greater than or equal to: <b>>=</b></li>\n",
" <li>less than or equal to: <b>&lt;=</b></li>\n",
"</ul>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's assign <code>a</code> a value of 5. Use the equality operator denoted with two equal <b>==</b> signs to determine if two values are equal. The case below compares the variable <code>a</code> with 6.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Condition Equal\n",
"\n",
"a = 5\n",
"a == 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The result is <b>False</b>, as 5 does not equal to 6.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the following equality comparison operator: <code>i > 5</code>. If the value of the left operand, in this case the variable <b>i</b>, is greater than the value of the right operand, in this case 5, then the statement is <b>True</b>. Otherwise, the statement is <b>False</b>. If <b>i</b> is equal to 6, because 6 is larger than 5, the output is <b>True</b>.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 6\n",
"i > 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set <code>i = 2</code>. The statement is False as 2 is not greater than 5:\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Greater than Sign\n",
"\n",
"i = 2\n",
"i > 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's display some values for <code>i</code> in the figure. Set the values greater than 5 in green and the rest in red. The green region represents where the condition is **True**, the red where the statement is **False**. If the value of <code>i</code> is 2, we get **False** as the 2 falls in the red region. Similarly, if the value for <code>i</code> is 6 we get a **True** as the condition falls in the green region.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsGreater.gif\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The inequality test uses an exclamation mark preceding the equal sign, if two operands are not equal then the condition becomes **True**. For example, the following condition will produce **True** as long as the value of <code>i</code> is not equal to 6:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 2\n",
"i != 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When <code>i</code> equals 6 the inequality expression produces <b>False</b>.\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Inequality Sign\n",
"\n",
"i = 6\n",
"i != 6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"See the number line below. When the condition is **True**, the corresponding numbers are marked in green and for where the condition is **False** the corresponding number is marked in red. If we set <code>i</code> equal to 2 the operator is true, since 2 is in the green region. If we set <code>i</code> equal to 6, we get a **False**, since the condition falls in the red region.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsIneq.gif\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can apply the same methods on strings. For example, we can use an equality operator on two different strings. As the strings are not equal, we get a **False**.\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use Equality sign to compare the strings\n",
"\n",
"\"ACDC\" == \"Michael Jackson\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we use the inequality operator, the output is going to be **True** as the strings are not equal.\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use Inequality sign to compare the strings\n",
"\n",
"\"ACDC\" != \"Michael Jackson\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The inequality operation is also used to compare the letters/words/symbols according to the ASCII value of letters. The decimal value shown in the following table represents the order of the character:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<style type=\"text/css\">\n",
".tg {border-collapse:collapse;border-spacing:0;}\n",
".tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n",
" overflow:hidden;padding:10px 5px;word-break:normal;}\n",
".tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n",
" font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n",
".tg .tg-baqh{text-align:center;vertical-align:top}\n",
".tg .tg-7geq{background-color:#ffffc7;text-align:center;vertical-align:top}\n",
".tg .tg-1cln{background-color:#ffcc67;font-size:100%;font-weight:bold;text-align:center;vertical-align:top}\n",
".tg .tg-xozw{background-color:#ffcc67;font-weight:bold;text-align:center;vertical-align:top}\n",
"</style>\n",
"\n",
"<table class=\"tg\">\n",
"<thead>\n",
" <tr>\n",
" <th class=\"tg-1cln\">Char.</th>\n",
" <th class=\"tg-xozw\">ASCII</th>\n",
" <th class=\"tg-xozw\">Char.</th>\n",
" <th class=\"tg-xozw\">ASCII</th>\n",
" <th class=\"tg-xozw\">Char.</th>\n",
" <th class=\"tg-xozw\">ASCII</th>\n",
" <th class=\"tg-xozw\">Char.</th>\n",
" <th class=\"tg-xozw\">ASCII</th>\n",
" </tr>\n",
"</thead>\n",
"<tbody>\n",
" <tr>\n",
" <td class=\"tg-7geq\">A</td>\n",
" <td class=\"tg-baqh\">65</td>\n",
" <td class=\"tg-7geq\">N</td>\n",
" <td class=\"tg-baqh\">78</td>\n",
" <td class=\"tg-7geq\">a</td>\n",
" <td class=\"tg-baqh\">97</td>\n",
" <td class=\"tg-7geq\">n</td>\n",
" <td class=\"tg-baqh\">110</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">B</td>\n",
" <td class=\"tg-baqh\">66</td>\n",
" <td class=\"tg-7geq\">O</td>\n",
" <td class=\"tg-baqh\">79</td>\n",
" <td class=\"tg-7geq\">b</td>\n",
" <td class=\"tg-baqh\">98</td>\n",
" <td class=\"tg-7geq\">o</td>\n",
" <td class=\"tg-baqh\">111</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">C</td>\n",
" <td class=\"tg-baqh\">67</td>\n",
" <td class=\"tg-7geq\">P</td>\n",
" <td class=\"tg-baqh\">80</td>\n",
" <td class=\"tg-7geq\">c</td>\n",
" <td class=\"tg-baqh\">99</td>\n",
" <td class=\"tg-7geq\">p</td>\n",
" <td class=\"tg-baqh\">112</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">D</td>\n",
" <td class=\"tg-baqh\">68</td>\n",
" <td class=\"tg-7geq\">Q</td>\n",
" <td class=\"tg-baqh\">81</td>\n",
" <td class=\"tg-7geq\">d</td>\n",
" <td class=\"tg-baqh\">100</td>\n",
" <td class=\"tg-7geq\">q</td>\n",
" <td class=\"tg-baqh\">113</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">E</td>\n",
" <td class=\"tg-baqh\">69</td>\n",
" <td class=\"tg-7geq\">R</td>\n",
" <td class=\"tg-baqh\">82</td>\n",
" <td class=\"tg-7geq\">e</td>\n",
" <td class=\"tg-baqh\">101</td>\n",
" <td class=\"tg-7geq\">r</td>\n",
" <td class=\"tg-baqh\">114</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">F</td>\n",
" <td class=\"tg-baqh\">70</td>\n",
" <td class=\"tg-7geq\">S</td>\n",
" <td class=\"tg-baqh\">83</td>\n",
" <td class=\"tg-7geq\">f</td>\n",
" <td class=\"tg-baqh\">102</td>\n",
" <td class=\"tg-7geq\">s</td>\n",
" <td class=\"tg-baqh\">115</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">G</td>\n",
" <td class=\"tg-baqh\">71</td>\n",
" <td class=\"tg-7geq\">T</td>\n",
" <td class=\"tg-baqh\">84</td>\n",
" <td class=\"tg-7geq\">g</td>\n",
" <td class=\"tg-baqh\">103</td>\n",
" <td class=\"tg-7geq\">t</td>\n",
" <td class=\"tg-baqh\">116</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">H</td>\n",
" <td class=\"tg-baqh\">72</td>\n",
" <td class=\"tg-7geq\">U</td>\n",
" <td class=\"tg-baqh\">85</td>\n",
" <td class=\"tg-7geq\">h</td>\n",
" <td class=\"tg-baqh\">104</td>\n",
" <td class=\"tg-7geq\">u</td>\n",
" <td class=\"tg-baqh\">117</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">I</td>\n",
" <td class=\"tg-baqh\">73</td>\n",
" <td class=\"tg-7geq\">V</td>\n",
" <td class=\"tg-baqh\">86</td>\n",
" <td class=\"tg-7geq\">i</td>\n",
" <td class=\"tg-baqh\">105</td>\n",
" <td class=\"tg-7geq\">v</td>\n",
" <td class=\"tg-baqh\">118</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">J</td>\n",
" <td class=\"tg-baqh\">74</td>\n",
" <td class=\"tg-7geq\">W</td>\n",
" <td class=\"tg-baqh\">87</td>\n",
" <td class=\"tg-7geq\">j</td>\n",
" <td class=\"tg-baqh\">106</td>\n",
" <td class=\"tg-7geq\">w</td>\n",
" <td class=\"tg-baqh\">119</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">K</td>\n",
" <td class=\"tg-baqh\">75</td>\n",
" <td class=\"tg-7geq\">X</td>\n",
" <td class=\"tg-baqh\">88</td>\n",
" <td class=\"tg-7geq\">k</td>\n",
" <td class=\"tg-baqh\">107</td>\n",
" <td class=\"tg-7geq\">x</td>\n",
" <td class=\"tg-baqh\">120</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">L</td>\n",
" <td class=\"tg-baqh\">76</td>\n",
" <td class=\"tg-7geq\">Y</td>\n",
" <td class=\"tg-baqh\">89</td>\n",
" <td class=\"tg-7geq\">l</td>\n",
" <td class=\"tg-baqh\">108</td>\n",
" <td class=\"tg-7geq\">y</td>\n",
" <td class=\"tg-baqh\">121</td>\n",
" </tr>\n",
" <tr>\n",
" <td class=\"tg-7geq\">M</td>\n",
" <td class=\"tg-baqh\">77</td>\n",
" <td class=\"tg-7geq\">Z</td>\n",
" <td class=\"tg-baqh\">90</td>\n",
" <td class=\"tg-7geq\">m</td>\n",
" <td class=\"tg-baqh\">109</td>\n",
" <td class=\"tg-7geq\">z</td>\n",
" <td class=\"tg-baqh\">122</td>\n",
" </tr>\n",
"</tbody>\n",
"</table>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, the ASCII code for <b>!</b> is 33, while the ASCII code for <b>+</b> is 43. Therefore <b>+</b> is larger than <b>!</b> as 43 is greater than 33.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, from the table above we see that the value for <b>A</b> is 65, and the value for <b>B</b> is 66, therefore:\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"\n",
"'B' > 'A'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When there are multiple letters, the first letter takes precedence in ordering:\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Compare characters\n",
"\n",
"'BA' > 'AB'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Note</b>: Upper Case Letters have different ASCII code than Lower Case Letters, which means the comparison between the letters in Python is case-sensitive.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"branch\">Branching</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Branching allows us to run different statements for different inputs. It is helpful to think of an **if statement** as a locked room, if the statement is **True** we can enter the room and your program will run some predefined tasks, but if the statement is **False** the program will ignore the task.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, consider the blue rectangle representing an ACDC concert. If the individual is older than 18, they can enter the ACDC concert. If they are 18 or younger, they cannot enter the concert.\n",
"\n",
"We can use the condition statements learned before as the conditions that need to be checked in the **if statement**. The syntax is as simple as <code> if <i>condition statement</i> :</code>, which contains a word <code>if</code>, any condition statement, and a colon at the end. Start your tasks which need to be executed under this condition in a new line with an indent. The lines of code after the colon and with an indent will only be executed when the **if statement** is **True**. The tasks will end when the line of code does not contain the indent.\n",
"\n",
"In the case below, the code <code>print(“you can enter”)</code> is executed only if the variable <code>age</code> is greater than 18 is a True case because this line of code has the indent. However, the execution of <code>print(“move on”)</code> will not be influenced by the if statement.\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"you can enter\n",
"move on\n"
]
}
],
"source": [
"# If statement example\n",
"\n",
"age = 19\n",
"#age = 18\n",
"\n",
"\n",
"#expression that can be true or false\n",
"if age > 18:\n",
" \n",
" #within an indent, we have the expression that is run if the condition is true\n",
" print(\"you can enter\" )\n",
"\n",
"#The statements after the if statement will run regardless if the condition is true or false \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<i>Try uncommenting the age variable</i>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is helpful to use the following diagram to illustrate the process. On the left side, we see what happens when the condition is <b>True</b>. The person enters the ACDC concert representing the code in the indent being executed; they then move on. On the right side, we see what happens when the condition is <b>False</b>; the person is not granted access, and the person moves on. In this case, the segment of code in the indent does not run, but the rest of the statements are run.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsIf.gif\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <code>else</code> statement runs a block of code if none of the conditions are **True** before this <code>else</code> statement. Let's use the ACDC concert analogy again. If the user is 17 they cannot go to the ACDC concert, but they can go to the Meatloaf concert.\n",
"The syntax of the <code>else</code> statement is similar as the syntax of the <code>if</code> statement, as <code>else :</code>. Notice that, there is no condition statement for <code>else</code>.\n",
"Try changing the values of <code>age</code> to see what happens:\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see Meat Loaf\n",
"move on\n"
]
}
],
"source": [
"# Else statement example\n",
"\n",
"age = 18\n",
"# age = 19\n",
"\n",
"if age > 18:\n",
" print(\"you can enter\" )\n",
"else:\n",
" print(\"go see Meat Loaf\" )\n",
" \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The process is demonstrated below, where each of the possibilities is illustrated on each side of the image. On the left is the case where the age is 17, we set the variable age to 17, and this corresponds to the individual attending the Meatloaf concert. The right portion shows what happens when the individual is over 18, in this case 19, and the individual is granted access to the concert.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsElse.gif\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <code>elif</code> statement, short for else if, allows us to check additional conditions if the condition statements before it are <b>False</b>. If the condition for the <code>elif</code> statement is <b>True</b>, the alternate expressions will be run. Consider the concert example, where if the individual is 18 they will go to the Pink Floyd concert instead of attending the ACDC or Meat-loaf concert. A person that is 18 years of age enters the area, and as they are not older than 18 they can not see ACDC, but since they are 18 years of age, they attend Pink Floyd. After seeing Pink Floyd, they move on. The syntax of the <code>elif</code> statement is similar in that we merely change the <code>if</code> in the <code>if</code> statement to <code>elif</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"go see Pink Floyd\n",
"move on\n"
]
}
],
"source": [
"# Elif statment example\n",
"\n",
"age = 18\n",
"\n",
"if age > 18:\n",
" print(\"you can enter\" )\n",
"elif age == 18:\n",
" print(\"go see Pink Floyd\")\n",
"else:\n",
" print(\"go see Meat Loaf\" )\n",
" \n",
"print(\"move on\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The three combinations are shown in the figure below. The left-most region shows what happens when the individual is less than 18 years of age. The central component shows when the individual is exactly 18. The rightmost shows when the individual is over 18.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src =\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsElif.gif\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Look at the following code:\n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is greater than 1980\n",
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1999\n",
"album_year = 1999\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
" \n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feel free to change <code>album_year</code> value to other values -- you'll see that the result changes!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that the code in the above <b>indented</b> block will only be executed if the results are <b>True</b>.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As before, we can add an <code>else</code> block to the <code>if</code> block. The code in the <code>else</code> block will only be executed if the result is <b>False</b>.\n",
"\n",
"<b>Syntax:</b>\n",
"\n",
"if (condition):\n",
"\\# do something\n",
"else:\n",
"\\# do something else\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If the condition in the <code>if</code> statement is <b>False</b>, the statement after the <code>else</code> block will execute. This is demonstrated in the figure:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsLogicMap.png\" width=\"650\" />\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"less than 1980\n",
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1980\n",
"#album_year = 1970\n",
"\n",
"if album_year > 1980:\n",
" print(\"Album year is greater than 1980\")\n",
"else:\n",
" print(\"less than 1980\")\n",
"\n",
"print('do something..')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feel free to change the <code>album_year</code> value to other values -- you'll see that the result changes based on it!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"logic\">Logical operators</h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you want to check more than one condition at once. For example, you might want to check if one condition and another condition are both **True**. Logical operators allow you to combine or modify conditions.\n",
"\n",
"<ul>\n",
" <li><code>and</code></li>\n",
" <li><code>or</code></li>\n",
" <li><code>not</code></li>\n",
"</ul>\n",
"\n",
"These operators are summarized for two variables using the following truth tables:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsTable.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <code>and</code> statement is only **True** when both conditions are true. The <code>or</code> statement is True if one condition, or both are **True**. The <code>not</code> statement outputs the opposite truth value.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see how to determine if an album was released after 1979 (1979 is not included) and before 1990 (1990 is not included). The time periods between 1980 and 1989 satisfy this condition. This is demonstrated in the figure below. The green on lines <strong>a</strong> and <strong>b</strong> represents periods where the statement is **True**. The green on line <strong>c</strong> represents where both conditions are **True**, this corresponds to where the green regions overlap.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsEgOne.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The block of code to perform this check is given by:\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year was in between 1980 and 1989\n",
"\n",
"Do Stuff..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1980\n",
"\n",
"if(album_year > 1979) and (album_year < 1990):\n",
" print (\"Album year was in between 1980 and 1989\")\n",
" \n",
"print(\"\")\n",
"print(\"Do Stuff..\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To determine if an album was released before 1980 (~ - 1979) or after 1989 (1990 - ~), an **or** statement can be used. Periods before 1980 (~ - 1979) or after 1989 (1990 - ~) satisfy this condition. This is demonstrated in the following figure, the color green in <strong>a</strong> and <strong>b</strong> represents periods where the statement is true. The color green in **c** represents where at least one of the conditions\n",
"are true.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%203/images/CondsEgTwo.png\" width=\"650\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The block of code to perform this check is given by:\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album was not made in the 1980's\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1990\n",
"\n",
"if(album_year < 1980) or (album_year > 1989):\n",
" print (\"Album was not made in the 1980's\")\n",
"else:\n",
" print(\"The Album was made in the 1980's \")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The <code>not</code> statement checks if the statement is false:\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Album year is not 1984\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"\n",
"if not (album_year == '1984'):\n",
" print (\"Album year is not 1984\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"quiz\">Quiz on Conditions</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write an if statement to determine if an album had a rating greater than 8. Test it using the rating for the album <b>“Back in Black”</b> that had a rating of 8.5. If the statement is true print \"This album is Amazing!\"\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This album is Amazing!\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"# Conditional block example \n",
"\n",
"Back_in_Black = 8.5 \n",
"\n",
"if (Back_in_Black > 8):\n",
" print (\"This album is Amazing!\")\n",
"\n",
"else: \n",
" print(\"Could have been better\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"rating = 8.5\n",
"if rating > 8:\n",
" print (\"This album is Amazing!\")\n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write an if-else statement that performs the following. If the rating is larger then eight print “this album is amazing”. If the rating is less than or equal to 8 print “this album is ok”.\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this album is amazing\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"Back_in_Black = 8.5\n",
"\n",
"if (Back_in_Black > 8):\n",
"\n",
" print (\"this album is amazing\")\n",
"\n",
"else:\n",
"\n",
" print(\"this album is ok\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"rating = 8.5\n",
"if rating > 8:\n",
" print (\"this album is amazing\")\n",
"else:\n",
" print (\"this album is ok\")\n",
" \n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write an if statement to determine if an album came out before 1980 or in the years: 1991 or 1993. If the condition is true print out the year the album came out.\n"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"\n",
"Album = 1982\n",
"\n",
"if (Album < 1980 or Album == 1991 or Album == 1993):\n",
" print (\"This album came out in\" + Album)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"album_year = 1979\n",
"\n",
"if album_year < 1980 or album_year == 1991 or album_year == 1993:\n",
" print (\"This album came out in year %d\" %album_year)\n",
" \n",
"```\n",
"\n",
"</details>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2>The last exercise!</h2>\n",
"<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Author\n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\" target=\"_blank\">Joseph Santarcangelo</a>\n",
"\n",
"## Other contributors\n",
"\n",
"<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n",
"\n",
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"|---|---|---|---|\n",
"| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n",
"| | | | |\n",
"| | | | |\n",
"\n",
"<hr/>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.13"
},
"metadata": {
"interpreter": {
"hash": "3abed311d39052675f1169a15d6f64f9745014cae486d494ad699e58cf37536b"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment