diff options
author | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
---|---|---|
committer | Jovina Dsouza | 2014-06-18 12:43:07 +0530 |
commit | 206d0358703aa05d5d7315900fe1d054c2817ddc (patch) | |
tree | f2403e29f3aded0caf7a2434ea50dd507f6545e2 /C++_Demystified:_A_Self-Teaching_Guide | |
parent | c6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff) | |
download | Python-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')
18 files changed, 9040 insertions, 0 deletions
diff --git a/C++_Demystified:_A_Self-Teaching_Guide/README.txt b/C++_Demystified:_A_Self-Teaching_Guide/README.txt new file mode 100644 index 00000000..c9358953 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/README.txt @@ -0,0 +1,10 @@ +Contributed By: Vaibhav Shah +Course: others +College/Institute/Organization: Indian Institute of Engineering Bombay +Department/Designation: System Administrator +Book Title: C++ Demystified: A Self-Teaching Guide +Author: Jeff Kent +Publisher: McGraw-Hill +Year of publication: 2004 +Isbn: 0-07-225370-3 +Edition: 1st
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter1.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter1.ipynb new file mode 100644 index 00000000..b19bede4 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter1.ipynb @@ -0,0 +1,53 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 1 - How a C++ Program Works" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 1.1, page no. 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Simple Hello World !\n", + "\"\"\"\n", + "\n", + "print \"Hello World!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World!\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter10.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter10.ipynb new file mode 100644 index 00000000..dd6f3a0f --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter10.ipynb @@ -0,0 +1,1295 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 10 - Arrays" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.1, page no. 206" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "defining an array\n", + "note: no need to specify size of an array in python, it exapands automatically.\n", + "\"\"\"\n", + "\n", + "testScore = [] #array defined" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.2, page no. 206" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "variable as size of array\n", + "note: this won't give an error as mentioned in book as it will take numTests as\n", + "an element of array instead of size of array, as there is no need of size of \n", + "array in python.\n", + "\"\"\"\n", + "\n", + "print \"Enter the number of test scores:\",\n", + "numTests = int(raw_input())\n", + "testScore = [numTests]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of test scores:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.3, page no. 207" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "variable as size of array\n", + "note: numTests will be taken as an element of array instead of size.\n", + "Also, there in no constant in Python, this program will be same as previous one\n", + "\"\"\"\n", + "\n", + "numTests = 3\n", + "print \"Enter the number of test scores:\",\n", + "numTests = int(raw_input())\n", + "testScore = [numTests]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of test scores:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.4, page no. 208" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "variable as size of array\n", + "note: numTests will be taken as an element of array instead of size.\n", + "Also, there in no constant in Python, this program will be same as previous one\n", + "\"\"\"\n", + "\n", + "numTests = 3\n", + "print \"Enter the number of test scores:\",\n", + "num = int(raw_input())\n", + "numTests = num\n", + "testScore = [numTests]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of test scores:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "78\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.5, page no. 213" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "intializing character array\n", + "note: '\\0' will have different result in python than in c++ \n", + "\"\"\"\n", + "\n", + "name = ['J', 'e', 'f', 'f', '\\0']\n", + "print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "['J', 'e', 'f', 'f', '\\x00']\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.5, page no. 213" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "intializing character array\n", + "note: output will be different than that in book.\n", + "\"\"\"\n", + "\n", + "name = ['J', 'e', 'f', 'f']\n", + "print name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "['J', 'e', 'f', 'f']\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.7, page no. 216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "assigning & displaying array values\n", + "\"\"\"\n", + "\n", + "testScore = []\n", + "print \"Enter test score #1: \",\n", + "testScore.append(int(raw_input()))\n", + "print \"Enter test score #2: \",\n", + "testScore.append(int(raw_input()))\n", + "print \"Enter test score #3: \",\n", + "testScore.append(int(raw_input()))\n", + "print \"Test score #1: \", testScore[0]\n", + "print \"Test score #2: \", testScore[1]\n", + "print \"Test score #3: \", testScore[2]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score #1: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "78\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score #2: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score #3: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "65\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score #1: 78\n", + "Test score #2: 88\n", + "Test score #3: 65\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.8, page no. 216" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using 3 separate variables instead of array in previous example\n", + "\"\"\"\n", + "\n", + "\n", + "print \"Enter test score #1: \",\n", + "testScore1 = int(raw_input())\n", + "print \"Enter test score #2: \",\n", + "testScore2 = int(raw_input())\n", + "print \"Enter test score #3: \",\n", + "testScore3 = int(raw_input())\n", + "print \"Test score #1: \", testScore1\n", + "print \"Test score #2: \", testScore2\n", + "print \"Test score #3: \", testScore3" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score #1: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "65\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score #2: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "67\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score #3: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "88\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score #1: 65\n", + "Test score #2: 67\n", + "Test score #3: 88\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.9, page no. 217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "looping for array\n", + "\"\"\"\n", + "\n", + "testScore = []\n", + "for i in range(3):\n", + " print \"Enter testScore #\", i+1, \": \",\n", + " testScore.append(int(raw_input()))\n", + "\n", + "for i in range(3):\n", + " print \"Test Score #\", i+1, \": \", testScore[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter testScore # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test Score # 1 : 3\n", + "Test Score # 2 : 4\n", + "Test Score # 3 : 5\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.10, page no. 217" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using constant for range of array\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = []\n", + "for i in range(MAX):\n", + " print \"Enter testScore #\", i+1, \": \",\n", + " testScore.append(int(raw_input()))\n", + "\n", + "for i in range(MAX):\n", + " print \"Test Score #\", i+1, \": \", testScore[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter testScore # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test Score # 1 : 5\n", + "Test Score # 2 : 6\n", + "Test Score # 3 : 7\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.11, page no. 218" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Common programming mistake for array\n", + "note: the problem mentioned in the textbook won't occur in python as we are not\n", + "specifying size of an array.\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = []\n", + "for i in range(MAX+1):\n", + " print \"Enter testScore #\", i+1, \": \",\n", + " testScore.append(int(raw_input()))\n", + "\n", + "for i in range(MAX+1):\n", + " print \"Test Score #\", i+1, \": \", testScore[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter testScore # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter testScore # 4 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test Score # 1 : 4\n", + "Test Score # 2 : 5\n", + "Test Score # 3 : 6\n", + "Test Score # 4 : 3\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.12, page no. 219" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "input element by index in array\n", + "note: if the array is not initialized then this method won't work. append() may\n", + "be used instead.\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "grades = [0,0,0]\n", + "\n", + "for i in range(MAX):\n", + " print \"Enter Garde #\", i+1, \": \",\n", + " grades[i] = int(raw_input())" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Garde # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Garde # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Garde # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.13, page no. 219" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "character array\n", + "\"\"\"\n", + "\n", + "name = ['J','e','f','f']\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is: Jeff\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.14, page no. 220" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cout Object with Numeric Arrays\n", + "note: here the list itself will be printed instead of base address(as mentioned\n", + "in textbook).\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "testScore = []\n", + "\n", + "for i in range(MAX):\n", + " print \"Enter test score #\", i + 1, \": \",\n", + " testScore.append(int(raw_input()))\n", + "print \"The test scores are: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The test scores are: [4, 5, 6]\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.15, page no. 221" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cin with numeric array\n", + "note: there won't be any error in python as it is with the case of c++. It will\n", + "just take testScore as a normal variable.\n", + "\"\"\"\n", + "\n", + "testScore = []\n", + "testScore = raw_input()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "45\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.16, page no. 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cin with character array\n", + "note: it will simply override the array and assign whatever value you enter to\n", + "the name variable unlike c++(as mentioned in c++)\n", + "\"\"\"\n", + "\n", + "name = ['J','e','f','f']\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is: Jeff\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.17, page no. 222" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "name as string\n", + "\"\"\"\n", + "\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is: Jeff\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.18, page no. 223" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "getline()\n", + "note: there is no getline() in python.\n", + "\"\"\"\n", + "\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is: Jeff\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.19, page no. 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "passing array as function argument\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = []\n", + "for i in range(MAX):\n", + " print \"Enter test score #\", i + 1, \": \",\n", + " testScore.append(int(raw_input()))\n", + "\n", + "for i in range(MAX):\n", + " print \"Test score #\", i + 1, \": \", testScore[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score # 1 : 4\n", + "Test score # 2 : 5\n", + "Test score # 3 : 6\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 10.20, page no. 225" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "MAX = 3\n", + "\n", + "def assignValues(tests, num):\n", + " for i in range(num):\n", + " print \"Enter test score #\", i + 1, \": \",\n", + " tests.append(int(raw_input()))\n", + "\n", + "def displayValues(scores, elems):\n", + " for i in range(elems):\n", + " print \"Test score #\", i + 1, \": \", scores[i]\n", + "\n", + "\n", + "testScore = []\n", + "assignValues(testScore, MAX)\n", + "displayValues(testScore, MAX)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score # 1 : 5\n", + "Test score # 2 : 6\n", + "Test score # 3 : 7\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter11.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter11.ipynb new file mode 100644 index 00000000..f1d28b76 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter11.ipynb @@ -0,0 +1,1120 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11 - What\u2019s the Address? Pointers" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.1, page no. 232" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "size of pointers\n", + "note: there is no concept of pointers in python\n", + "\"\"\"\n", + "\n", + "import sys\n", + "\n", + "iPtr = 0\n", + "fPtr = 0.0\n", + "cPtr = 'c'\n", + "\n", + "print sys.getsizeof(iPtr)\n", + "print sys.getsizeof(fPtr)\n", + "print sys.getsizeof(cPtr)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "24\n", + "24\n", + "38\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.2, page no. 233 " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "attempt to print the value of pointer.\n", + "note: there is no pointer in python, we will simply print out an integer value instead\n", + "\"\"\"\n", + "\n", + "iPtr = 0\n", + "print \"The value of iPtr is\", iPtr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of iPtr is 0\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.3, page no. 234" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Null pointers\n", + "\"\"\"\n", + "\n", + "iPtr = None\n", + "print \"The value of iPtr is \", iPtr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of iPtr is None\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.4, page no. 235" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "address of pointers\n", + "\"\"\"\n", + "\n", + "num = 5\n", + "iPtr = id(num)\n", + "print \"The address of x using id() is \", id(num)\n", + "print \"The address of x using iPtr is \", iPtr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of x using id() is 42658888\n", + "The address of x using iPtr is 42658888\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.5, page no. 236" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "dereferencing\n", + "output will deffer from that given in textbook\n", + "\"\"\"\n", + "\n", + "num = 5\n", + "iPtr = id(num)\n", + "print \"The value of num is \", num\n", + "num = 10\n", + "print \"The value of num after num = 10 is \", num\n", + "iPtr = 15\n", + "print \"The value of num after iPtr = 15 is \", num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of num is 5\n", + "The value of num after num = 10 is 10\n", + "The value of num after iPtr = 15 is 10\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.6, page no. 238" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Pointer as a Variable\n", + "note: output may differ from that given in textbook\n", + "\"\"\"\n", + "\n", + "num1 = 5\n", + "num2 = 14\n", + "iPtr = id(num1)\n", + "print \"The value of num1 is \", num1\n", + "iPtr = 2\n", + "print \"The value of num1 after iPtr = 2 is \", iPtr\n", + "iPtr = id(num2)\n", + "print \"The value of num2 is \", num2\n", + "iPtr /= 2\n", + "print \"The value of num after iPtr /= 2 is \", iPtr" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The value of num1 is 5\n", + "The value of num1 after iPtr = 2 is 2\n", + "The value of num2 is 14\n", + "The value of num after iPtr /= 2 is 21329336\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "exmple 11.7, page no. 239" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "array name as a constant pointer\n", + "note: we cannot use * operator to point in python as there are no pointers\n", + "python\n", + "\"\"\"\n", + "\n", + "testScore = [4, 7, 1]\n", + "print \"The address of the array using testScore is \", testScore\n", + "print \"The address of the first element of the array using &testScore[0] is \", id(testScore[0])\n", + "print \"The value of the first element of the array using *testScore is \", testScore[0]\n", + "print \"The value of the first element of the array using testScore[0] is \", testScore[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of the array using testScore is [4, 7, 1]\n", + "The address of the first element of the array using &testScore[0] is 42658912\n", + "The value of the first element of the array using *testScore is 4\n", + "The value of the first element of the array using testScore[0] is 4\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.8, page no. 240" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Pointer Arithmetic\n", + "the adress values will be different\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = [4, 7, 1]\n", + "for i in range(MAX):\n", + " print \"The address of index \", i, \" of the array is \", id(testScore[i])\n", + " print \"The value at index \", i, \" of the array is \", testScore[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of index 0 of the array is 42658912\n", + "The value at index 0 of the array is 4\n", + "The address of index 1 of the array is 42658840\n", + "The value at index 1 of the array is 7\n", + "The address of index 2 of the array is 42658984\n", + "The value at index 2 of the array is 1\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.9, page no. 241" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "previous program modified with a pointer to point to array\n", + "the adress values will be different\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = [4, 7, 1]\n", + "iPtr = testScore\n", + "for i in range(MAX):\n", + " print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n", + " print \"The value at index \", i, \" of the array is \", iPtr[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of index 0 of the array is 42658912\n", + "The value at index 0 of the array is 4\n", + "The address of index 1 of the array is 42658840\n", + "The value at index 1 of the array is 7\n", + "The address of index 2 of the array is 42658984\n", + "The value at index 2 of the array is 1\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.10, page no. 241" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Incrementing a Pointer\n", + "the method given in textbook is not possible in python.\n", + "We will do it in different way\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "testScore = [4, 7, 1]\n", + "iPtr = testScore\n", + "for i in range(MAX):\n", + " print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n", + " print \"The value at index \", i, \" of the array is \", iPtr[i]\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of index 0 of the array is 42658912\n", + "The value at index 0 of the array is 4\n", + "The address of index 1 of the array is 42658840\n", + "The value at index 1 of the array is 7\n", + "The address of index 2 of the array is 42658984\n", + "The value at index 2 of the array is 1\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.11, page no. 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "comparing address\n", + "\"\"\"\n", + "import sys\n", + "MAX = 3\n", + "\n", + "testScore = [4, 7, 1]\n", + "iPtr = testScore\n", + "i = 0\n", + "while (id(iPtr[MAX-1]) <= id(testScore[MAX-1])):\n", + " try:\n", + " print \"The address of index \", i, \" of the array is \", id(iPtr[i])\n", + " print \"The value at index \", i, \" of the array is \", iPtr[i]\n", + " i += 1\n", + " except IndexError:\n", + " print \"\\n\\nEnd of program\"\n", + " sys.exit()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "SystemExit", + "evalue": "", + "output_type": "pyerr", + "traceback": [ + "An exception has occurred, use %tb to see the full traceback.\n", + "\u001b[1;31mSystemExit\u001b[0m\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of index 0 of the array is 42658912\n", + "The value at index 0 of the array is 4\n", + "The address of index 1 of the array is 42658840\n", + "The value at index 1 of the array is 7\n", + "The address of index 2 of the array is 42658984\n", + "The value at index 2 of the array is 1\n", + "The address of index 3 of the array is \n", + "\n", + "End of program\n" + ] + }, + { + "output_type": "stream", + "stream": "stderr", + "text": [ + "To exit: use 'exit', 'quit', or Ctrl-D.\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.12, page no. 243" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "decrementing pointers\n", + "Note: not possible in python. Impletement in a different way\n", + "\"\"\"\n", + "import sys\n", + "MAX = 3\n", + "\n", + "testScore = [4, 7, 1]\n", + "iPtr = id(testScore[MAX-1])\n", + "i = MAX - 1\n", + "while (iPtr >= id(testScore[0])):\n", + " print \"The address of index \", i, \" of the array is \", iPtr\n", + " print \"The value at index \", i, \" of the array is \", testScore[i]\n", + " i -= 1\n", + " if i < 0:\n", + " break" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of index 2 of the array is 42658984\n", + "The value at index 2 of the array is 1\n", + "The address of index 1 of the array is 42658984\n", + "The value at index 1 of the array is 7\n", + "The address of index 0 of the array is 42658984\n", + "The value at index 0 of the array is 4\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.13, page no. 245" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "pointer as function\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "def assignValues(tests, num):\n", + " for i in range(num):\n", + " print \"Enter test score #\", i + 1, \": \",\n", + " tests.append(int(raw_input()))\n", + "\n", + "def displayValues(scores, elems):\n", + " for i in range(elems):\n", + " print \"Test score #\", i + 1, \": \", scores[i]\n", + "\n", + "testScore = []\n", + "assignValues(testScore, MAX)\n", + "displayValues(testScore, MAX)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score # 1 : 4\n", + "Test score # 2 : 5\n", + "Test score # 3 : 6\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.14, page no. 246" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "pointer as function\n", + "the program remains same in python\n", + "\"\"\"\n", + "\n", + "MAX = 3\n", + "\n", + "def assignValues(tests, num):\n", + " for i in range(num):\n", + " print \"Enter test score #\", i + 1, \": \",\n", + " tests.append(int(raw_input()))\n", + "\n", + "def displayValues(scores, elems):\n", + " for i in range(elems):\n", + " print \"Test score #\", i + 1, \": \", scores[i]\n", + "\n", + "testScore = []\n", + "assignValues(testScore, MAX)\n", + "displayValues(testScore, MAX)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score # 1 : 3\n", + "Test score # 2 : 4\n", + "Test score # 3 : 5\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.15, page no. 247" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Passing a Single Variable Using Pointer Notation\n", + "\"\"\"\n", + "\n", + "def doubleIt(x):\n", + " print \"The number to be doubled is \", x\n", + " x *= 2\n", + " print \"The number doubled in doubleIt is \", x\n", + "\n", + "\n", + "print \"Enter number: \",\n", + "num = int(raw_input())\n", + "doubleIt(num)\n", + "print \"The number doubled in main is \", num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number to be doubled is 4\n", + "The number doubled in doubleIt is 8\n", + "The number doubled in main is 4\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.16, page no. 248" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "modify previous program so it passes the variable to be doubled by address instead of by\n", + "reference\n", + "\n", + "note: in python it will remain same\n", + "\"\"\"\n", + "\n", + "def doubleIt(x):\n", + " print \"The number to be doubled is \", x\n", + " x *= 2\n", + " print \"The number doubled in doubleIt is \", x\n", + "\n", + "\n", + "print \"Enter number: \",\n", + "num = int(raw_input())\n", + "doubleIt(num)\n", + "print \"The number doubled in main is \", num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number to be doubled is 4\n", + "The number doubled in doubleIt is 8\n", + "The number doubled in main is 4\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.17, page no. 250" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Dynamic Memory Allocation\n", + "\"\"\"\n", + "\n", + "print \"Enter the number of test scores: \",\n", + "numTests = int(raw_input())\n", + "iPtr = []\n", + "for i in range(numTests):\n", + " print \"Enter test score #\", i + 1,\": \",\n", + " iPtr.append(int(raw_input()))\n", + "\n", + "for i in range(numTests):\n", + " print \"Test score #\", i + 1, \" is \", iPtr[i]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter the number of test scores: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 1 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 2 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter test score # 3 : " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Test score # 1 is 5\n", + "Test score # 2 is 6\n", + "Test score # 3 is 7\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.18, page no. 253" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Returning Pointers from Functions\n", + "\"\"\"\n", + "\n", + "str = \"Jeff Kent\"\n", + "print str" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff Kent\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.19, page no. 253" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Returning a Pointer to a Local Variable\n", + "\"\"\"\n", + "\n", + "def setName():\n", + " print \"Enter your name: \",\n", + " name = raw_input()\n", + " return name\n", + "\n", + "str = setName()\n", + "print str\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Jeff\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.20, page no. 254" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Returning a Pointer to a Static Local Variable\n", + "\"\"\"\n", + "\n", + "def setName():\n", + " print \"Enter your name: \",\n", + " name = raw_input()\n", + " return name\n", + "\n", + "str = setName()\n", + "print str\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Jeff\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 11.21, page no. 255" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Returning a Pointer to a Dynamically Created Variable\n", + "\"\"\"\n", + "\n", + "def setName():\n", + " print \"Enter your name: \",\n", + " name = raw_input()\n", + " return name\n", + "\n", + "str = setName()\n", + "print str\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Jeff\n" + ] + } + ], + "prompt_number": 21 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter12.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter12.ipynb new file mode 100644 index 00000000..20f1ab81 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter12.ipynb @@ -0,0 +1,610 @@ +{ + "metadata": { + "name": "chapter12" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 12 - Character, C-String, and C++ String Class\n", + "Functions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.1, page no. 262" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The \"Press Any Key to Continue\" Problem\n", + "\"\"\"\n", + "\n", + "ch = 'a'\n", + "\n", + "while(ch != 'Q' and ch != 'q'):\n", + " print \"Press Q or q to quit, any other key to continue: \",\n", + " ch = raw_input()\n", + " if(ch != 'Q' and ch != 'q'):\n", + " print \"You want to continue? \"\n", + " else:\n", + " print \"You quit\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "r\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You want to continue? \n", + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You quit\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.2, page no. 264" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cin.get function example.\n", + "we will use raw_input\n", + "\"\"\"\n", + "\n", + "ch = 'a'\n", + "\n", + "while(ch != 'Q' and ch != 'q'):\n", + " print \"Press Q or q to quit, any other key to continue: \",\n", + " ch = raw_input()\n", + " if(ch != 'Q' and ch != 'q'):\n", + " print \"You want to continue? \"\n", + " else:\n", + " print \"You quit\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You want to continue? \n", + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You quit\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.3, page no. 266" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cin.ignore function.\n", + "note: no such function in Python, using normal raw_input\n", + "\"\"\"\n", + "\n", + "ch = 'a'\n", + "\n", + "while(ch != 'Q' and ch != 'q'):\n", + " print \"Press Q or q to quit, any other key to continue: \",\n", + " ch = raw_input()\n", + " if(ch != 'Q' and ch != 'q'):\n", + " print \"You want to continue? \"\n", + " else:\n", + " print \"You quit\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You quit\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.4, page no. 268" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "ignoring \\n character\n", + "we will do it normally in Python as \\n is alread ignored.\n", + "\"\"\"\n", + "\n", + "ch = 'a'\n", + "\n", + "while(ch != 'Q' and ch != 'q'):\n", + " print \"Press Q or q to quit, any other key to continue: \",\n", + " ch = raw_input()\n", + " if(ch != 'Q' and ch != 'q'):\n", + " print \"You want to continue? \"\n", + " else:\n", + " print \"You quit\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "u\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You want to continue? \n", + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You quit\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.5, page no. 269" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Combining Use of cin, cin.get, and cin.getline\n", + "note: result differ from textbook\n", + "\"\"\"\n", + "\n", + "name = []\n", + "print \"Enter course number: \",\n", + "courseNum = int(raw_input())\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Course number is: \", courseNum\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter course number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "321\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Course number is: 321\n", + "Your name is: Jeff\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.6, page no. 271" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "cin.get, cin.getline contd.\n", + "\"\"\"\n", + "\n", + "name = []\n", + "print \"Enter course number: \",\n", + "courseNum = int(raw_input())\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Course number is: \", courseNum\n", + "print \"Your name is: \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter course number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "222\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Course number is: 222\n", + "Your name is: Jeff\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.7, page no. 273" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Useful Character Functions\n", + "toupper\n", + "\"\"\"\n", + "\n", + "ch = 'a'\n", + "while(ch != 'Q'):\n", + " print \"Press Q or q to quit, any other key to continue: \",\n", + " ch = raw_input()\n", + " ch = ch.upper()\n", + " if(ch != 'Q'):\n", + " print \"You want to continue? \"\n", + " else:\n", + " print \"You quit\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Press Q or q to quit, any other key to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You quit\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.8, page no. 280" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "string comparison\n", + "\"\"\"\n", + "\n", + "print \"Enter first string: \",\n", + "str1 = raw_input()\n", + "print \"Enter second string: \",\n", + "str2 = raw_input()\n", + "if (str1 == str2):\n", + " print \"The two Cstrings are equal\"\n", + "elif (str1 > str2):\n", + " print \"The first Cstring is larger\"\n", + "else:\n", + " print \"The second Cstring is larger\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first string: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter second string: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kent\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The second Cstring is larger\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 12.9, page no. 281" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "string to int\n", + "\"\"\"\n", + "\n", + "import sys\n", + "\n", + "print \"Enter an integer: \",\n", + "input = raw_input()\n", + "for x in input:\n", + " if (x == 0):\n", + " if (not(x.isdigit()) and x != '-'):\n", + " sys.exit()\n", + " else:\n", + " if(not(x.isdigit())):\n", + " sys.exit()\n", + "num = int(input)\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter an integer: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "567\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 567\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter13.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter13.ipynb new file mode 100644 index 00000000..46b00b70 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter13.ipynb @@ -0,0 +1,429 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 13: Persistent Data: File Input and Output" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.1, page no. 296" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "open a file\n", + "note: it will give an error\n", + "\"\"\"\n", + "\n", + "try:\n", + " if(fp):\n", + " print \"infile = \", fp\n", + " print \"0\"\n", + "except IOError:\n", + " print \"1\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'fp' is not defined", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-1-112e202a5944>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[1;32mif\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfp\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 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"infile = \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"0\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: name 'fp' is not defined" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.2, page no. 296" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "open file write mode\n", + "\"\"\"\n", + "\n", + "try:\n", + " fp = open(\"students.dat\", \"w\")\n", + " if(fp):\n", + " print \"infile = \", fp\n", + " print \"0\"\n", + "except IOError:\n", + " print \"1\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "infile = <open file 'students.dat', mode 'w' at 0x2d80a50>\n", + "0\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.3, page no. 299" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "writing to file\n", + "\"\"\"\n", + "\n", + "fp = open(\"students.dat\", \"w\")\n", + "print \"Writing to the file\"\n", + "print \"===================\"\n", + "print \"Enter class name: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.write(\"\\n\")\n", + "print \"Enter number of students: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Writing to the file\n", + "===================\n", + "Enter class name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number of students: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.4, page no. 301" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "reading from file\n", + "\"\"\"\n", + "\n", + "fp = open(\"students.dat\", \"w\")\n", + "print \"Writing to the file\"\n", + "print \"===================\"\n", + "print \"Enter class name: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.write(\"\\n\")\n", + "print \"Enter number of students: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.close()\n", + "fp = open(\"students.dat\", \"r\")\n", + "print \"Reading from the file\"\n", + "print \"=====================\"\n", + "lines = fp.readlines()\n", + "for line in lines:\n", + " print line\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Writing to the file\n", + "===================\n", + "Enter class name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number of students: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Reading from the file\n", + "=====================\n", + "C++\n", + "\n", + "32\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "example 13.5, page no. 303" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "reading from file using getline\n", + "\"\"\"\n", + "\n", + "fp = open(\"students.dat\", \"w\")\n", + "print \"Writing to the file\"\n", + "print \"===================\"\n", + "print \"Enter class name: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.write(\"\\n\")\n", + "print \"Enter number of students: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.close()\n", + "fp = open(\"students.dat\", \"r\")\n", + "print \"Reading from the file\"\n", + "print \"=====================\"\n", + "lines = fp.readlines()\n", + "for line in lines:\n", + " print line\n", + "fp.close()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Writing to the file\n", + "===================\n", + "Enter class name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number of students: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Reading from the file\n", + "=====================\n", + "C++\n", + "\n", + "32\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 13.6, page no. 305" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "looping through file\n", + "\"\"\"\n", + "\n", + "fp = open(\"students.dat\", \"w\")\n", + "print \"Writing to the file\"\n", + "print \"===================\"\n", + "print \"Enter class name: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.write(\"\\n\")\n", + "print \"Enter number of students: \",\n", + "data = raw_input()\n", + "fp.write(data)\n", + "fp.close()\n", + "fp = open(\"students.dat\", \"r\")\n", + "print \"Reading from the file\"\n", + "print \"=====================\"\n", + "while(fp.readline()):\n", + " print fp.readline()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Writing to the file\n", + "===================\n", + "Enter class name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "C++\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number of students: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "32\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Reading from the file\n", + "=====================\n", + "32\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb new file mode 100644 index 00000000..57cde338 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter14.ipynb @@ -0,0 +1,779 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 14: The Road Ahead: Structures and Classes" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.1, page no. 317" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "declaring structure\n", + "note: there is no concept of structure in python. We will use class instead\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.2, page no. 318" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "declares a three-element Person array, assigns values to the member\n", + "variables of each element, and then outputs their values\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + "\n", + "MAX = 3\n", + "p = []\n", + "\n", + "for x in range(MAX):\n", + " per = Person()\n", + " print \"Enter person's name: \",\n", + " per.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " per.height = int(raw_input())\n", + " p.append(per)\n", + "\n", + "print \"Outputting person data\\n\";\n", + "print \"======================\\n\";\n", + "for x in range(MAX):\n", + " print \"Person \", x + 1, \"'s name is \", p[x].name, \" and height is \", p[x].height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kent\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jefff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "40\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "\n", + "======================\n", + "\n", + "Person 1 's name is Jeff and height is 50\n", + "Person 2 's name is Kent and height is 50\n", + "Person 3 's name is Jefff and height is 40\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.3, page no. 320" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors & destructors\n", + "note: there are no destructors in Python\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + "\n", + "MAX = 3\n", + "p1 = Person()\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The person's name is and height is 0\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.4, page no. 322" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self):\n", + " self.name = \"no name assigned\"\n", + " self.height = -1\n", + "\n", + "MAX = 3\n", + "p1 = Person()\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The person's name is no name assigned and height is -1\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.5, page no. 323" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "constructors with arguments\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self, s=\"no name assigned\", h=-1):\n", + " self.name = s\n", + " self.height = h\n", + "\n", + "MAX = 3\n", + "print \"Enter a person's name: \",\n", + "s = raw_input()\n", + "print \"Enter height in inches: \",\n", + "h = int(raw_input())\n", + "p1 = Person(s, h)\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "39\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The person's name is Jeff and height is 39\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.6, page no. 324" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Separating the Constructor Prototype and Implementation\n", + "note: separation is not possible in Python we will do it the same way as previous\n", + "program\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " def __init__(self, s=\"no name assigned\", h=-1):\n", + " self.name = s\n", + " self.height = h\n", + "\n", + "MAX = 3\n", + "print \"Enter a person's name: \",\n", + "s = raw_input()\n", + "print \"Enter height in inches: \",\n", + "h = int(raw_input())\n", + "p1 = Person(s, h)\n", + "print \"The person's name is \", p1.name, \" and height is \", p1.height\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The person's name is Jeff and height is 50\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.7, page no. 325" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Passing Structures as Function Arguments\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self, pers):\n", + " print \"Person's name is \", pers.name, \" height is \", pers.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is Jeff height is 60\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.8, page no. 326" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "nesting structures\n", + "\"\"\"\n", + "\n", + "class Date:\n", + " month = 0\n", + " day = 0\n", + " year = 0\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = 0\n", + " bDay = Date()\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " print \"Enter birthdate\"\n", + " pers.bDay.month = int(raw_input(\"Month: \"))\n", + " pers.bDay.day = int(raw_input(\"Day: \"))\n", + " pers.bDay.year = int(raw_input(\"Year: \"))\n", + " def getValues(self, pers):\n", + " print \"Person's name: \", pers.name\n", + " print \"Person's height\", pers.height\n", + " print \"Person's birthday in mm/dd/yyyy format is: \", pers.bDay.month, pers.bDay.day, pers.bDay.year\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "59\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter birthdate\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Month: 20\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Day: 05\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Year: 1999\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Outputting person data\n", + "======================\n", + "Person's name: Jeff\n", + "Person's height 59\n", + "Person's birthday in mm/dd/yyyy format is: 20 5 1999\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.9, page no. 330" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "classes\n", + "note: there won't be any error unlike the situation mentioned in textbook\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self, pers):\n", + " print \"Person's name is \", pers.name, \" height is \", pers.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues(p1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "JEff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is JEff height is 50\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 14.10, page no. 331" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "private & public specifiers\n", + "note: there are no such specifiers in Python\n", + "\"\"\"\n", + "\n", + "class Person:\n", + " name = \"\"\n", + " height = \"\"\n", + " def setValues(self, pers):\n", + " print \"Enter person's name: \",\n", + " pers.name = raw_input()\n", + " print \"Enter height in inches: \",\n", + " pers.height = int(raw_input())\n", + " def getValues(self):\n", + " print \"Person's name is \", self.getName(), \" height is \", self.getHeight()\n", + " def getName(self):\n", + " return self.name\n", + " def getHeight(self):\n", + " return self.height\n", + "\n", + "p1 = Person()\n", + "p1.setValues(p1)\n", + "print \"Outputting person data\"\n", + "print \"======================\"\n", + "p1.getValues()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter person's name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "60\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Outputting person data\n", + "======================\n", + "Person's name is Jeff height is 60\n" + ] + } + ], + "prompt_number": 10 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter2.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter2.ipynb new file mode 100644 index 00000000..d25f5c31 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter2.ipynb @@ -0,0 +1,63 @@ +{ + "metadata": { + "name": "chapter2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 2 - Memory & Data Types" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 2.1, page no. 54" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Data Types\n", + "Note: there is no long double, or float double or double range or short.\n", + "Also, the size of data types my differ compared to c++\n", + "\"\"\"\n", + "\n", + "import sys \n", + "\n", + "print \"Size of Integer is: \" + str(sys.getsizeof(int()))\n", + "print \"Size of Float is: \" + str(sys.getsizeof(float()))\n", + "print \"Size of Long is: \" + str(sys.getsizeof(long()))\n", + "print \"Size of String is: \" + str(sys.getsizeof(str()))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Size of Integer is: 24\n", + "Size of Float is: 24\n", + "Size of Long is: 24\n", + "Size of String is: 37\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter3.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter3.ipynb new file mode 100644 index 00000000..d1b20dbb --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter3.ipynb @@ -0,0 +1,614 @@ +{ + "metadata": { + "name": "chapter3" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 3 - Variables" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.1, page no. 62" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Variable Declaration\n", + "\"\"\"\n", + "\n", + "testScore = 0 #will declare testScore as an integer" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.2, page no. 63" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Variable Declaration with error\n", + "\"\"\"\n", + "\n", + "testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 3, + "text": [ + "0" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.3, page no. 65" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Memory Address of variable\n", + "Note: address will be different from that given in the book\n", + "\"\"\"\n", + "\n", + "testScore = 0\n", + "print id(testScore)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "27819200\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.4, page no. 66" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Address and size of two variables\n", + "Note: the address and size will be diffent than the ones given in book\n", + "\"\"\"\n", + "import sys\n", + "\n", + "testScore = 0\n", + "myGPA = 0.0\n", + "\n", + "print \"The address of testScore is: \", id(testScore)\n", + "print \"The size of testScore is: \", sys.getsizeof(testScore)\n", + "print \"The address of myGPA is: \", id(myGPA)\n", + "print \"The size of myGPA is: \", sys.getsizeof(myGPA)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The address of testScore is: 27819200\n", + "The size of testScore is: 24\n", + "The address of myGPA is: 42871928\n", + "The size of myGPA is: 24\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.5, page no. 68" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "change in the value of a variable\n", + "\"\"\"\n", + "\n", + "testScore = 95;\n", + "print \"Your test score is: \", testScore\n", + "testScore = 75\n", + "print \"Your test score now is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your test score is: 95\n", + "Your test score now is: 75\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.6, page no. 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "A floating-point value, 77.83, is being assigned to an integer variable\n", + "Note: it will be printed as 77.83 unlike in the book, as Python implicitly\n", + "takes the data type according to the value assigned\n", + "\"\"\"\n", + "\n", + "testScore = 77.83;\n", + "print \"The test score is: \", testScore\n", + "print \"The test score is: \", int(testScore) # explicitly converting to integer" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "The test score is: 77.83\n", + "The test score is: 77\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.7 page no. 69" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Printing variable of short type but out of range.\n", + "Note: it will print 32768 as it is as there is no short data type in python\n", + "\"\"\"\n", + "\n", + "testScore = 32768;\n", + "print \"Your test score is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your test score is: 32768\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.8 page no. 70" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Printing variable of short type but out of range.\n", + "Note: it will print -32769 as it is as there is no short data type in python\n", + "\"\"\"\n", + "\n", + "testScore = -32769\n", + "print \"Your test score is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your test score is: -32769\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 3, + "metadata": {}, + "source": [ + "example 3.9, page no. 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Inputting a value (without prompt msg)\n", + "\"\"\"\n", + "\n", + "testScore = int(raw_input())\n", + "print \"Your test score is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.10, page no. 71" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Inputting a value (with prompt msg)\n", + "\"\"\"\n", + "\n", + "testScore = int(raw_input(\"Enter your test score: \"))\n", + "print \"Your test score is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your test score: 89\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your test score is: 89\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.11, page no. 73" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Inputting Values for Multiple Variables\n", + "\"\"\"\n", + "\n", + "print \"Enter your name: \",\n", + "myName = raw_input()\n", + "print \"Enter your weight in pounds: \",\n", + "myWeight = raw_input()\n", + "print \"Enter your height in inches: \",\n", + "myHeight = raw_input()\n", + "print \"Your name score is: \", myName\n", + "print \"Your weight in pounds is \", myWeight\n", + "print \"Your height in inches is \", myHeight" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hardik\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your weight in pounds: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name score is: Hardik\n", + "Your weight in pounds is 90\n", + "Your height in inches is 50\n" + ] + } + ], + "prompt_number": 13 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.12, page no. 74" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Inputting Values for Multiple Variables in one prompt (no possible in python, it\n", + "will be same like the previous program\n", + "\"\"\"\n", + "\n", + "print \"Enter your name: \",\n", + "myName = raw_input()\n", + "print \"Enter your weight in pounds: \",\n", + "myWeight = raw_input()\n", + "print \"Enter your height in inches: \",\n", + "myHeight = raw_input()\n", + "print \"Your name score is: \", myName\n", + "print \"Your weight in pounds is \", myWeight\n", + "print \"Your height in inches is \", myHeight" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hardik\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your weight in pounds: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "90\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your height in inches: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "50\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name score is: Hardik\n", + "Your weight in pounds is 90\n", + "Your height in inches is 50\n" + ] + } + ], + "prompt_number": 15 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.13, page no. 75" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "inputting a string (python will take the whole string and will not omit anything)\n", + "\"\"\"\n", + "\n", + "print \"Enter your name: \",\n", + "name = raw_input()\n", + "print \"Your name is \", name" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your name: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hardik\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is Hardik\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 3.14 page no. 76" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Printing variable of short type but out of range.\n", + "Note: it will print -32769 as it is as there is no short data type in python\n", + "\"\"\"\n", + "\n", + "testScore = 32769\n", + "print \"Your test score is: \", testScore" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your test score is: 32769\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter4.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter4.ipynb new file mode 100644 index 00000000..3293ca3b --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter4.ipynb @@ -0,0 +1,605 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 4 - Arithmetic Operators" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.1, page no. 82" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "following program has two integer variables, total and added\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": [ + "35\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: 38\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.2, page no. 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "addition operator also can be used with string operands\n", + "\"\"\"\n", + "\n", + "firstName = \"Jeff\"\n", + "lastName = \"Kent\"\n", + "print \"Your name is \", firstName + lastName" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Your name is JeffKent\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.3, page no. 84" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "program then uses the subtraction operator to update total.\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 \"How many students dropped? \",\n", + "dropped = int(raw_input())\n", + "total -= dropped\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": [ + " How many students dropped? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total number of students: 28\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "exampe 4.4, page no. 85" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The Multiplication Operator\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 \"How many students dropped? \",\n", + "dropped = int(raw_input())\n", + "total -= dropped\n", + "print \"Total number of students: \", total\n", + "print \"Total tuition owed: $\",((total + dropped) * 72)" + ], + "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": [ + " How many students dropped? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total number of students: 28\n", + "Total tuition owed: $ 2376\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.5, page no. 88" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The Division Operator\n", + "\"\"\"\n", + "\n", + "firstOp = 10\n", + "secondOp = 4\n", + "result = firstOp / secondOp\n", + "print firstOp, \" / \", secondOp, \" = \", result" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 / 4 = 2\n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.6, page no. 88" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "The Division Operator with the variables intialized with float values\n", + "\"\"\"\n", + "\n", + "firstOp = 10.0\n", + "secondOp = 4.0\n", + "result = firstOp / secondOp\n", + "print firstOp, \" / \", secondOp, \" = \", result" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10.0 / 4.0 = 2.5\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.7, page no. 89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "the first division example changed to make the result of integer division\n", + "result into float\n", + "\"\"\"\n", + "\n", + "firstOp = 10\n", + "secondOp = 4\n", + "result = float(firstOp) / float(secondOp) #converting both the variables to float\n", + "print firstOp, \" / \", secondOp, \" = \", result" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "10 / 4 = 2.5\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.8, page no. 89" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using all the operators together\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 \"How many students dropped? \",\n", + "dropped = int(raw_input())\n", + "total -= dropped\n", + "print \"Total number of students: \", total\n", + "tuition = (total + dropped) * 72\n", + "print \"Total tuition owed: $\",tuition\n", + "print \"Average tuition per enrolled student: $\", (float(tuition)/float(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": [ + " How many students dropped? " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total number of students: 28\n", + "Total tuition owed: $ 2376\n", + "Average tuition per enrolled student: $ 84.8571428571\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.9, page no. 91" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "program calculates the area of a circle based on a radius inputted by the user\n", + "\"\"\"\n", + "\n", + "print \"Enter radius of circle: \",\n", + "radius = float(raw_input())\n", + "area = 3.14159 * pow(radius, 2);\n", + "print \"The area is \", area" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter radius of circle: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The area is 113.09724\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 4.10, page no. 92" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "converting pennies to dollars, Dollars: 3, \n", + "Quarters, Dimes: 1, Nickels: 0, Pennies: 2\n", + "\"\"\"\n", + "\n", + "print \"Enter number of pennies to make change for: \",\n", + "total = int(raw_input())\n", + "dollars = total / 100\n", + "leftover = total % 100\n", + "quarters = leftover / 25\n", + "leftover %= 25\n", + "dimes = leftover / 10\n", + "leftover %= 10\n", + "nickels = leftover / 5\n", + "leftover %= 5\n", + "print \"Dollars: \", dollars\n", + "print \"Quarters: \", quarters\n", + "print \"Dimes: \", dimes\n", + "print \"Nickels: \", nickels\n", + "print \"Pennies: \", leftover" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number of pennies to make change for: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "387\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Dollars: 3\n", + "Quarters: 3\n", + "Dimes: 1\n", + "Nickels: 0\n", + "Pennies: 2\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file 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 diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter6.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter6.ipynb new file mode 100644 index 00000000..a00118a8 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter6.ipynb @@ -0,0 +1,325 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 6 - Nested if Statements and Logical Operators" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.1, page no. 125" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "use of nested if statements\n", + "\"\"\"\n", + "\n", + "print \"Enter your age: \",\n", + "age = int(raw_input())\n", + "print \"Are you a citizen (Y/N): \",\n", + "choice = raw_input()\n", + "if (choice == 'Y'):\n", + " citizen = True\n", + "else:\n", + " citizen = False\n", + "if (age >= 18):\n", + " if citizen:\n", + " print \"You are eligible to vote\"\n", + " else:\n", + " print \"You are not eligible to vote\"\n", + "else:\n", + " print \"You are not eligible to vote\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your age: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "65\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Are you a citizen (Y/N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You are eligible to vote\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.2, page no. 127" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "use of nested if statements\n", + "\"\"\"\n", + "\n", + "print \"Enter your age: \",\n", + "age = int(raw_input())\n", + "if (age > 12):\n", + " if (age >= 65):\n", + " print \"Admission is free\"\n", + " else:\n", + " print \"You have to pay\"\n", + "else:\n", + " print \"Admission is free\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your age: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "11\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Admission is free\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.3, page no. 129" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "use of the logical And operator\n", + "\"\"\"\n", + "\n", + "print \"Enter your age: \",\n", + "age = int(raw_input())\n", + "print \"Are you a citizen (Y/N): \",\n", + "choice = raw_input()\n", + "if (choice == 'Y'):\n", + " citizen = True\n", + "else:\n", + " citizen = False\n", + "if (age >= 18 and citizen == True):\n", + " print \"You are eligible to vote\"\n", + "else:\n", + " print \"You are not eligible to vote\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter your age: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Are you a citizen (Y/N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You are not eligible to vote\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.4, page no. 131" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "use of the logical Or operator\n", + "\"\"\"\n", + "\n", + "print \"Enter your age: \",\n", + "age = int(raw_input())\n", + "if (age <= 12 or age >= 65):\n", + " print \"Admission is free\"\n", + "else:\n", + " print \"You have to pay\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your age: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You have to pay\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 6.5, page no. 132" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "use of the logical Not operator\n", + "\"\"\"\n", + "\n", + "print \"Enter your age: \",\n", + "age = int(raw_input())\n", + "if not ((age > 12 and age < 65)):\n", + " print \"Admission is free\"\n", + "else:\n", + " print \"You have to pay\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter your age: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "22\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You have to pay\n" + ] + } + ], + "prompt_number": 9 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter7.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter7.ipynb new file mode 100644 index 00000000..6076d617 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter7.ipynb @@ -0,0 +1,979 @@ +{ + "metadata": { + "name": "chapter7" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 7 - The For Loop" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.1, page no. 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "unary operators\n", + "\"\"\"\n", + "\n", + "num = 2\n", + "num += 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.2, page no. 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "increment operator\n", + "note: there is no increment/decrement operator in python we will use += instead\n", + "\"\"\"\n", + "\n", + "num = 2\n", + "num += 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.3, page no. 142" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "increment operator\n", + "note: there is no increment/decrement operator in python we will use += instead\n", + "\"\"\"\n", + "\n", + "num = 2\n", + "print num\n", + "num += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.4, page no. 143" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "unary operator (decrement)\n", + "\"\"\"\n", + "\n", + "num = 2\n", + "num -= 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.5, page no. 143" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "prefix & postfix\n", + "note: there is no increment/decrement operator in python we will use -= instead\n", + "this program illustrates n--\n", + "\"\"\"\n", + "\n", + "\n", + "num = 2\n", + "num -= 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.6, page no. 144" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "prefix & postfix\n", + "note: there is no increment/decrement operator in python we will use -= instead\n", + "this program illustrates --n\n", + "\"\"\"\n", + "\n", + "\n", + "num = 2\n", + "num -= 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.7, page no. 146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "output the numbers between 1 and 10\n", + "\"\"\"\n", + "\n", + "num = 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num\n", + "num += 1\n", + "print num" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.8, page no. 146" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using a for loop (1 to 10)\n", + "\"\"\"\n", + "\n", + "for num in range(1, 11):\n", + " print num\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.9, page no. 147" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using a for loop (1 to 100)\n", + "\"\"\"\n", + "\n", + "for num in range(1, 101):\n", + " print num," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.10, page no. 149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using a for loop\n", + "note: the method given in textbook is not possible in python, we will use a while\n", + "loop instead\n", + "\"\"\"\n", + "\n", + "num = 1\n", + "while num <= 10:\n", + " print num,\n", + " num += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10\n" + ] + } + ], + "prompt_number": 14 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.11, page no. 149" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "infinite loop\n", + "\"\"\"\n", + "\n", + "num = 1\n", + "while num <= 10:\n", + " print num," + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.12, page no. 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "factorial\n", + "\"\"\"\n", + "\n", + "total = 1;\n", + "print \"Enter a number: \",\n", + "num = int(raw_input())\n", + "print \"The factorial of \", num, \" is \",\n", + "for counter in range(1, num+1):\n", + " total *= counter\n", + "print total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The factorial of 5 is 120\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.13, page no. 150" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Breaking Out of a Loop\n", + "\"\"\"\n", + "\n", + "secret = 3\n", + "print \"Guess a number between 1 and 10\"\n", + "print \"You have 3 tries\"\n", + "for counter in range(1,4):\n", + " print \"Enter the number now: \",\n", + " num = int(raw_input())\n", + " if (num == secret):\n", + " print \"You guessed the secret number!\"\n", + " break\n", + "print \"Program over\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Guess a number between 1 and 10\n", + "You have 3 tries\n", + "Enter the number now: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the number now: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter the number now: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Program over\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.14, page no. 151" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "logical && (And) operator is an alternative to using the break\n", + "keyword\n", + "\n", + "note: and cannot be used in python with for loop, we will use if condition\n", + "\"\"\"\n", + "\n", + "secret = 3\n", + "print \"Guess a number between 1 and 10\\n\"\n", + "print \"You have 3 tries\\n\"\n", + "keepgoing = True\n", + "for counter in range(1, 4):\n", + " if keepgoing == True:\n", + " print \"Enter the number now: \",\n", + " num = int(raw_input())\n", + " if (num == secret):\n", + " print \"You guessed the secret number!\"\n", + " keepgoing = False\n", + " else:\n", + " break\n", + "print \"Program over\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Guess a number between 1 and 10\n", + "\n", + "You have 3 tries\n", + "\n", + "Enter the number now: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You guessed the secret number!\n", + "Program over\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.15, page no. 152" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "missing condition\n", + "Instead, the break keyword inside the if/else structure substitutes for that condition\n", + "note: we cannot write an infinte loop in python as given in book, we will use\n", + "while loop\n", + "\"\"\"\n", + "\n", + "num = 1\n", + "while True:\n", + " if (num > 10):\n", + " break\n", + " else:\n", + " print num, \" \",\n", + " num += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10 \n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.16, page no 153" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "continue keyword\n", + "\"\"\"\n", + "\n", + "total = 0\n", + "print \"How many items do you want to buy: \",\n", + "num = int(raw_input())\n", + "for counter in range(1, num+1):\n", + " if (counter % 13 == 0):\n", + " continue\n", + " total += 3\n", + "print \"Total for \", num, \" items is $\", total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many items do you want to buy: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total for 5 items is $ 15\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.17, page no. 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using logical Not instead of continue keyword\n", + "\"\"\"\n", + "\n", + "total = 0\n", + "print \"How many items do you want to buy: \",\n", + "num = int(raw_input())\n", + "keepgoing = True\n", + "for counter in range(1, num+1):\n", + " if (not(counter % 13 == 0 )):\n", + " total += 3\n", + "print \"Total for \", num, \" items is $\", total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many items do you want to buy: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total for 6 items is $ 18\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.18, page no. 154" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "Nesting for loop\n", + "\"\"\"\n", + "\n", + "for x in range(1, 6):\n", + " for y in range(1, 11):\n", + " print \"X\",\n", + " print \"\\n\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X X X X X X X X X X \n", + "\n", + "X X X X X X X X X X \n", + "\n", + "X X X X X X X X X X \n", + "\n", + "X X X X X X X X X X \n", + "\n", + "X X X X X X X X X X \n", + "\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 7.19, page no. 155" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "more nested for loop\n", + "\"\"\"\n", + "\n", + "print \"Enter number of salespersons: \",\n", + "persons = int(raw_input())\n", + "print \"Enter number of sales per salesperson: \",\n", + "numSales = int(raw_input())\n", + "for x in range(1, persons+1):\n", + " total = 0\n", + " for y in range(1, numSales+1):\n", + " print \"Enter sale \", y, \" for salesperson \", x, \": \"\n", + " sale = int(raw_input())\n", + " total += sale\n", + " average = float(total/numSales)\n", + " print \"Average sales for salesperson #\", x, \" is \", average" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number of salespersons: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number of sales per salesperson: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter sale 1 for salesperson 1 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter sale 2 for salesperson 1 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average sales for salesperson # 1 is 1.0\n", + "Enter sale 1 for salesperson 2 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter sale 2 for salesperson 2 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average sales for salesperson # 2 is 3.0\n", + "Enter sale 1 for salesperson 3 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter sale 2 for salesperson 3 : \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "6\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Average sales for salesperson # 3 is 5.0\n" + ] + } + ], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter8.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter8.ipynb new file mode 100644 index 00000000..c3bc8e16 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter8.ipynb @@ -0,0 +1,603 @@ +{ + "metadata": { + "name": "chapter8" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 8 - While and Do While Loops" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.1, page no. 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "for loop that was used earlier to show the difference between while loop & for\n", + "\"\"\"\n", + "\n", + "for num in range(1, 11):\n", + " print num," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.2, page no. 161" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "using while loop for the same\n", + "\"\"\"\n", + "\n", + "num = 1\n", + "while num <=10:\n", + " print num,\n", + " num += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1 2 3 4 5 6 7 8 9 10\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.3, page no. 162" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "updation in the condition itself.\n", + "it is not possible in python, we will use normal while loop\n", + "\"\"\"\n", + "\n", + "num = 0\n", + "while (num <= 10):\n", + " print num,\n", + " num += 1" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "0 1 2 3 4 5 6 7 8 9 10\n" + ] + } + ], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.4, page no. 163" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "asks the user to enter a positive number,\n", + "and in a loop continues that request until the user does so\n", + "\"\"\"\n", + "\n", + "print \"Enter a positive number: \",\n", + "num = int(raw_input())\n", + "while (num <= 0):\n", + " print \"Number must be positive; please retry: \",\n", + " num = int(raw_input())\n", + "print \"The number you entered is \", num, \" \"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number you entered is 4 \n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.5, page no. 164" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "modification of the program uses the break keyword to provide the user with the\n", + "option of quitting the data entry\n", + "\"\"\"\n", + "\n", + "print \"Enter a positive number: \",\n", + "num = int(raw_input())\n", + "while (num <= 0):\n", + " print \"Number must be positive; try again (Y/N): \",\n", + " choice = raw_input()\n", + " if (choice == 'Y'):\n", + " print \"Enter number: \",\n", + " num = int(raw_input())\n", + " else:\n", + " break\n", + "print \"The number you entered is \", num, \" \"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "0\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Number must be positive; try again (Y/N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Y\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "7\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number you entered is 7 \n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.6, page no. 165" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "flags\n", + "note: there is a mistake in the textbook, it should be choice == 'Y' instead of\n", + "choice != 'Y'\n", + "\"\"\"\n", + "\n", + "quit = False\n", + "print \"Enter a positive number: \",\n", + "num = int(raw_input())\n", + "while (num <= 0 and quit == False):\n", + " print \"Number must be positive; try again (Y/N): \",\n", + " choice = raw_input()\n", + " if (choice is 'Y'):\n", + " print \"Enter number: \",\n", + " num = int(raw_input())\n", + " else:\n", + " quit = True\n", + "if (quit == False):\n", + " print \"The number you entered is \", num, \" \"\n", + "else:\n", + " print \"You did not enter a positive number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number you entered is 3 \n" + ] + } + ], + "prompt_number": 6 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.7, page no. 168" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "while(true)\n", + "\"\"\"\n", + "\n", + "quit = False\n", + "while(True):\n", + " print \"Enter a positive number: \",\n", + " num = int(raw_input())\n", + " if (num > 0):\n", + " break\n", + " else:\n", + " print \"Number must be positive; try again (Y/N): \",\n", + " choice = raw_input()\n", + " if (choice != 'Y'):\n", + " quit = True\n", + " break\n", + "if (quit == False):\n", + " print \"The number you entered is \", num, \" \"\n", + "else:\n", + " print \"You did not enter a positive number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "-1\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Number must be positive; try again (Y/N): " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "N\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You did not enter a positive number\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.8, page no. 169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "continue keyword\n", + "\"\"\"\n", + "\n", + "counter = 0\n", + "total = 0\n", + "print \"How many items do you want to buy: \",\n", + "num = int(raw_input())\n", + "while(counter < num):\n", + " counter += 1\n", + " if (counter % 13 == 0):\n", + " continue\n", + " total += 3\n", + "print \"Total for \", num, \" items is $\", total" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.9, page no. 169" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "logical ! (Not)\n", + "operator as an alternative to using the continue keyword\n", + "\"\"\"\n", + "\n", + "counter = 0\n", + "total = 0\n", + "print \"How many items do you want to buy: \",\n", + "num = int(raw_input())\n", + "keepgoing = True\n", + "while(counter < num):\n", + " counter += 1\n", + " if (not(counter % 13 == 0)):\n", + " total += 3;\n", + "print \"Total for \", num, \" items is $\", total" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "How many items do you want to buy: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Total for 4 items is $ 12\n" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.10, page no. 170" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "program using nested while loops\n", + "\"\"\"\n", + "\n", + "x = 0\n", + "while(x < 5):\n", + " x += 1\n", + " y = 0\n", + " while(y < 5):\n", + " y += 1\n", + " print \"X\",\n", + " print '\\n'" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n", + "X X X X X \n", + "\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 8.11, page no. 171" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "do while loop\n", + "note: there is no do while loop in python, we will use while loop instead\n", + "\"\"\"\n", + "\n", + "quit = False\n", + "print \"Enter a positive number: \",\n", + "num = int(raw_input())\n", + "while (num <= 0 and quit == False):\n", + " print \"Number must be positive; try again (Y/N): \",\n", + " choice = raw_input()\n", + " if (choice is 'Y'):\n", + " print \"Enter number: \",\n", + " num = int(raw_input())\n", + " else:\n", + " quit = True\n", + "if (quit == False):\n", + " print \"The number you entered is \", num, \" \"\n", + "else:\n", + " print \"You did not enter a positive number\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a positive number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number you entered is 3 \n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb b/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb new file mode 100644 index 00000000..4918a533 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/chapter9.ipynb @@ -0,0 +1,1036 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 9 - Functions" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.1, page no. 179" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "terminology of a function\n", + "note: there is no need of main in python, defining here just for sake of understanding\n", + "\"\"\"\n", + "\n", + "def main(): #defining a function \n", + " print \"Hello World!\"\n", + "\n", + "main()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello World!\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.2, page no. 180" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "defining a function\n", + "\"\"\"\n", + "\n", + "def printMessage(): # defining function\n", + " print \"Hello world!\"\n", + "\n", + "printMessage() # calling the function" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello world!\n" + ] + } + ], + "prompt_number": 4 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.3, page no. 181" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "prototyping\n", + "note: there is no need of prototyping in Python. In fact, you have to define a function before you call it in Python.\n", + "\"\"\"\n", + "\n", + "printMessage();\n", + "\n", + "def printMessage():\n", + " print \"Hello world!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello world!\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.4, page no. 182" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "prototyping\n", + "note: the way it is given in textbook is not possible in python. We have to \n", + "define a funciton first in order to call it\n", + "\"\"\"\n", + "\n", + "def printMessage():\n", + " print \"Hello world!\"\n", + "\n", + "printMessage()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello world!\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.5, page no. 184" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "local variables\n", + "note: program will give an error saying local variable time is not defined\n", + "\"\"\"\n", + "\n", + "def printMessage():\n", + " times += 1\n", + " print \"This function called \", times, \" times\"\n", + "\n", + "\n", + "times = 0\n", + "choice = '1'\n", + "while(choice != 'Q'):\n", + " print \"Enter Q to quit, any other character to continue: \",\n", + " choice = raw_input()\n", + " if (choice == 'Q'):\n", + " print \"Input stopped\"\n", + " else:\n", + " printMessage()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "e\n" + ] + }, + { + "ename": "UnboundLocalError", + "evalue": "local variable 'times' referenced before assignment", + "output_type": "pyerr", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m<ipython-input-8-42e131eb489c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 17\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Input stopped\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 18\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[0mprintMessage\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m<ipython-input-8-42e131eb489c>\u001b[0m in \u001b[0;36mprintMessage\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprintMessage\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mtimes\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"This function called \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\" times\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'times' referenced before assignment" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.6, page no. 185" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "local variables\n", + "\"\"\"\n", + "\n", + "def printMessage():\n", + " times = 0\n", + " times += 1\n", + " print \"This function called \", times, \" times\"\n", + "\n", + "\n", + "choice = '1'\n", + "while(choice != 'Q'):\n", + " print \"Enter Q to quit, any other character to continue: \",\n", + " choice = raw_input()\n", + " if (choice == 'Q'):\n", + " print \"Input stopped\"\n", + " else:\n", + " printMessage()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 1 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 1 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Input stopped\n" + ] + } + ], + "prompt_number": 9 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.7, page no. 186" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "global variables\n", + "\"\"\"\n", + "\n", + "times = 0\n", + "def printMessage():\n", + " global times\n", + " times += 1\n", + " print \"This function called \", times, \" times\"\n", + "\n", + "\n", + "choice = '1'\n", + "while(choice != 'Q'):\n", + " print \"Enter Q to quit, any other character to continue: \",\n", + " choice = raw_input()\n", + " if (choice == 'Q'):\n", + " print \"Input stopped\"\n", + " else:\n", + " printMessage()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 1 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 2 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "u\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 3 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Input stopped\n" + ] + } + ], + "prompt_number": 11 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.8, page no. 189" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "static local variables\n", + "note: there is no static keyword in python so we will use it as global variable\n", + "only.\n", + "\"\"\"\n", + "\n", + "times = 0\n", + "def printMessage():\n", + " global times\n", + " times += 1\n", + " print \"This function called \", times, \" times\"\n", + "\n", + "\n", + "choice = '1'\n", + "while(choice != 'Q'):\n", + " print \"Enter Q to quit, any other character to continue: \",\n", + " choice = raw_input()\n", + " if (choice == 'Q'):\n", + " print \"Input stopped\"\n", + " else:\n", + " printMessage()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "w\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 1 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "r\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 2 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "t\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " This function called 3 times\n", + "Enter Q to quit, any other character to continue: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Q\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Input stopped\n" + ] + } + ], + "prompt_number": 12 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.9, page no. 190" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "sending info. to a function\n", + "\"\"\"\n", + "\n", + "def printMessage():\n", + " print \"You inputted \", str\n", + "\n", + "print \"Enter a string: \",\n", + "str = raw_input()\n", + "printMessage()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You inputted Jeff\n" + ] + } + ], + "prompt_number": 22 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.10, page no. 191" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "passing argument by value\n", + "\"\"\"\n", + "\n", + "def printMessage(s):\n", + " print \"You inputted \", s\n", + "\n", + "print \"Enter a string: \",\n", + "str = raw_input()\n", + "printMessage(str)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a string: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " You inputted Jeff\n" + ] + } + ], + "prompt_number": 21 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.11, page no. 193" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "multiple function arguments\n", + "\"\"\"\n", + "\n", + "def printMessage(firstName, lastName):\n", + " print \"Your name is \", firstName, \" \", lastName\n", + "\n", + "print \"Enter first name:\",\n", + "name1 = raw_input()\n", + "print \"Enter last name:\",\n", + "name2 = raw_input()\n", + "printMessage(name1, name2)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first name:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter last name:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Kent\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is Jeff Kent\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.12, page no. 194" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "multiple function arguments\n", + "note: in Python even if the arguments are reversed, it won't give an error as \n", + "we do not have to specipy the data type explicitly. The meaning or the context\n", + "of the output may change.\n", + "\"\"\"\n", + "\n", + "def printMessage(thename, theage):\n", + " print \"Your name is \", thename, \" and your age is\", theage\n", + "\n", + "print \"Enter name:\",\n", + "name = raw_input()\n", + "print \"Enter age:\",\n", + "age = int(raw_input())\n", + "printMessage(name, age)\n", + "printMessage(age, name)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter name:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Jeff\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter age:" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Your name is Jeff and your age is 34\n", + "Your name is 34 and your age is Jeff\n" + ] + } + ], + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.13, page no. 195" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "passing argument by reference\n", + "\"\"\"\n", + "\n", + "def doubleIt(x):\n", + " print \"The number to be doubled is \", x\n", + " x *= 2\n", + " print \"The number doubled in doubleIt is \", x\n", + "\n", + "print \"Enter number: \",\n", + "num = int(raw_input())\n", + "doubleIt(num)\n", + "print \"The number doubled outside the function is \", num," + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "34\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number to be doubled is 34\n", + "The number doubled in doubleIt is 68\n", + "The number doubled outside the function is 34\n" + ] + } + ], + "prompt_number": 18 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.14, page no. 196" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "pass by reference (address)\n", + "note: & sign is used in c++ to access a varibale by its address. There is no\n", + "such way or it is not a good practice to implement it. We will do the same in \n", + "some other way(there are multiple ways to achieve the same result)\n", + "\"\"\"\n", + "\n", + "li = [0]\n", + "def doubleIt():\n", + " print \"The number to be doubled is \", li[0]\n", + " li[0] *= 2\n", + " print \"The number doubled in doubleIt is \", li[0]\n", + " \n", + "print \"Enter number: \",\n", + "num = int(raw_input())\n", + "li[0] = num\n", + "doubleIt()\n", + "print \"The number doubled outside the function is \", li[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " The number to be doubled is 5\n", + "The number doubled in doubleIt is 10\n", + "The number doubled outside the function is 10\n" + ] + } + ], + "prompt_number": 19 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.15, page no. 197" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "function to add two numbers\n", + "note: we are doing it in a differnt way then the one given in book\n", + "\"\"\"\n", + "\n", + "z = []\n", + "def addNumbers (a, b):\n", + " z.append(a + b)\n", + "\n", + "\n", + "print \"Enter first number: \",\n", + "firstNum = int(raw_input())\n", + "print \"Enter second number: \",\n", + "secondNum = int(raw_input())\n", + "addNumbers(firstNum, secondNum)\n", + "print firstNum, \" + \", secondNum, \" = \", z[0]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "3\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter second number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 3 + 4 = 7\n" + ] + } + ], + "prompt_number": 23 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "example 9.16, page no. 199" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "\"\"\"\n", + "returning value from function\n", + "\"\"\"\n", + "\n", + "def addNumbers(x, y):\n", + " return x + y\n", + "\n", + "print \"Enter first number: \",\n", + "firstNum = int(raw_input())\n", + "print \"Enter second number: \",\n", + "secondNum = int(raw_input())\n", + "sum = addNumbers(firstNum, secondNum)\n", + "print firstNum, \" + \", secondNum, \" = \", sum\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "4\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " Enter second number: " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "5\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + " 4 + 5 = 9\n" + ] + } + ], + "prompt_number": 24 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file diff --git a/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-1.png b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-1.png Binary files differnew file mode 100644 index 00000000..683964bf --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-1.png diff --git a/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-2.png b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-2.png Binary files differnew file mode 100644 index 00000000..58c58b09 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-2.png diff --git a/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-3.png b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-3.png Binary files differnew file mode 100644 index 00000000..a8ecc8f5 --- /dev/null +++ b/C++_Demystified:_A_Self-Teaching_Guide/screenshots/vaibhav-1-3.png |