Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Created on Cognitive Class Labs
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"https://cocl.us/topNotebooksPython101Coursera\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/TopAd.png\" width=\"750\" align=\"center\">\n",
" </a>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Conditions in Python</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p><strong>Welcome!</strong> This notebook will teach you about the condition statements in the Python Programming Language. By the end of this lab, you'll know how to use the condition statements in Python, including operators, and branching.</p>"
]
},
{
"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",
" <p>\n",
" Estimated time needed: <strong>20 min</strong>\n",
" </p>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"cond\">Condition Statements</h2>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"comp\">Comparison Operators</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparison operations compare some value or operand and, based on a condition, they 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>"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"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."
]
},
{
"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>."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"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:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsGreater.gif\" width=\"650\" />"
]
},
{
"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:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"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>. "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"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 as 2 is in the green region. If we set <code>i</code> equal to 6, we get a **False** as the condition falls in the red region."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsIneq.gif\" width=\"650\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can apply the same methods on strings. For example, use an equality operator on two different strings. As the strings are not equal, we get a **False**."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 7,
"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."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use Inequality sign to compare the strings\n",
" \n",
"\"ACDC\" != \"Michael Jackson\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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": [
"For example, the ASCII code for <b>!</b> is 21, while the ASCII code for <b>+</b> is 43. Therefore <b>+</b> is larger than <b>!</b> as 43 is greater than 21."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly, the value for <b>A</b> is 101, and the value for <b>B</b> is 102 therefore:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"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:"
]
},
{
"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",
"'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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"branch\">Branching</h3>"
]
},
{
"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 than 18 they cannot enter the concert.\n",
"\n",
"Use the condition statements learned before as the conditions 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 tasks executed <code>print(“you can enter”)</code> only occurs 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."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"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",
"#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>"
]
},
{
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsIf.gif\" width=\"650\" />"
]
},
{
"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: "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsElse.gif\" width=\"650\" />"
]
},
{
"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. The person of 18 years of age enters the area, and as they are not older than 18 they can not see ACDC, but as 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 <code>if</code> statement to <code>elif</code>."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src =\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsElif.gif\" width=\"650\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Look at the following code:\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"do something..\n"
]
}
],
"source": [
"# Condition statement example\n",
"\n",
"album_year = 1983\n",
"album_year = 1970\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!"
]
},
{
"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>. "
]
},
{
"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",
"\n",
"<b>Syntax:</b> \n",
"\n",
"if (condition):\n",
" # do something\n",
"else:\n",
" # do something else"
]
},
{
"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: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsLogicMap.png\" width=\"650\" />"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"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 = 1983\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!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 id=\"logic\">Logical operators</h3>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Sometimes you want to check more than one condition at once. For example, you might want to check if one condition and another condition is **True**. Logical operators allow you to combine or modify conditions.\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: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsTable.png\" width=\"650\" />"
]
},
{
"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 is **True**. The <code>not</code> statement outputs the opposite truth value."
]
},
{
"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",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsEgOne.png\" width=\"650\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The block of code to perform this check is given by:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"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://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%203/Images/CondsEgTwo.png\" width=\"650\" />"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The block of code to perform this check is given by:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"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:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"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>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"quiz\">Quiz on Conditions</h2>"
]
},
{
"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!\""
]
},
{
"cell_type": "code",
"execution_count": 24,
"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",
"rating = 8.5\n",
"if rating>8:\n",
" print(\"This album is Amazing!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"\n",
"<!-- \n",
"rating = 8.5\n",
"if rating > 8:\n",
" print (\"This album is Amazing!\")\n",
" -->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"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”."
]
},
{
"cell_type": "code",
"execution_count": 25,
"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",
"rating = 8.5\n",
"if rating > 8:\n",
" print(\"This album is Amazing!\")\n",
"elif rating<=8:\n",
" print(\"This album is ok\")\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"\n",
"<!-- \n",
"rating = 8.5\n",
"if rating > 8:\n",
" print (\"this album is amazing\")\n",
"else:\n",
" print (\"this album is ok\")\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"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."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this album came out already\n"
]
}
],
"source": [
"# Write your code below and press Shift+Enter to execute\n",
"album_year = 1979\n",
"if album_year<1980 or album_year ==1991 or album_year==1993:\n",
" print(\"this album came out already\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"\n",
"<!-- \n",
"album_year = 1979\n",
"\n",
"if album_year < 1980 or album_year == 1991 or album_year == 1993:\n",
" print (\"this album came out already\")\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/\" target=\"_blank\">this article</a> to learn how to share your work.\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<h2>Get IBM Watson Studio free of charge!</h2>\n",
" <p><a href=\"https://cocl.us/bottemNotebooksPython101Coursera\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>About the Authors:</h3> \n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> is a Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other contributors: <a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<p>Copyright &copy; 2018 IBM Developer Skills Network. This notebook and its source code are released under the terms of the <a href=\"https://cognitiveclass.ai/mit-license/\">MIT License</a>.</p>"
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.