{ "metadata": { "name": "", "signature": "sha256:13095677f4efa0909d9aeb25aa7b8b57bea864fd1d9a0a797f9e06000e52dc18" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Chapter 3: The Basic Data Types

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

Example 3.1, Page Number: 35

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def func():\n", " x=-199 #local to func\n", " print x #displays -199 \n", " \n", "x=10 #local to main\n", "\n", "func()\n", "print x #displays 10\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "-199\n", "10\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.2, Page Number: 37

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def func1():\n", " global count\n", " print \"count: \",count #access global count\n", " func2()\n", "\n", "def func2():\n", " for count in range(3): #this is local variable\n", " print '.',\n", "\n", " \n", "count=None #global variables\n", "\n", "for i in range(10):\n", " count=i*2\n", " func1()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "count: 0\n", ". . . count: 2\n", ". . . count: 4\n", ". . . count: 6\n", ". . . count: 8\n", ". . . count: 10\n", ". . . count: 12\n", ". . . count: 14\n", ". . . count: 16\n", ". . . count: 18\n", ". . .\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.3, Page Number: 40

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "from ctypes import *\n", "\n", "#Variable declaration\n", "j=c_uint(60000)\n", "i=c_int(60000)\n", "\n", "#Result\n", "print i.value,j.value" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "60000 60000\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.4, Page Number: 41

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for letter in xrange(ord('Z'),ord('A')-1,-1):\n", " print chr(letter),\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Z Y X W V U T S R Q P O N M L K J I H G F E D C B A\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.5, Page Number: 44

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "print \"\\n\\\\\\b\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\\\b\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.6, Page Number: 45

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def total(x):\n", " sum=0\n", " for i in xrange(1,x+1):\n", " sum=sum+i\n", " for count in range(10):\n", " print '-',\n", " print \"The current sum is\",sum\n", " \n", "print \"Computing summation of 5.\"\n", "total(5)\n", "\n", "print \"Computing summation of 6.\"\n", "total(6)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Computing summation of 5.\n", "- - - - - - - - - - The current sum is 1\n", "- - - - - - - - - - The current sum is 3\n", "- - - - - - - - - - The current sum is 6\n", "- - - - - - - - - - The current sum is 10\n", "- - - - - - - - - - The current sum is 15\n", "Computing summation of 6.\n", "- - - - - - - - - - The current sum is 1\n", "- - - - - - - - - - The current sum is 3\n", "- - - - - - - - - - The current sum is 6\n", "- - - - - - - - - - The current sum is 10\n", "- - - - - - - - - - The current sum is 15\n", "- - - - - - - - - - The current sum is 21\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.7, Page Number: 47

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "#Variable declaration\n", "x=10\n", "y=3\n", "\n", "print x/y #will display 3\n", "print x%y #will display1, the remainder\n", "\n", "x=1\n", "y=2\n", "\n", "#Result\n", "print x/y,x%y #will display 0 1" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "1\n", "0 1\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.8, Page Number: 51

" ] }, { "cell_type": "code", "collapsed": false, "input": [ " \n", "def xor(a,b):\n", " return (a or b)and(not(a and b))\n", "\n", "#User-input\n", "print \"Enter P(0 or 1):\"\n", "p=1 \n", "print \"Enter Q(0 or 1):\"\n", "q=0\n", "\n", "#Result\n", "print \"P AND Q:\",(p and q)\n", "print \"P OR Q:\",(p or q)\n", "print \"P XOR Q:\",xor(p,q)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter P(0 or 1):\n", "Enter Q(0 or 1):\n", "P AND Q: 0\n", "P OR Q: 1\n", "P XOR Q: True\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.9, Page Number: 54

" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for i in xrange(1,100+1):\n", " print i,\"/ 2 is:\",float(i)/2\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 / 2 is: 0.5\n", "2 / 2 is: 1.0\n", "3 / 2 is: 1.5\n", "4 / 2 is: 2.0\n", "5 / 2 is: 2.5\n", "6 / 2 is: 3.0\n", "7 / 2 is: 3.5\n", "8 / 2 is: 4.0\n", "9 / 2 is: 4.5\n", "10 / 2 is: 5.0\n", "11 / 2 is: 5.5\n", "12 / 2 is: 6.0\n", "13 / 2 is: 6.5\n", "14 / 2 is: 7.0\n", "15 / 2 is: 7.5\n", "16 / 2 is: 8.0\n", "17 / 2 is: 8.5\n", "18 / 2 is: 9.0\n", "19 / 2 is: 9.5\n", "20 / 2 is: 10.0\n", "21 / 2 is: 10.5\n", "22 / 2 is: 11.0\n", "23 / 2 is: 11.5\n", "24 / 2 is: 12.0\n", "25 / 2 is: 12.5\n", "26 / 2 is: 13.0\n", "27 / 2 is: 13.5\n", "28 / 2 is: 14.0\n", "29 / 2 is: 14.5\n", "30 / 2 is: 15.0\n", "31 / 2 is: 15.5\n", "32 / 2 is: 16.0\n", "33 / 2 is: 16.5\n", "34 / 2 is: 17.0\n", "35 / 2 is: 17.5\n", "36 / 2 is: 18.0\n", "37 / 2 is: 18.5\n", "38 / 2 is: 19.0\n", "39 / 2 is: 19.5\n", "40 / 2 is: 20.0\n", "41 / 2 is: 20.5\n", "42 / 2 is: 21.0\n", "43 / 2 is: 21.5\n", "44 / 2 is: 22.0\n", "45 / 2 is: 22.5\n", "46 / 2 is: 23.0\n", "47 / 2 is: 23.5\n", "48 / 2 is: 24.0\n", "49 / 2 is: 24.5\n", "50 / 2 is: 25.0\n", "51 / 2 is: 25.5\n", "52 / 2 is: 26.0\n", "53 / 2 is: 26.5\n", "54 / 2 is: 27.0\n", "55 / 2 is: 27.5\n", "56 / 2 is: 28.0\n", "57 / 2 is: 28.5\n", "58 / 2 is: 29.0\n", "59 / 2 is: 29.5\n", "60 / 2 is: 30.0\n", "61 / 2 is: 30.5\n", "62 / 2 is: 31.0\n", "63 / 2 is: 31.5\n", "64 / 2 is: 32.0\n", "65 / 2 is: 32.5\n", "66 / 2 is: 33.0\n", "67 / 2 is: 33.5\n", "68 / 2 is: 34.0\n", "69 / 2 is: 34.5\n", "70 / 2 is: 35.0\n", "71 / 2 is: 35.5\n", "72 / 2 is: 36.0\n", "73 / 2 is: 36.5\n", "74 / 2 is: 37.0\n", "75 / 2 is: 37.5\n", "76 / 2 is: 38.0\n", "77 / 2 is: 38.5\n", "78 / 2 is: 39.0\n", "79 / 2 is: 39.5\n", "80 / 2 is: 40.0\n", "81 / 2 is: 40.5\n", "82 / 2 is: 41.0\n", "83 / 2 is: 41.5\n", "84 / 2 is: 42.0\n", "85 / 2 is: 42.5\n", "86 / 2 is: 43.0\n", "87 / 2 is: 43.5\n", "88 / 2 is: 44.0\n", "89 / 2 is: 44.5\n", "90 / 2 is: 45.0\n", "91 / 2 is: 45.5\n", "92 / 2 is: 46.0\n", "93 / 2 is: 46.5\n", "94 / 2 is: 47.0\n", "95 / 2 is: 47.5\n", "96 / 2 is: 48.0\n", "97 / 2 is: 48.5\n", "98 / 2 is: 49.0\n", "99 / 2 is: 49.5\n", "100 / 2 is: 50.0\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }