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", "
" ] }, { "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", "

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

Get IBM Watson Studio free of charge!

\n", "

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