{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Relational Operators\n", "\n", "
    \n", "
  1. == : Equal to ?
  2. \n", "
  3. < : Lesser than ?
  4. \n", "
  5. > : Greater than ?
  6. \n", "
  7. <= : Lesser than or equal to ?
  8. \n", "
  9. >= : Greater than or equal to ?
  10. \n", "
  11. != : Not Equal ?
  12. \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Difference between Assignment & equal to operator" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "a = 5\n", "print(a==3)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(a!=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.1 Integer" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "a = 18\n", "b = 81\n", "print(a==a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.2 Float\n", "\n", "floating point numbers have only a limited precision, and calculations\n", "can introduce roundoff errors. You must take these inevitable roundoffs into account when\n", "comparing floating point numbers." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.5\n" ] } ], "source": [ "print(15.0/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1.3 String\n", "For strings to be equal, they must be of same length & must contain same sequence of characters" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "a = 'Abcd'\n", "b = 'abcd'\n", "print(a\n", "
  • Lexographic Order
  • \n", "
  • in Operator
  • \n", "
  • endswith()
  • \n", "
  • startswith()
  • \n", "
  • find()
  • \n", "
  • count()
  • \n", "
  • isspace()
  • \n", "
  • isalnum()
  • \n", "
  • isdigit()
  • \n", "" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "b = '123'\n", "print(b.isdigit())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Logical Operators (to combine relational operators)\n", "
      \n", "
    1. and
    2. \n", "
    3. or
    4. \n", "
    5. not
    6. \n", "
    " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.1 and" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes\n" ] } ], "source": [ "a = 10\n", "b = 10\n", "c = 20\n", "d = 20\n", "if (a==b and c==d):\n", " print('yes')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.2 or" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes\n" ] } ], "source": [ "a = 10\n", "b = 10\n", "c = 20\n", "d = 30\n", "if (a==b or c==d):\n", " print('yes')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2.3 not" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(not(('a'<'a') or ('b'<'b')))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Demorgan's law\n", "To simplify conditions with negations of and or or expressions, it’s a good idea to apply De Morgan’s law to move the negations to the innermost level.\n", "\n", "(notA or notB) is same as not(A and B). \n", "\n", "(notA and notB) is same as not(A or B).\n", "\n", "Eg. if (country != \"USA\" and state != \"AK\" and state != \"HI\") : shippingCharge = 20.00\n", "\n", "if not(country==\"USA\" or state==\"AK\" or state==\"HI\") : shippingCharge = 20.00" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Lets combine logical & relational operators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Compound Statements\n", "Statements with : at the end\n", "\n", "Consists header & a statment block\n", "\n", "The statement block is a group of one or more statements, all indented to the same colum\n", "\n", "Eg - for loop, if statements, etc\n", "\n", "```python\n", "for i in range(10):\n", " print(i)\n", " print(\"-----\")\n", "```\n", "\n", "### Nested Statement Blocks\n", "• Statement blocks can be nested inside other types of blocks (we will learn about more blocks later)\n", "\n", "• Statement blocks signal that one or more statements are part of a given compound statement\n", "\n", "```python\n", "for i in range(5):\n", " for j in range(5):\n", " print(j)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Flowchart\n", "![Flowchart](flowchart.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. If Else Statements\n", "Used when a computer needs to make decisions based on input or circumstances\n", "\n", "### Syntax\n", "\n", "```python\n", "if (Condition):\n", " Do This\n", "else:\n", " Do That\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.1 Floor Example\n", "Buildings often skip 13th floor, elevators should skip too" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13\n" ] } ], "source": [ "floor = 14\n", "actualFloor = floor\n", "if (floor > 13):\n", " actualFloor = floor - 1\n", "print(actualFloor)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.2 KiloByte Day\n", "The university bookstore has a Kilobyte Day sale every October 24 (10.24), giving an 8 percent discount on all computer accessory purchases if the price is less than 128 USD and a 16 percent discount if the price is at least 128 USD." ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "168.0\n" ] } ], "source": [ "price = 200\n", "if (price < 128):\n", " finalPrice = 0.92*price\n", "else:\n", " finalPrice = 0.84*price\n", "print(finalPrice)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.3 Ordering Drinks (Nested Branches Example)\n", "Ask the customer for their drink order, if customer orders wine, ask customer for ID.\n", "\n", "If customer's age is 21 & over, serve wine else politely explain the law" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Explain Law\n" ] } ], "source": [ "order = 'wine'\n", "ID = 19\n", "law = 'Explain Law'\n", "\n", "if (order == 'wine'):\n", " if (ID >= 21):\n", " print('serve')\n", " else:\n", " print(law)\n", "else:\n", " print('serve')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 6.4 Tax Example (Nested Branches Example)\n", "![Tax](tax.png)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20200.0\n" ] } ], "source": [ "status = 'S'\n", "income = 100000\n", "\n", "if (status == 'S'):\n", " if (income <= 32000):\n", " tax = 0.1*income\n", " else:\n", " tax = 3200 + (income-32000)*0.25\n", "else:\n", " if (income <= 64000):\n", " tax = 0.1*income\n", " else:\n", " tax = 6400 + (income-64000)*0.25\n", "\n", "print(tax)\n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Handtrace tax example\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6.5 What if you have more than 2 branches ?? Use Else if\n", "\n", "Richter Scale Example\n", "\n", "![Earthquake](earthquake.png)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Damage to poorly constructed buildings\n" ] } ], "source": [ "scale = 0\n", "\n", "if (scale > 0):\n", " if (scale<=4.5):\n", " print(\"Damage to poorly constructed buildings\")\n", "elif (scale>4.5 and scale<=6):\n", " print(\"Many building damaged\")\n", "elif(scale>6 and scale<=7):\n", " print(\"many buildings destroyed\")\n", "elif(scale>7 and scale<=8):\n", " print(\"Most structures fall\")\n", "else:\n", " print(\"End of the world\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.15+" } }, "nbformat": 4, "nbformat_minor": 2 }