{ "metadata": { "name": "", "signature": "sha256:708386728a0ddd9fd0fa033f85042eb5d454abc98a33806d4ca4cc0500a010f0" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5 : Strings" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.1, Page No. 97" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Menu\"\n", "print \"=====\"\n", "print \"A. Order\"\n", "print \"B. Invoice\"\n", "print \"C. Warehouse\"\n", "print \"D. Finance\"\n", "cSel = raw_input(\"Select: \")\n", "if cSel=='A' :\n", " print \"You Selected Order\"\n", "elif cSel=='B' :\n", " print \"You Selected Invoice\"\n", "elif cSel=='C':\n", " print \"You Selected Warehouse\"\n", "elif cSel=='D' :\n", " print \"You Selected Finance\"\n", "else :\n", " print \"Erroneous Choice\"\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Menu\n", "=====\n", "A. Order\n", "B. Invoice\n", "C. Warehouse\n", "D. Finance\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Select: A\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You Selected Order\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.2, Page No.99" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "cSal = 'A'\n", "while cSal!='X':\n", " print \"Menu\"\n", " print \"=====\"\n", " print \"A. Order\"\n", " print \"B. Invoice\"\n", " print \"C. Warehouse\"\n", " print \"D. Finance\"\n", " print \"X. Exit\"\n", " cSel = raw_input(\"Select: \")\n", "\n", " \n", " if cSel=='A' :\n", " print \"You Selected Order\"\n", " elif cSel=='B' :\n", " print \"You Selected Invoice\"\n", " elif cSel=='C':\n", " print \"You Selected Warehouse\"\n", " elif cSel=='D' :\n", " print \"You Selected Finance\"\n", " elif cSel=='X':\n", " sys.exit()\n", " else :\n", " print \"Erroneous Choice\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Menu\n", "=====\n", "A. Order\n", "B. Invoice\n", "C. Warehouse\n", "D. Finance\n", "X. Exit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Select: A\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You Selected Order\n", "Menu\n", "=====\n", "A. Order\n", "B. Invoice\n", "C. Warehouse\n", "D. Finance\n", "X. Exit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Select: B\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "You Selected Invoice\n", "Menu\n", "=====\n", "A. Order\n", "B. Invoice\n", "C. Warehouse\n", "D. Finance\n", "X. Exit\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Select: X\n" ] }, { "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": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D.\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.3, Page No. 102" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 'X' \n", "blank = ' '\n", "for i in range(10, -1, -1):\n", " for j in range(i):\n", " print blank,\n", " print x,\n", " for j in range(10-i):\n", " print x, x,\n", " print ''\n", "for i in range(2):\n", " for j in range(10):\n", " print blank,\n", " print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " X \n", " X X X \n", " X X X X X \n", " X X X X X X X \n", " X X X X X X X X X \n", " X X X X X X X X X X X \n", " X X X X X X X X X X X X X \n", " X X X X X X X X X X X X X X X \n", " X X X X X X X X X X X X X X X X X \n", " X X X X X X X X X X X X X X X X X X X \n", "X X X X X X X X X X X X X X X X X X X X X \n", " X\n", " X\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.4, Page No. 105" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "cName = raw_input(\"Enter your name: \")\n", "print \"Your name is \" , cName\n", "print \"Your name is \" , len(cName), \" Characters long\"\n", "print \"Your entire string is \" , sys.getsizeof(cName), \" characters long\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: John Smith\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your name is John Smith\n", "Your name is 10 Characters long\n", "Your entire string is 31 characters long\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.5, Page No. 106" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cName=range(30)\n", "cName = raw_input(\"Enter your name in lower case: \")\n", "iLen = len(cName)\n", "for ltr in cName:\n", " print ltr.lower(),\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name in lower case: ABCD\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "a b c d\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.6, Page No. 107" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cName = raw_input(\"Enter your name: \")\n", "iLen = len(cName)\n", "cInt = []\n", "cInt.append(cName[0])\n", "for i in range(1, iLen):\n", " if cName[i] == ' ':\n", " cInt.append(cName[i+1])\n", "print \"Your Initials are \", ''.join(cInt)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your name: Kjell Backman\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Your Initials are KB\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.7, Page No. 109" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cName1 = raw_input(\"Enter a Name: \")\n", "cName2 = raw_input(\"Enter another Name: \")\n", "if cName1 > cName2 :\n", " print cName1 , cName2\n", "else :\n", " print cName2 , cName1" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a Name: abcd\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter another Name: abcx\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "abcx abcd\n" ] } ], "prompt_number": 33 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.8, Page No. 110" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter 5 names: \"\n", "names = []\n", "for i in range(5):\n", " names.append(raw_input(\"name \" + str(i+1) + \": \"))\n", "names.sort()\n", "for i in range(5):\n", " print names[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter 5 names: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name 1: hardik\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name 2: Parth\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name 3: Nikunj\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name 4: Vaibhav\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "name 5: Chirag\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Chirag\n", "Nikunj\n", "Parth\n", "Vaibhav\n", "hardik\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.9, Page No.113" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cName = raw_input(\"Enter Your Name: \")\n", "for i in range(29):\n", " if cName[i] == ' ':\n", " break\n", "cFirst=cName[0:i]\n", "print cFirst\n", "cSur=cName[i+1:]\n", "print cSur\n", "cSur=cSur+\" \"\n", "cSur=cSur+cFirst\n", "print cSur\n", "cSur=cSur[::-1]\n", "print cSur" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Name: John Smith\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "John\n", "Smith\n", "Smith John\n", "nhoJ htimS\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.10, Page No.115" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cEncrypt = []\n", "cName = []\n", "cName = raw_input(\"Enter Your Name: \")\n", "iLen = len(cName)\n", "for i in range(iLen):\n", " asc=ord(cName[i])\n", " asc=asc+1\n", " cEncrypt.append(chr(asc))\n", "print ''.join(cEncrypt)" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter Your Name: JohnSmith\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "KpioTnjui\n" ] } ], "prompt_number": 24 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.11, Page No. 116" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "iLen = random.randint(0,100)%3+5\n", "cPw=[]\n", "for i in range(iLen):\n", " val=random.randint(0,122)%26+65\n", " cPw.append(chr(val))\n", "print ''.join(cPw)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "ZXZUOY\n" ] } ], "prompt_number": 37 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 5.12, Page No. 119" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cText=[]\n", "cEncrypt=[]\n", "cTemp1=[]\n", "cText=raw_input(\"\\nWrite a text in upper case: \")\n", "iLen= len(cText)\n", "for i in range(iLen):\n", " print cText[i]\n", " car=ord(cText[i])\n", " cEncrypt.append(car)\n", "print ''.join(str(cEncrypt))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Write a text in upper case: hellow\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "h\n", "e\n", "l\n", "l\n", "o\n", "w\n", "[104, 101, 108, 108, 111, 119]\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "cKey=\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n", "cKey=list(cKey)\n", "cEncrypt=range(50)\n", "for i in range(26):\n", " j=random.randint(0,100)%26\n", " cTemp=cKey[i]\n", " cKey[i]=cKey[j]\n", " cKey[j]=cTemp\n", "cKey=\"\".join(map(str,cKey))\n", "cText=raw_input(\"\\nWrite a text in upper case: \")\n", "iLen= len(cText)\n", "for i in range(iLen):\n", " cEncrypt[i]=cKey[ord(cText[i])-65]\n", "cEncrypt=cEncrypt[::iLen]\n", "print \"\".join(map(str,cEncrypt))" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n", "Write a text in upper case: JOHNSMITH\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "R918273645\n" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }