{ "metadata": { "name": "1 An overview of C++" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": "#Ex 1 , Page Number: 14#\\n#This program outputs a string, two integer values, and a double floating-point value. #\ni=10\nj=20\nd=99.101\nprint ('Here are some values : %d %d %2.3f'%(i,j,d))\n ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Here are some values : 10 20 99.101\n" } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 3 , Page Number: 16#\n\n#Variable declaration\ni=100\n\n#Result\nprint('Enter a value: 100')\nprint 'Here\\'s your number: ',i\n\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter a value: 100\nHere's your number: 100\n" } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 4 , Page Number 16#\n\n#Variable declaration\ni=10\nf=100.12\ns='Hello World'\n\n#Result\nprint 'Enter an integer,float and string: ',i,f,s\nprint 'Here\\'s your data: ',i,f,s \n ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": " Enter an integer,float and string: 10 100.12 Hello World\nHere's your data: 10 100.12 Hello World\n" } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 5 , Page Number 17#\n\n#Variable declaration\nch='X'\n\n#Result\nprint 'Enter keys,X to stop.'\n\n#while loop\nwhile ch!='X':\n if(ch!='X'): \n print ': ',ch\n else:\n break", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter keys,X to stop.\n" } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 1 , Page Number 19#\n\n#Variable declaration\na=199\n\nprint 'Enter number to be tested: ',a\n#if loop\nif a % 2 == 0:\n#Result\n print 'Number is even'\nelse:\n print 'Number is odd'\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter number to be tested: 199\nNumber is odd\n" } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 3 ,Page Number 25#\n\n#class declaration\nclass Myclass():\n def __init__(self,c,d):\n self.a = c\n self.b = d\nobj = Myclass(10,99)\nobj.a,obj.b\n\n#Result\nprint obj.a\nprint obj.b", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "10\n99\n" } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 2 , Page Number 30#\n\n#DefiningFunction\ndef sum(a,b):\n return a+b\na=10\nb=40\n\n#Result\nprint 'Enter two numbers: ',a,b\nprint 'Sum is: ',sum(a,b)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter two numbers: 10 40\nSum is: 50\n" } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 3 , Page Number 31#\n\n#Variable declaration\ni=5\nprint 'Enter number: ',i\nfact=1\nfor j in range(i):\n fact = fact * (j+1)\n \n#Result\nprint 'Factorial is ',fact\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter number: 5\nFactorial is 120\n" } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 4 ,Page Number 32#\n\noutcome = 'false'\n\n#if loop\nif(outcome):\n print 'true'\nelse:\n print 'false'", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "true\n" } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 2 ,Page Number 36#\n\n#Defining function\ndef get_date(day=None, month=None, year=None, as_string=None):\n\tif as_string:\n\t\tprint 'Date:', as_string\n\telse:\n\t\tif not (day and month and year):\n\t\t\traise Exception(\"Invalid Date arguments\")\n\t\tprint \"Date : %d/%d/%d\" % (month, day, year)\n\n\nif __name__ == '__main__':\n\tget_date(as_string=\"3/12/2013\")\n\tget_date(day=12, month=3, year=2013)\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Date: 3/12/2013\nDate : 3/12/2013\n" } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": "#Exercise 1.3 Q3 , Page Number 18#\n\n#Variable declaration\na=4\nb=8\nd=2\nprint 'Enter two numbers: ',a,b\nif a>b:\n min=b\nelse:\n min=a\nfor d in range(d,min):\n if a % d == 0 and b % d == 0: \n break\nif d == min:\n print 'No common denominators ' \nelse:\n print 'The lowest common denominator is ',d\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter two numbers: 4 8\nThe lowest common denominator is 2\n" } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": "#Ex 1 , Page Number 34#\n\nprint 'Absolute value of -10: ',abs(-10)\nprint 'Absolute value of -10L: ',abs(-10L)\nprint 'Absolute value of -10.01: ',abs(-10.01)\n\n#Defining function\ndef abs(n):\n print 'In integer abs() '\n \n\ndef abs(n):\n print 'In longs abs() '\n\ndef abs(n):\n print 'In double abs() '\n", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Absolute value of -10: 10\nAbsolute value of -10L: 10\nAbsolute value of -10.01: 10.01\n" } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": "#Excercise 1 , Page Number 18#\n\n#Variable declaration\nhours=3\nwage=2000\n\n#Result\nprint 'Enter hours worked: ',hours\n\nprint 'Enter wage per hour: ',wage\nprint 'Pay is: $',wage*hours", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter hours worked: 3\nEnter wage per hour: 2000\nPay is: $ 6000\n" } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": "#Exercise 2 , Page Number 18#\n\n#Variable declaration\nfeet = 5.0\n\n#If loop\nif(feet == 0.0):\n print 'Wrong inpt.'\nelse:\n print 'Enter feet :',feet\n #Result\n print feet*12,'inches'\n ", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Enter feet : 5.0\n60.0 inches\n" } ], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": "#Exercise 1.7 Q1 , Page Number 38#\n\nimport math\n\n#Result\nprint 'Square root of 90.34 is : ',math.sqrt(90.34)\nprint 'Square root of 90L is: ',math.sqrt(90L)\nprint 'Square root of 90 is: ',math.sqrt(90)\n\n#Defining functions\ndef sqrt(n):\n print 'computing integer root '\n \n\ndef sqrt(n):\n print 'computing long root '\n\ndef sqrt(n):\n print 'computing double root '", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "Square root of 90.34 is : 9.50473566176\nSquare root of 90L is: 9.48683298051\nSquare root of 90 is: 9.48683298051\n" } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": "#Example 3 , Page Number 36#\n\n#Defining functions\ndef f1(a=None,b=None,c=None):\n if a:\n print 'In f1',(a)\n else:\n if not (b and c):\n raise Exception(\"Invalid arguments\")\n print 'In f1',(b,c)\nif __name__ == '__main__':\n\tf1(a=10)\n\tf1(b=10, c=20)", "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": "In f1 10\nIn f1 (10, 20)\n" } ], "prompt_number": 16 } ], "metadata": {} } ] }