{ "metadata": { "name": "", "signature": "sha256:275b458b34ae91067224e50705db107812d45fc58e5291ede57a7d2fe00a2167" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 14: Advanced and Specialized Topics" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 14.1, page no. 592" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import string\n", "import sys\n", "\n", "print \"Enter a character: \",\n", "ch = raw_input()\n", "\n", "if ch.isalnum():\n", " if ch.isdigit():\n", " print \"You entered the digit \", ch\n", " elif ch.islower():\n", " print \"You entered a lower case \", ch.upper()\n", " elif ch.isupper():\n", " print \"You entered a upper case \", ch.lower()\n", " elif ch in string.punctuation:\n", " print \"You entered a punctuation character \", ch\n", " else:\n", " print \"You entered \", ch, \"but I don't know what it is!\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a character: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "g\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " You entered a lower case G\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 14.2, page no. 596" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter a string of less than 100 characters: \",\n", "text = raw_input()\n", "\n", "print \"Enter the string sought: \",\n", "sought = raw_input()\n", "sought = sought.lower()\n", "count = 0\n", "\n", "sought_len = len(sought)\n", "\n", "for i in range(len(text)):\n", " substr = text[i:sought_len+i]\n", " if substr == sought:\n", " count += 1\n", "\n", "print \"First string entered: \", text\n", "print \"Second string entered: \", sought\n", "print \"The second string was found in first %d times\" %count\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a string of less than 100 characters: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Smith, where Jones had had, \"had\", had had \"had had\". Enter the string sought (less than 40 characters): Had\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter the string sought: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "had\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " First string entered: Smith, where Jones had had, \"had\", had had \"had had\". Enter the string sought (less than 40 characters): Had\n", "Second string entered: had\n", "The second string was found in first 7 times\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 14.3, page no. 606" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "cx = 1.0 + 3.0j\n", "cy = 1.0 - 4.0j\n", "\n", "sum = cx + cy\n", "diff = cx - cy\n", "product = cx * cy\n", "quotient = cy/cy\n", "conjugate = cx.conjugate()\n", "\n", "print \"Complex numbers are supported\"\n", "print \"Working with complex numbers: \"\n", "print \"Starting values: cx = %.2f%+.2fi cy = %.2f%+.2fi\" %(cx.real, cx.imag, cy.real, cy.imag)\n", "print \"The sum cx + cy = %.2f%+.2fi\" %(sum.real, sum.imag)\n", "print \"The difference cx - cy = %.2f%+.2fi\" %(diff.real, diff.imag)\n", "print \"The product cx * cy = %.2f%+.2fi\" %(product.real, product.imag)\n", "print \"The quotient cx / cy = %.2f%+.2fi\" %(quotient.real, quotient.imag)\n", "print \"The conjugate of cx = %.2f%+.2fi\" %(conjugate.real, conjugate.imag)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Complex numbers are supported\n", "Working with complex numbers: \n", "Starting values: cx = 1.00+3.00i cy = 1.00-4.00i\n", "The sum cx + cy = 2.00-1.00i\n", "The difference cx - cy = 0.00+7.00i\n", "The product cx * cy = 13.00-1.00i\n", "The quotient cx / cy = 1.00-0.00i\n", "The conjugate of cx = 1.00-3.00i\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 14.4, page no. 610" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "from threading import Thread\n", "\n", "def get_data(pdata):\n", " pd = pdata\n", " print \"The get_data thread received: data.a=%d and data.b=%d\" %(pd[0], pd[1])\n", " pd[0] *= 3\n", " print \"The get_data thread makes it: data.a=%d and data.b=%d \" %(pd[0], pd[1])\n", " \n", "def process_data(pdata):\n", " pd = pdata\n", " print \"The process_data thread received: data.a=%d and data.b=%d \"%(pd[0], pd[1])\n", " \n", "mydata = [123, 345]\n", "print \"Before starting the get_data thread: mydata.a=%d and mydata. b=%d\" %(mydata[0], mydata[1])\n", "print \"get_data thread started.\"\n", "t1 = Thread(target=get_data, args=(mydata,))\n", "t1.start()\n", "Thread.join(t1)\n", "print \"process_data thread started.\"\n", "t2 = Thread(target=process_data, args=(mydata,))\n", "t2.start()\n", "Thread.join(t2)\n", "print \"After both threads finish executing: mydata.a=%d and mydata. b=%d\" %(mydata[0], mydata[1])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Before starting the get_data thread: mydata.a=123 and mydata. b=345\n", "get_data thread started.\n", "The get_data thread received: data.a=123 and data.b=345\n", "The get_data thread makes it: data.a=369 and data.b=345 \n", "process_data thread started.\n", "The process_data thread received: data.a=369 and data.b=345 \n", "After both threads finish executing: mydata.a=369 and mydata. b=345\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 14.5, page no. 616" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "from threading import Thread\n", "import math\n", "import time\n", "\n", "thread_count = 5\n", "\n", "thread_id = []\n", "task = 0\n", "\n", "def execute_task():\n", " global task\n", " task += 1\n", " print \"Task %d started.\" %task\n", " time.sleep(1)\n", " for i in range(1000000):\n", " x = math.sqrt(3.1415926)\n", " print \"Task %d finished\" %task\n", " \n", "\n", "for i in range(thread_count):\n", " thread_id.append(Thread(target=execute_task))\n", " thread_id[i].start()\n", " \n", "for j in range(thread_count):\n", " Thread.join(thread_id[j])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Task 1 started.\n", "Task 2 started.\n", "Task 3 started.\n", "Task 4 started.\n", "Task 5 started.\n", "Task 5 finished\n", "Task 5 finished\n", "Task 5 finished\n", "Task 5 finished\n", "Task 5 finished\n" ] } ], "prompt_number": 9 } ], "metadata": {} } ] }