summaryrefslogtreecommitdiff
path: root/C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb')
-rw-r--r--C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb519
1 files changed, 519 insertions, 0 deletions
diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb
new file mode 100644
index 00000000..cd6b98e9
--- /dev/null
+++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter5.ipynb
@@ -0,0 +1,519 @@
+{
+ "metadata": {
+ "name": "chapter5"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5 - Making Decisions: if and switch Statements"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.1, page no. 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "outputs the results of several variable comparisons.\n",
+ "0 -> False 1 -> True\n",
+ "\"\"\"\n",
+ "\n",
+ "a = 4\n",
+ "b = 5\n",
+ "print a, \" > \", b, \" is \", (a > b)\n",
+ "print a, \" >= \", b, \" is \", (a >= b)\n",
+ "print a, \" == \", b, \" is \", (a == b)\n",
+ "print a, \" <= \", b, \" is \", (a <= b)\n",
+ "print a, \" < \", b, \" is \", (a < b)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4 > 5 is False\n",
+ "4 >= 5 is False\n",
+ "4 == 5 is False\n",
+ "4 <= 5 is True\n",
+ "4 < 5 is True\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.2, page no. 102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Addition operation\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter number of preregistered students: \",\n",
+ "total = int(raw_input())\n",
+ "print \"Enter number of students adding the course: \",\n",
+ "added = int(raw_input())\n",
+ "total = total + added\n",
+ "print \"Total number of students: \", total"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of preregistered students: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "30\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter number of students adding the course: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Total number of students: 33\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.3, page no. 104"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "tests if a whole number entered is even\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter a whole number: \",\n",
+ "num = int(raw_input())\n",
+ "if (num % 2 == 0):\n",
+ " print \"The number is even\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a whole number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The number is even\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.4, page no. 108"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "previous program modified to add else part for odd number\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter a whole number: \",\n",
+ "num = int(raw_input())\n",
+ "if (num % 2 == 0):\n",
+ " print \"The number is even\"\n",
+ "else:\n",
+ " print \"The number is odd\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a whole number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The number is odd\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.4, page no. 109"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "odd or even using inline condition (similar to conditional operator)\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter a whole number: \",\n",
+ "num = int(raw_input())\n",
+ "print \"The number is\", (\"even\" if num %2 == 0 else \"odd\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a whole number: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The number is even\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.6, page no. 112"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "determines grades based on test score\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter your test score: \",\n",
+ "testScore = int(raw_input())\n",
+ "if (testScore >= 90 ):\n",
+ " print \"Your grade is an A\"\n",
+ "elif (testScore >= 80 ):\n",
+ " print \"Your grade is a B\"\n",
+ "elif (testScore >= 70 ):\n",
+ " print \"Your grade is a C\"\n",
+ "elif (testScore >= 60 ):\n",
+ " print \"Your grade is a D\"\n",
+ "else:\n",
+ " print \"Your grade is an F\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your test score: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "70\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Your grade is a C\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.7, page no. 115"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "determines your average based on your grade\n",
+ "note: there is no switch statement in python\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Enter your grade: \",\n",
+ "grade = raw_input()\n",
+ "\n",
+ "if grade == 'A':\n",
+ " print \"Your average must be between 90 - 100\"\n",
+ "elif grade == 'B':\n",
+ " print \"Your average must be between 80 - 89\"\n",
+ "elif grade == 'C':\n",
+ " print \"Your average must be between 70 - 79\"\n",
+ "elif grade == 'D':\n",
+ " print \"Your average must be between 60 - 69\"\n",
+ "else:\n",
+ " print \"Your average must be below 60\" "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your grade: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "D\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Your average must be between 60 - 69\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.8, page no. 118"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "determines your average based on your grade with lower case alphabets also\n",
+ "note: there is no switch statement in python\n",
+ "\"\"\"\n",
+ "\n",
+ "\n",
+ "print \"Enter your grade: \",\n",
+ "grade = raw_input()\n",
+ "\n",
+ "if grade == 'A' or grade == 'a':\n",
+ " print \"Your average must be between 90 - 100\"\n",
+ "elif grade == 'B' or grade == 'b':\n",
+ " print \"Your average must be between 80 - 89\"\n",
+ "elif grade == 'C' or grade == 'c':\n",
+ " print \"Your average must be between 70 - 79\"\n",
+ "elif grade == 'D' or grade == 'd':\n",
+ " print \"Your average must be between 60 - 69\"\n",
+ "else:\n",
+ " print \"Your average must be below 60\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter your grade: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "d\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Your average must be between 60 - 69\n"
+ ]
+ }
+ ],
+ "prompt_number": 10
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "example 5.9, page no. 119"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "more example for switch (if else in python)\n",
+ "\"\"\"\n",
+ "\n",
+ "print \"Choose your car\"\n",
+ "print \"S for Standard\"\n",
+ "print \"L for Leather Seats\"\n",
+ "print \"D for Leather Seats + Chrome Wheels\"\n",
+ "choice = raw_input()\n",
+ "print \"Extra features purchased\"\n",
+ "if choice == 'D':\n",
+ " print \"Chrome wheels\\n\"\n",
+ "elif choice == 'L':\n",
+ " print \"Leather seats\"\n",
+ "else:\n",
+ " print \"None selected\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "StdinNotImplementedError",
+ "evalue": "raw_input was called, but this frontend does not support stdin.",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mStdinNotImplementedError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m/home/jovina/TBC/hardik/c++-demystified/<ipython-input-1-44ee42c69511>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"L for Leather Seats\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"D for Leather Seats + Chrome Wheels\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mchoice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mraw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Extra features purchased\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mchoice\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'D'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;32m/home/jovina/epd/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(prompt)\u001b[0m\n\u001b[0;32m 251\u001b[0m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprompt\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mident\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparent\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 252\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 253\u001b[1;33m \u001b[0mraw_input\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mprompt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m''\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_no_raw_input\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 254\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 255\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mpy3compat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPY3\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;32m/home/jovina/epd/lib/python2.7/site-packages/IPython/zmq/ipkernel.pyc\u001b[0m in \u001b[0;36m_no_raw_input\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 443\u001b[0m \"\"\"Raise StdinNotImplentedError if active frontend doesn't support\n\u001b[0;32m 444\u001b[0m stdin.\"\"\"\n\u001b[1;32m--> 445\u001b[1;33m raise StdinNotImplementedError(\"raw_input was called, but this \"\n\u001b[0m\u001b[0;32m 446\u001b[0m \"frontend does not support stdin.\") \n\u001b[0;32m 447\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mStdinNotImplementedError\u001b[0m: raw_input was called, but this frontend does not support stdin."
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Choose your car\n",
+ "S for Standard\n",
+ "L for Leather Seats\n",
+ "D for Leather Seats + Chrome Wheels\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file