Instantly share code, notes, and snippets.

# SharonJoseph1409/PY0101EN-3-1-Conditions.ipynb Created Oct 9, 2019

Created on Cognitive Class Labs
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", " \n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Conditions in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Welcome! 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.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "
\n", "
\n", "

\n", " Estimated time needed: 20 min\n", "

\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Condition Statements

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Comparison Operators

" ] }, { "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", "
\n", "
• equal: ==
• \n", "
• not equal: !=
• \n", "
• greater than: >
• \n", "
• less than: <
• \n", "
• greater than or equal to: >=
• \n", "
• less than or equal to: <=
• \n", "

Branching

Logical operators

" ] }, { "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", "
\n", "
• and
• \n", "
• or
• \n", "
• not
• \n", "
\n", "\n", "These operators are summarized for two variables using the following truth tables: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The and statement is only **True** when both conditions are true. The or statement is true if one condition is **True**. The not 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 a and b represents periods where the statement is **True**. The green on line c represents where both conditions are **True**, this corresponds to where the green regions overlap. \n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " The block of code to perform this check is given by:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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 a and b 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": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The block of code to perform this check is given by:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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 not statement checks if the statement is false:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "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": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Quiz on Conditions

" ] }, { "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 “Back in Black” that had a rating of 8.5. If the statement is true print \"This album is Amazing!\"" ] }, { "cell_type": "code", "execution_count": 1, "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", "x=8.5\n", "if x>8:\n", " print(\"This album is amazing\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "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": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "this album is ok\n" ] } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "x=7.5\n", "if x>8:\n", " print(\"this album is amazing\")\n", "elif x<=8:\n", " print(\"this album is ok\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "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": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "# Write your code below and press Shift+Enter to execute\n", "album=1994\n", "if album<1980:\n", " print(\"Album not yet released\")\n", "elif album==1991 or album==1993:\n", " print(\"Album released in 90s\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The last exercise!

\n", "

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 this article to learn how to share your work.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Get IBM Watson Studio free of charge!

\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "