Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Learn Python in One Day and Learn It Well_ Python for Beginners with Hands-on Project. The only book you need to start coding in Python immediately ( PDFDrive )

Learn Python in One Day and Learn It Well_ Python for Beginners with Hands-on Project. The only book you need to start coding in Python immediately ( PDFDrive )

Published by Anutida Mapet, 2022-01-23 10:08:37

Description: Learn Python in One Day and Learn It Well_ Python for Beginners with Hands-on Project. The only book you need to start coding in Python immediately ( PDFDrive )

Search

Read the Text Version

["The\tlast\tchallenge\texercise\tis\tthe\tmost\tdifficult. \t So\tfar,\tbrackets\tare\tmissing\tin\tthe\tquestions\tgenerated.\tCan\tyou\tmodify the\tprogram\tso\tthat\tthe\tquestions\tuse\tbrackets\ttoo?\tAn\texample\tof\ta question\twill\tbe\t2\t+\t(3*7\t-1)\t+\t5. \t Have\tfun\twith\tthese\texercises.\tThe\tsuggested\tsolution\tis\tprovided\tin Appendix\tE.","Thank\tYou \t We\u2019ve\tcome\tto\tthe\tend\tof\tthe\tbook.\tThank\tyou\tfor\treading\tthis\tbook\tand\tI hope\tyou\thave\tenjoyed\tthe\tbook.\tMore\timportantly,\tI\tsincerely\thope\tthe book\thas\thelped\tyou\tmaster\tthe\tfundamentals\tof\tPython\tprogramming. \t I\tknow\tyou\tcould\thave\tpicked\tfrom\ta\tdozen\tof\tbooks\ton\tPython Programming,\tbut\tyou\ttook\ta\tchance\twith\tthis\tbook.\tThank\tyou\tonce again\tfor\tdownloading\tthis\tbook\tand\treading\tall\tthe\tway\tto\tthe\tend. Please\tdo\ttry\tthe\texercises\tand\tchallenges.\tYou\u2019ll\tlearn\ta\tlot\tby\tdoing. \t Now\tI\u2019d\tlike\tto\task\tfor\ta\t\u201csmall\u201d\tfavor.\tCould\tyou\tplease\ttake\ta\tfew minutes\tor\ttwo\tto\tleave\ta\treview\tfor\tthis\tbook\ton\tAmazon? \t This\tfeedback\twill\thelp\tme\ttremendously\tand\twill\thelp\tme\tcontinue\tto write\tmore\tguides\ton\tprogramming.\tIf\tyou\tlike\tthe\tbook\tor\thave\tany suggestions\tfor\timprovement,\tplease\tlet\tme\tknow.\tI\twill\tbe\tdeeply grateful.\t:) \t Last\tbut\tnot\tleast,\tremember\tyou\tcan\tdownload\tthe\tsource\tcode\tfor\tthe project\tand\tthe\tappendices\tat\thttp:\/\/www.learncodingfast.com\/python. \t You\tcan\talso\tcontact\tme\tat\[email protected].","Appendix\tA:\tWorking\tWith\tStrings \t Note:\tThe\tnotation\t[start,\t[end]]\tmeans\tstart\tand\tend\tare\toptional parameters.\tIf\tonly\tone\tnumber\tis\tprovided\tas\tthe\tparameter,\tit\tis\ttaken to\tbe\tstart. \t #\tmarks\tthe\tstart\tof\ta\tcomment \u2018\u2019\u2019\tmarks\tthe\tstart\tand\tend\tof\ta\tmultiline\tcomment\tThe\tactual\tcode\tis\tin monotype\tfont. =>\tmarks\tthe\tstart\tof\tthe\toutput count\t(sub,\t[start,\t[end]]) \t Return\tthe\tnumber\tof\ttimes\tthe\tsubstring\tsub\tappears\tin\tthe\tstring. This\tfunction\tis\tcase-sensitive. \t [Example] \t #\tIn\tthe\texamples\tbelow,\t\u2018s\u2019\toccurs\tat\tindex\t3,\t6\tand\t10 \t #\tcount\tthe\tentire\tstring \u2018This\tis\ta\tstring\u2019.count(\u2018s\u2019) =>\t3 \t #\tcount\tfrom\tindex\t4\tto\tend\tof\tstring\t\u2018This\tis\ta\tstring\u2019.count(\u2018s\u2019,\t4)\t=>\t2 \t #\tcount\tfrom\tindex\t4\tto\t10-1 \u2018This\tis\ta\tstring\u2019.count(\u2018s\u2019,\t4,\t10\t)\t=>\t1 \t #\tcount\t\u2018T\u2019.\tThere\u2019s\tonly\t1\t\u2018T\u2019\tas\tthe\tfunction\tis\tcase\tsensitive. \u2018This\tis\ta\tstring\u2019.count(\u2018T\u2019) =>\t1","endswith\t(suffix,\t[start,\t[end]]) Return\tTrue\tif\tthe\tstring\tends\twith\tthe\tspecified\tsuffix,\totherwise\treturn False. suffix\tcan\talso\tbe\ta\ttuple\tof\tsuffixes\tto\tlook\tfor. This\tfunction\tis\tcase-sensitive. \t [Example] \t #\t\u2019man\u2019\toccurs\tat\tindex\t4\tto\t6 \t #\tcheck\tthe\tentire\tstring \u2018Postman\u2019.endswith(\u2018man\u2019) =>\tTrue \t #\tcheck\tfrom\tindex\t3\tto\tend\tof\tstring\t\u2018Postman\u2019.endswith(\u2018man\u2019,\t3) =>\tTrue \t #\tcheck\tfrom\tindex\t2\tto\t6-1 \u2018Postman\u2019.endswith(\u2018man\u2019,\t2,\t6)\t=>\tFalse \t #\tcheck\tfrom\tindex\t2\tto\t7-1 \u2018Postman\u2019.endswith(\u2018man\u2019,\t2,\t7)\t=>\tTrue \t #\tUsing\ta\ttuple\tof\tsuffixes\t(check\tfrom\tindex\t2\tto\t6-1) \u2018Postman\u2019.endswith((\u2018man\u2019,\t\u2018ma\u2019),\t2,\t6)\t=>\tTrue \t find\/index\t(sub,\t[start,\t[end]]) Return\tthe\tindex\tin\tthe\tstring\twhere\tthe\tfirst\toccurrence\tof\tthe\tsubstring sub\tis\tfound. find()\treturns\t-1\tif\tsub\tis\tnot\tfound. index()\treturns\tValueError\tis\tsub\tis\tnot\tfound. This\tfunction\tis\tcase-sensitive. \t [Example]","#\tcheck\tthe\tentire\tstring \u2018This\tis\ta\tstring\u2019.find(\u2018s\u2019) =>\t3 \t #\tcheck\tfrom\tindex\t4\tto\tend\tof\tstring\t\u2018This\tis\ta\tstring\u2019.find(\u2018s\u2019,\t4)\t=>\t6 \t #\tcheck\tfrom\tindex\t7\tto\t11-1 \u2018This\tis\ta\tstring\u2019.find(\u2018s\u2019,\t7,11\t)\t=>\t10 \t #\tSub\tis\tnot\tfound 'This\tis\ta\tstring'.find(\u2018p\u2019) =>\t-1 \t 'This\tis\ta\tstring'.index(\u2018p\u2019) =>\tValueError \t isalnum() \t Return\ttrue\tif\tall\tcharacters\tin\tthe\tstring\tare\talphanumeric\tand\tthere\tis\tat least\tone\tcharacter,\tfalse\totherwise. Alphanumeric\tdoes\tnot\tinclude\twhitespaces. \t [Example] \t \u2018abcd1234\u2019.isalnum() =>\tTrue \t \u2018a\tb\tc\td\t1\t2\t3\t4\u2019.isalnum() =>\tFalse \t \u2018abcd\u2019.isalnum() =>\tTrue","\u20181234\u2019.isalnum() =>\tTrue \t isalpha() \t Return\ttrue\tif\tall\tcharacters\tin\tthe\tstring\tare\talphabetic\tand\tthere\tis\tat least\tone\tcharacter,\tfalse\totherwise. \t [Example] \t \u2018abcd\u2019.isalpha() =>\tTrue \t \u2018abcd1234\u2019.isalpha() =>\tFalse \t \u20181234\u2019.isalpha() =>\tFalse \t \u2018a\tb\tc\u2019.isalpha() =>\tFalse \t isdigit() \t Return\ttrue\tif\tall\tcharacters\tin\tthe\tstring\tare\tdigits\tand\tthere\tis\tat\tleast one\tcharacter,\tfalse\totherwise. \t [Example] \t \u20181234\u2019.isdigit() =>\tTrue","\u2018abcd1234\u2019.isdigit() =>\tFalse \t \u2018abcd\u2019.isdigit() =>\tFalse \t \u20181\t2\t3\t4\u2019.isdigit() =>\tFalse \t islower() \t Return\ttrue\tif\tall\tcased\tcharacters\tin\tthe\tstring\tare\tlowercase\tand\tthere\tis at\tleast\tone\tcased\tcharacter,\tfalse\totherwise. \t [Example] \t \u2018abcd\u2019.islower() =>\tTrue \t \u2018Abcd\u2019.islower() =>\tFalse \t \u2018ABCD\u2019.islower() =>\tFalse \t isspace() \t Return\ttrue\tif\tthere\tare\tonly\twhitespace\tcharacters\tin\tthe\tstring\tand\tthere is\tat\tleast\tone\tcharacter,\tfalse\totherwise. \t [Example]","\u2018\t\u2019.isspace() =>\tTrue \t \u2018a\tb\u2019.isspace() =>\tFalse \t istitle() \t Return\ttrue\tif\tthe\tstring\tis\ta\ttitlecased\tstring\tand\tthere\tis\tat\tleast\tone character [Example] \t \u2018This\tIs\tA\tString\u2019.istitle() =>\tTrue \t \u2018This\tis\ta\tstring\u2019.istitle() =>\tFalse \t isupper() \t Return\ttrue\tif\tall\tcased\tcharacters\tin\tthe\tstring\tare\tuppercase\tand\tthere is\tat\tleast\tone\tcased\tcharacter,\tfalse\totherwise. \t [Example] \t \u2018ABCD\u2019.isupper() =>\tTrue \t \u2018Abcd\u2019.isupper() =>\tFalse \t \u2018abcd\u2019.isupper()","=>\tFalse \t join() \t Return\ta\tstring\tin\twhich\tthe\tparameter\tprovided\tis\tjoined\tby\ta\tseparator. \t [Example] \t sep\t=\t\u2018-\u2019 myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019) myList\t=\t[\u2018d\u2019,\t\u2018e\u2019,\t\u2018f\u2019] myString\t=\t\u201cHello\tWorld\u201d \t sep.join(myTuple) =>\t\u2018a-b-c\u2019 \t sep.join(myTuple) =>\t\u2018d-e-f\u2019 \t sep.join(myString) =>\t\u2018H-e-l-l-o-\t-W-o-r-l-d\u2019\u2019 \t lower() \t Return\ta\tcopy\tof\tthe\tstring\tconverted\tto\tlowercase. \t [Example] \t \u2018Hello\tPython\u2019.lower() =>\t\u2018hello\tpython\u2019 \t replace(old,\tnew[,\tcount])","Return\ta\tcopy\tof\tthe\tstring\twith\tall\toccurrences\tof\tsubstring\told\treplaced by\tnew. count\tis\toptional.\tIf\tgiven,\tonly\tthe\tfirst\tcount\toccurrences\tare\treplaced. This\tfunction\tis\tcase-sensitive. \t [Example] \t #\tReplace\tall\toccurences \u2018This\tis\ta\tstring\u2019.replace(\u2018s\u2019,\t\u2018p\u2019)\t=>\t'Thip\tip\ta ptring' \t #\tReplace\tfirst\t2\toccurences \u2018This\tis\ta\tstring\u2019.replace(\u2018s\u2019,\t\u2018p\u2019,\t2)\t=>\t'Thip\tip\ta string' \t split([sep\t[,maxsplit]]) \t Return\ta\tlist\tof\tthe\twords\tin\tthe\tstring,\tusing\tsep\tas\tthe\tdelimiter\tstring. sep\tand\tmaxsplit\tare\toptional. If\tsep\tis\tnot\tgiven,\twhitespace\tis\tused\tas\tthe\tdelimiter. If\tmaxsplit\tis\tgiven,\tat\tmost\tmaxsplit\tsplits\tare\tdone. This\tfunction\tis\tcase-sensitive. \t [Example] \t \u2018\u2019\u2019 Split\tusing\tcomma\tas\tthe\tdelimiter\tNotice\tthat\tthere\u2019s\ta\tspace\tbefore\tthe words\t\u2018is\u2019,\t\u2018a\u2019\tand\t\u2018string\u2019\tin\tthe\toutput. \u2018\u2019\u2019 \u2018This,\tis,\ta,\tstring\u2019.split(\u2018,\u2019)\t=>\t['This',\t'\tis',\t' a',\t'\tstring'] \t #\tSplit\tusing\twhitespace\tas\tdelimiter\t\u2018This\tis\ta\tstring\u2019.split()","=>\t['This',\t'is',\t'a',\t'string'] \t #\tOnly\tdo\t2\tsplits \u2018This,\tis,\ta,\tstring\u2019.split(\u2018,\u2019\t2)\t=>\t['This',\t'\tis', '\ta,\tstring'] \t splitlines\t([keepends]) \t Return\ta\tlist\tof\tthe\tlines\tin\tthe\tstring,\tbreaking\tat\tline\tboundaries. Line\tbreaks\tare\tnot\tincluded\tin\tthe\tresulting\tlist\tunless\tkeepends\tis\tgiven and\ttrue. \t [Example] \t #\tSplit\tlines\tseparated\tby\t\\\\n \u2018This\tis\tthe\tfirst\tline.\\\\nThis\tis\tthe\tsecond line\u2019.splitlines()\t=>\t['This\tis\tthe\tfirst\tline.', 'This\tis\tthe\tsecond\tline.'] \t #\tSplit\tmulti\tline\tstring\t(e.g.\tstring\tthat\tuses\tthe\t\u2018\u2019\u2019\tmark)\t\u2018\u2019\u2019This\tis\tthe\tfirst line. This\tis\tthe\tsecond\tline.\u2019\u2019\u2019.splitlines()\t=>\t['This\tis the\tfirst\tline.',\t'This\tis\tthe\tsecond\tline.'] \t #\tSplit\tand\tkeep\tline\tbreaks 'This\tis\tthe\tfirst\tline.\\\\nThis\tis\tthe\tsecond line.'.splitlines(True)\t=>\t['This\tis\tthe\tfirst line.\\\\n',\t'This\tis\tthe\tsecond\tline.'] \t \u2018\u2019\u2019This\tis\tthe\tfirst\tline. This\tis\tthe\tsecond\tline.\u2019\u2019\u2019.splitlines(True)\t=>\t['This is\tthe\tfirst\tline.\\\\n',\t'This\tis\tthe\tsecond\tline.'] \t startswith\t(prefix[,\tstart[,\tend]])","Return\tTrue\tif\tstring\tstarts\twith\tthe\tprefix,\totherwise\treturn\tFalse. prefix\tcan\talso\tbe\ta\ttuple\tof\tprefixes\tto\tlook\tfor. This\tfunction\tis\tcase-sensitive. \t [Example] \t #\t\u2019Post\u2019\toccurs\tat\tindex\t0\tto\t3 \t #\tcheck\tthe\tentire\tstring \u2018Postman\u2019.startswith(\u2018Post\u2019) =>\tTrue \t #\tcheck\tfrom\tindex\t3\tto\tend\tof\tstring\t\u2018Postman\u2019.startswith(\u2018Post\u2019,\t3)\t=> False \t #\tcheck\tfrom\tindex\t2\tto\t6-1 \u2018Postman\u2019.startswith(\u2018Post\u2019,\t2,\t6)\t=>\tFalse \t #\tcheck\tfrom\tindex\t2\tto\t6-1 \u2018Postman\u2019.startswith(\u2018stm\u2019,\t2,\t6)\t=>\tTrue \t #\tUsing\ta\ttuple\tof\tprefixes\t(check\tfrom\tindex\t3\tto\tend\tof\tstring) \u2018Postman\u2019.startswith((\u2018Post\u2019,\t\u2018tma\u2019),\t3)\t=>\tTrue \t strip\t([chars]) \t Return\ta\tcopy\tof\tthe\tstring\twith\tthe\tleading\tand\ttrailing\tcharacters\tchar removed. If\tchar\tis\tnot\tprovided,\twhitespaces\twill\tbe\tremoved. This\tfunction\tis\tcase-sensitive. \t [Example]","#\tStrip\twhitespaces \u2018\tThis\tis\ta\tstring\t\u2019.strip()\t=>\t'This\tis\ta\tstring' \t #\tStrip\t\u2018s\u2019.\tNothing\tis\tremoved\tsince\t\u2018s\u2019\tis\tnot\tat\tthe\tstart\tor\tend\tof\tthe string\t'This\tis\ta\tstring'.strip('s') =>\t'This\tis\ta\tstring' \t #\tStrip\t\u2018g\u2019. \u2018This\tis\ta\tstring\u2019.strip(\u2018g\u2019) =>\t\u2018This\tis\ta\tstrin\u2019 \t upper() \t Return\ta\tcopy\tof\tthe\tstring\tconverted\tto\tuppercase. \t [Example] \t \u2018Hello\tPython\u2019.upper() =>\t\u2018HELLO\tPYTHON\u2019","Appendix\tB:\tWorking\tWith\tLists \t =>\tmarks\tthe\tstart\tof\tthe\toutput append(\t) \t Add\titem\tto\tthe\tend\tof\ta\tlist [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] myList.append(\u2018e\u2019)\tprint\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t del \t Remove\titems\tfrom\ta\tlist [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019,\t\u2018f\u2019,\t\u2018g\u2019,\t\u2018h\u2019,\t\u2018i\u2019, \u2018j\u2019,\t\u2018k\u2019,\t\u2018l\u2019] \t #delete\tthe\tthird\titem\t(index\t=\t2)\tdel\tmyList[2] print\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018d\u2019,\t\u2018e\u2019,\t\u2018f\u2019,\t\u2018g\u2019,\t\u2018h\u2019,\t\u2018i\u2019,\t\u2018j\u2019,\t\u2018k\u2019,\t\u2018l\u2019] \t #delete\titems\tfrom\tindex\t1\tto\t5-1 del\tmyList[1:5] print\t(myList) =>\t[\u2018a\u2019,\t\u2018g\u2019,\t\u2018h\u2019,\t\u2018i\u2019,\t\u2018j\u2019,\t\u2018k\u2019,\t\u2018l\u2019] \t #delete\titems\tfrom\tindex\t0\tto\t3-1 del\tmyList\t[\t:3] print\t(myList) =>\t[\u2018i\u2019,\t\u2018j\u2019,\t\u2018k\u2019,\t\u2018l\u2019]","#delete\titems\tfrom\tindex\t2\tto\tend\tdel\tmyList\t[2:] print\t(myList) =>\t[\u2018i\u2019,\t\u2018j\u2019] \t extend(\t) \t Combine\ttwo\tlists \t [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] myList2\t=\t[1,\t2,\t3,\t4] myList.extend(myList2)\tprint\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019,\t1,\t2,\t3,\t4] \t In \t Check\tif\tan\titem\tis\tin\ta\tlist [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] \u2018c\u2019\tin\tmyList =>\tTrue \t \u2018e\u2019\tin\tmyList =>\tFalse \t insert(\t) \t Add\titem\tto\ta\tlist\tat\ta\tparticular\tposition [Example]","myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] myList.insert(1,\t\u2018Hi\u2019)\tprint\t(myList) =>\t[\u2018a\u2019,\t\u2018Hi\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t len(\t) \t Find\tthe\tnumber\tof\titems\tin\ta\tlist [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] print\t(len(myList))\t=>\t4 \t pop(\t) \t Get\tthe\tvalue\tof\tan\titem\tand\tremove\tit\tfrom\tthe\tlist\tRequires\tindex\tof item\tas\tthe\tparameter [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t #remove\tthe\tthird\titem\tmember\t=\tmyList.pop(2)\tprint\t(member) =>\tc \t print\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t #remove\tthe\tlast\titem\tmember\t=\tmyList.pop(\t)\tprint\t(member) =>\te \t print\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018d\u2019] \t remove(\t)","Remove\tan\titem\tfrom\ta\tlist.\tRequires\tthe\tvalue\tof\tthe\titem\tas\tthe parameter. \t [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t #remove\tthe\titem\t\u2018c\u2019 myList.remove(\u2018c\u2019)\tprint\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018d\u2019,\t\u2018e\u2019] \t reverse() \t Reverse\tthe\titems\tin\ta\tlist [Example] \t myList\t=\t[1,\t2,\t3,\t4] myList.reverse() print\t(myList) =>\t[4,\t3,\t2,\t1] \t sort() \t Sort\ta\tlist\talphabetically\tor\tnumerically [Example] \t myList\t=\t[3,\t0,\t-1,\t4,\t6] myList.sort() print(myList) =>\t[-1,\t0,\t3,\t4,\t6] \t sorted()","Return\ta\tnew\tsorted\tlist\twithout\tsorting\tthe\toriginal\tlist. Requires\ta\tlist\tas\tthe\tparameter [Example] \t myList\t=\t[3,\t0,\t-1,\t4,\t6] myList2\t=\tsorted(myList) #Original\tlist\tis\tnot\tsorted\tprint\t(myList) =>\t[3,\t0,\t-1,\t4,\t6] \t #New\tlist\tis\tsorted\tprint\t(myList2) =>\t[-1,\t0,\t3,\t4,\t6] \t Addition\tOperator:\t+ \t Concatenate\tList \t [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] print\t(myList\t+\t[\u2018e\u2019,\t\u2018f\u2019])\t=>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019, \u2018e\u2019,\t\u2018f\u2019] \t print\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] \t Multiplication\tOperator:\t* \t Duplicate\ta\tlist\tand\tconcatenate\tit\tto\tthe\tend\tof\tthe\tlist [Example] \t myList\t=\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] print\t(myList*3)","=>\t['a',\t'b',\t'c',\t'd',\t'a',\t'b',\t'c',\t'd',\t'a',\t'b',\t'c',\t'd'] \t print\t(myList) =>\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019] \t \t Note: The\t+\tand\t*\tsymbols\tdo\tnot\tmodify\tthe\tlist.\tThe\tlist\tstays\tas\t[\u2018a\u2019,\t\u2018b\u2019, \u2018c\u2019,\t\u2018d\u2019]\tin\tboth\tcases.","Appendix\tC:\tWorking\tWith\tTuples \t =>\tmarks\tthe\tstart\tof\tthe\toutput del \t Delete\tthe\tentire\ttuple [Example] \t myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019)\tdel\tmyTuple print\t(myTuple)\t=>\tNameError:\tname\t'myTuple'\tis\tnot defined in \t Check\tif\tan\titem\tis\tin\ta\ttuple [Example] \t myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019)\t\u2018c\u2019\tin\tmyTuple\t=>\tTrue \t \u2018e\u2019\tin\tmyTuple\t=>\tFalse \t len(\t) \t Find\tthe\tnumber\tof\titems\tin\ta\ttuple [Example] \t myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019)\tprint\t(len(myTuple))\t=> 4 \t Addition\tOperator:\t+","Concatenate\tTuples [Example] \t myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019)\tprint\t(myTuple\t+\t(\u2018e\u2019, \u2018f\u2019))\t=>\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019,\t\u2018e\u2019,\t\u2018f\u2019) print\t(myTuple)\t=>\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019) Multiplication\tOperator:\t* \t Duplicate\ta\ttuple\tand\tconcatenate\tit\tto\tthe\tend\tof\tthe\ttuple [Example] \t myTuple\t=\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019)\tprint(myTuple*3)\t=> ('a',\t'b',\t'c',\t'd',\t'a',\t'b',\t'c',\t'd',\t'a',\t'b', 'c',\t'd') print\t(myTuple)\t=>\t(\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019) Note:\tThe\t+\tand\t*\tsymbols\tdo\tnot\tmodify\tthe\ttuple.\tThe\ttuple\tstays as\t[\u2018a\u2019,\t\u2018b\u2019,\t\u2018c\u2019,\t\u2018d\u2019]\tin\tboth\tcases.","Appendix\tD:\tWorking\tWith\tDictionaries \t =>\tmarks\tthe\tstart\tof\tthe\toutput clear(\t) \t Removes\tall\telements\tof\tthe\tdictionary,\treturning\tan\tempty\tdictionary [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} print\t(dic1) =>\t{1:\t'one',\t2:\t'two'} \t dic1.clear() print\t(dic1) =>\t{\t} \t del \t Delete\tthe\tentire\tdictionary [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} del\tdic1 print\t(dic1) =>\tNameError:\tname\t'dic1'\tis\tnot\tdefined get(\t) \t Returns\ta\tvalue\tfor\tthe\tgiven\tkey. If\tthe\tkey\tis\tnot\tfound,\tit\u2019ll\treturn\tthe\tkeyword\tNone. Alternatively,\tyou\tcan\tstate\tthe\tvalue\tto\treturn\tif\tthe\tkey\tis\tnot\tfound. \t [Example]","dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} dic1.get(1) =>\t\u2018one\u2019 \t dic1.get(5) =>\tNone \t dic1.get(5,\t\u201cNot\tFound\u201d)\t=>\t\u2018Not\tFound\u2019 \t In \t Check\tif\tan\titem\tis\tin\ta\tdictionary [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} \t #\tbased\ton\tthe\tkey\t1\tin\tdic1 =>\tTrue \t 3\tin\tdic1 =>\tFalse \t #\tbased\ton\tthe\tvalue\t\u2018one\u2019\tin\tdic1.values()\t=>\tTrue \t \u2018three\u2019\tin\tdic1.values()\t=>\tFalse \t items(\t) \t Returns\ta\tlist\tof\tdictionary\u2019s\tpairs\tas\ttuples [Example]","dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} dic1.items() =>\tdict_items([(1,\t'one'),\t(2,\t'two')]) keys(\t) \t Returns\tlist\tof\tthe\tdictionary's\tkeys [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} dic1.keys() =>\tdict_keys([1,\t2]) len(\t) \t Find\tthe\tnumber\tof\titems\tin\ta\tdictionary [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} print\t(len(dic1))\t=>\t2 \t update(\t) \t Adds\tone\tdictionary\u2019s\tkey-values\tpairs\tto\tanother.\tDuplicates\tare removed. \t [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} dic2\t=\t{1:\t\u2018one\u2019,\t3:\t\u2018three\u2019} \t dic1.update(dic2)\tprint\t(dic1) =>\t{1:\t'one',\t2:\t'two',\t3:\t'three'} \t print\t(dic2)\t#no\tchange\t=>\t{1:\t\u2018one\u2019,\t3:\t\u2018three\u2019}","values(\t) \t Returns\tlist\tof\tthe\tdictionary's\tvalues [Example] \t dic1\t=\t{1:\t\u2018one\u2019,\t2:\t\u2018two\u2019} dic1.values() =>\tdict_values(['one',\t'two'])","Appendix\tE:\tProject\tAnswers \t Exercise\t1 \t from\trandom\timport\trandint from\tos\timport\tremove,\trename \t Exercise\t2 \t def\tgetUserScore(userName): \t try: input\t=\topen('userScores.txt',\t'r') for\tline\tin\tinput: content\t=\tline.split(',') if\tcontent[0]\t==\tuserName: input.close() return\tcontent[1] input.close() return\t\\\"-1\\\" except\tIOError: print\t(\\\"\\\\nFile\tuserScores.txt\tnot\tfound.\tA\tnew file\twill\tbe\tcreated.\\\")\tinput\t= open('userScores.txt',\t'w') input.close() return\t\\\"-1\\\" \t Exercise\t3 \t def\tupdateUserPoints(newUser,\tuserName,\tscore): if\tnewUser: input\t=\topen('userScores.txt',\t'a') input.write(\u2018\\\\n\u2019\t+\tuserName\t+\t',\t'\t+\tscore) input.close()","else: input\t=\topen('userScores.txt',\t'r') output\t=\topen('userScores.tmp',\t'w') for\tline\tin\tinput: content\t=\tline.split(',') if\tcontent[0]\t==\tuserName: content[1]\t=\tscore line\t=\tcontent[0]\t+\t',\t'\t+\tcontent[1]\t+\t '\\\\n'\t\t\t\t\t\t\t\t\t\t\t\t \t output.write(line) input.close() output.close() \t remove('userScores.txt') rename('userScores.tmp',\t'userScores.txt') Exercise\t4 \t def\tgenerateQuestion(): \t operandList\t=\t[0,\t0,\t0,\t0,\t0] operatorList\t=\t['',\t'',\t'',\t''] operatorDict\t=\t{1:'\t+\t',\t2:'\t-\t',\t3:'',\t4:'*'} \t for\tindex\tin\trange(0,\t5): operandList[index]\t=\trandint(1,\t9) \t for\tindex\tin\trange(0,\t4): if\tindex\t>\t0\tand\toperatorList[index-1]\t!= '**':\t\t\t\t\t\t\toperator\t=\toperatorDict[randint(1,\t4)] \t\t\t\t\t\telse:\t\t\t\t\t\t\toperator\t= operatorDict[randint(1,\t3)]","operatorList[index]\t=\toperator questionString\t=\tstr(operandList[0]) \t for\tindex\tin\trange(1,\t5): \t\t\t\t\t\tquestionString\t=\tquestionString\t+ operatorList[index-1]\t+\tstr(operandList[index]) result\t=\teval(questionString) \t questionString\t=\tquestionString.replace(\\\"**\\\",\t\\\"^\\\") print\t('\\\\n'\t+\tquestionString) \t userResult\t=\tinput('Answer:\t') \t while\tTrue: \t\t\t\t\t\ttry: \t\t\t\t\t\t\t\t\t\t\t\tif\tint(userResult)\t==\tresult:\t \t\t\t\t\t\t\t\t\t\t\t\tprint\t(\\\"So\tSmart\\\")\t\t\t\t\t\t\t\t\t\t\t\t\treturn\t1 \t\t\t\t\t\t\t\t\t\t\t\telse:\t\t\t\t\t\t\t\t\t\t\t\t\tprint\t(\\\"Sorry,\twrong answer.\tThe\tcorrect\tanswer\tis\\\",\tresult)\t \t\t\t\t\t\t\t\t\t\t\t\treturn\t0 \t\t\t\t\t\texcept\tException\tas\te:\t\t\t\t\t\t\tprint\t(\\\"You\tdid not\tenter\ta\tnumber.\tPlease\ttry\tagain.\\\")\t \t\t\t\t\t\tuserResult\t=\tinput('Answer:\t') [Explanation\tfor\tExercise\t4.2] \t Starting\tfrom\tthe\tsecond\titem\t(i.e.\tindex\t=\t1)\tin\toperatorList,\tthe\tline if\tindex\t>\t0\tand\toperatorList[index-1]\t!=\t'**':\tchecks\tif the\tprevious\titem\tin\toperatorList\tis\tthe\t\u2018**\u2019\tsymbol.. \t If\tit\tis\tnot,\tthe\tstatement\toperator\t=\toperatorDict[randint(1, 4)]\twill\texecute.\tSince\tthe\trange\tgiven\tto\tthe\trandint\tfunction\tis\t1\tto 4,\tthe\tnumbers\t1,\t2,\t3\tor\t4\twill\tbe\tgenerated.\tHence,\tthe\tsymbols\t\u2018+\u2019,\t\u2018-\u2019,\t\u2018\u2019 or\t\u2018*\u2019\twill\tbe\tassigned\tto\tthe\tvariable\toperator.","However,\tif\tthe\tprevious\tsymbol\tis\t\u2018**\u2019,\tthe\telse\tstatement\t(operator\t= operatorDict[randint(1,\t3)])\twill\texecute.\tIn\tthis\tcase,\tthe\trange given\tto\tthe\trandint\tfunction\tis\tfrom\t1\tto\t3.\tHence,\tthe\t\u2018**\u2019\tsymbol, which\thas\ta\tkey\tof\t4\tin\toperatorDict\t\twill\tNOT\tbe\t\tassigned\tto\tthe \toperator\tvariable. \t Exercise\t5 \t try: \t import\tmyPythonFunctions\tas\tm \t userName\t=\tinput('''Please\tenter\tyour\tuser\tname\tor create\ta\tnew\tone\tif\tthis\tis\tthe\tfirst\ttime\tyou\tare running\tthe\tprogram:\t''') \t userScore\t=\tint(m.getUserScore(userName)) if\tuserScore\t==\t-1: \t\t\t\t\t\tnewUser\t=\tTrue\tuserScore\t=\t0 else: \t\t\t\t\t\tnewUser\t=\tFalse userChoice\t=\t0 \t while\tuserChoice\t!=\t'-1': \t \t\t\t\t\t\tuserScore\t+=\tm.generateQuestion()\tprint (\\\"Current\tScore\t=\t\\\",\tuserScore)\tuserChoice\t= input(\\\"Press\tEnter\tTo\tContinue\tor\t-1\tto\tExit:\t\\\") m.updateUserPoints(newUser,\tuserName, str(userScore)) except\tException\tas\te: print\t(\\\"An\tunexpected\terror\toccurred.\tProgram\twill\t be\texited.\\\")","Challenge\tYourself \t You\tonly\tneed\tto\tchange\tthe\tfunction\tgenerateQuestion()\tfor\tall\tthe challenges.\tHere\u2019s\tthe\tsuggested\tsolution. \t def\tgenerateQuestion():\t\t\t\t\t\t operandList\t=\t[0,\t0,\t0,\t0,\t0] operatorList\t=\t['',\t'',\t'',\t''] operatorDict\t=\t{1:'\t+\t',\t2:'\t-\t',\t3:'',\t4:'\/', 5:'*'} \t result\t=\t500001 \t while\tresult\t>\t50000\tor\tresult\t<\t-50000:\tfor\tindex in\trange(0,\t5):\t\t\t\t\t\t\toperandList[index]\t=\trandint(1, 9) \t\t\t\t\t\tfor\tindex\tin\trange(0,\t4):\t\t\t\t\t\t\tif\tindex\t>\t0 and\toperatorList[index-1]\t!=\t'**':\t \t\t\t\t\t\t\t\t\t\t\t\toperator\t=\toperatorDict[randint(1,\t4)] \t\t\t\t\t\t\t\t\t\t\t\telse:\t\t\t\t\t\t\t\t\t\t\t\t\toperator\t=\t operatorDict[randint(1,\t5)]\t\t\t\t\t\t\t\t\t\t \t\t\t\t\t\t\t\t\t\t\t\toperatorList[index]\t=\toperator ''' Randomly\tgenerate\tthe\tpositions\tof\t(\tand\t) E.g.\tIf\topenBracket\t=\t2,\tthe\t(\tsymbol\twill\tbe placed\tin\tfront\tof\tthe\tthird\tnumber\tIf closeBracket\t=\t3,\tthe\t)\tsymbol\twill\tbe\tplaced behind\tthe\tfourth\tnumber\tSince\tthe\tclosing bracket\tcannot\tbe\tbefore\tthe\topening\tbracket, we\thave\tto\tgenerate\tthe\tposition\tfor\tthe closing\tbracket\tfrom\topenBracket\t+\t1\tonwards ''' \t\t\t\t\t\t \t\t\t\t\t\topenBracket\t=\trandint(0,\t3)\tcloseBracket\t= randint(openBracket+1,\t4)","if\topenBracket\t==\t0:\t\t\t\t\t\t\tquestionString\t= '('\t+\tstr(operandList[0])\telse:\t\t\t\t\t\t\tquestionString\t= str(operandList[0]) \t\t\t\t\t\tfor\tindex\tin\trange(1,\t5):\t\t\t\t\t\t\tif\tindex\t== openBracket:\t\t\t\t\t\t\t\t\t\t\t\t\tquestionString\t= questionString\t+\toperatorList[index-1]\t+\t'('\t+ str(operandList[index])\t\t\t\t\t\t\telif\tindex\t== closeBracket:\t\t\t\t\t\t\t\t\t\t\t\t\tquestionString\t= questionString\t+\toperatorList[index-1]\t+ str(operandList[index])\t+\t')' \t\t\t\t\t\t\t\t\t\t\t\telse:\t\t\t\t\t\t\t\t\t\t\t\t\tquestionString\t= questionString\t+\toperatorList[index-1]\t+ str(operandList[index]) \t\t\t\t\t\tresult\t=\tround(eval(questionString),\t2) \t\t\t\t\t\t#End\tof\tWhile\tLoop questionString\t=\tquestionString.replace(\\\"**\\\",\t\\\"^\\\") print\t('\\\\n'\t+\tquestionString) \t userResult\t=\tinput('Answer\t(correct\tto\t2\td.p.\tif not\tan\tinteger):\t') while\tTrue: \t\t\t\t\t\ttry: \t\t\t\t\t\t\t\t\t\t\t\tif\tfloat(userResult)\t==\tresult:\t \t\t\t\t\t\t\t\t\t\t\t\tprint\t(\\\"So\tSmart\\\")\t\t\t\t\t\t\t\t\t\t\t\t\treturn\t1 \t\t\t\t\t\t\t\t\t\t\t\telse:\t\t\t\t\t\t\t\t\t\t\t\t\tprint\t(\\\"Sorry,\twrong answer.\tThe\tcorrect\tanswer\tis\\\",\tresult)\t \t\t\t\t\t\t\t\t\t\t\t\treturn\t0 \t\t\t\t\t\texcept\tException\tas\te:\t\t\t\t\t\t\tprint\t(\\\"You\tdid not\tenter\ta\tnumber.\tPlease\ttry\tagain.\\\")\t \t\t\t\t\t\tuserResult\t=\tinput('Answer\t(correct\tto\t2\td.p.\tif\t not\tan\tinteger):\t')","One\tLast\tThing\u2026 \t When\tyou\tturn\tthe\tpage,\tAmazon\twill\tprompt\tyou\tto\trate\tthis\tbook\tand share\tyour\tthoughts\ton\tFacebook\tand\tTwitter. \t If\tthis\tguide\thas\thelped\tyou,\tI\twould\tbe\tdeeply\tappreciative\tif\tyou\twould take\ta\tfew\tseconds\tto\tlet\tyour\tfriends\tknow\tabout\tit. \t To\tme,\tprogramming\tis\tan\tart\tand\ta\tscience.\tIt\tis\thighly\taddictive\tand enjoyable.\tIt\tis\tmy\thope\tto\tshare\tthis\tpassion\twith\tas\tmany\tpeople\tas possible. \t In\taddition,\tI\thope\tyou\tdo\tnot\tstop\tlearning\there.\tIf\tyou\tare\tinterested\tin more\tprogramming\tchallenges,\tyou\tcan\tcheck\tout\tthe\tsite https:\/\/projecteuler.net\/.\tHave\tfun!"]