├── Assignment 10.2-checkpoint.ipynb ├── Assignment 6.5-checkpoint.ipynb ├── Assignment 7.1-checkpoint.ipynb ├── Assignment 7.2-checkpoint.ipynb ├── Assignment 8.4-checkpoint.ipynb ├── Assignment 8.5-checkpoint.ipynb ├── Assignment 9.4-checkpoint.ipynb ├── Chapter 10 Quiz.ipynb ├── Chapter 6 Quiz.ipynb ├── Chapter 7 Quiz.ipynb ├── Chapter 8 Quiz.ipynb ├── Chapter 9 Quiz.ipynb └── README.md /Assignment 10.2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 5, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Enter file:mbox-short.txt\n", 15 | "04 3\n", 16 | "06 1\n", 17 | "07 1\n", 18 | "09 2\n", 19 | "10 3\n", 20 | "11 6\n", 21 | "14 1\n", 22 | "15 2\n", 23 | "16 4\n", 24 | "17 2\n", 25 | "18 1\n", 26 | "19 1\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "imeFajla = raw_input(\"Enter file:\")\n", 32 | "otvFajl = open(imeFajla)\n", 33 | "rjec = {}\n", 34 | "for i in otvFajl:\n", 35 | " if i.startswith(\"From\") and len(i.split()) > 2:\n", 36 | " line = i.split()\n", 37 | " if not rjec.has_key(line[5][:2]):\n", 38 | " rjec[line[5][:2]] = 1\n", 39 | " else:\n", 40 | " rjec[line[5][:2]] += 1\n", 41 | " \n", 42 | "key = sorted(rjec)\n", 43 | "for i in key:\n", 44 | " print i, rjec[i]" 45 | ] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 2", 51 | "language": "python", 52 | "name": "python2" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 2 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython2", 64 | "version": "2.7.11" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 0 69 | } 70 | -------------------------------------------------------------------------------- /Assignment 6.5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "0.8475\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "text = \"X-DSPAM-Confidence: 0.8475\";\n", 20 | "text = text.replace(\" \", \"\")\n", 21 | "ind = text.find(\":\")\n", 22 | "print float(text[ind+1:])" 23 | ] 24 | } 25 | ], 26 | "metadata": { 27 | "kernelspec": { 28 | "display_name": "Python 2", 29 | "language": "python", 30 | "name": "python2" 31 | }, 32 | "language_info": { 33 | "codemirror_mode": { 34 | "name": "ipython", 35 | "version": 2 36 | }, 37 | "file_extension": ".py", 38 | "mimetype": "text/x-python", 39 | "name": "python", 40 | "nbconvert_exporter": "python", 41 | "pygments_lexer": "ipython2", 42 | "version": "2.7.11" 43 | } 44 | }, 45 | "nbformat": 4, 46 | "nbformat_minor": 0 47 | } 48 | -------------------------------------------------------------------------------- /Assignment 7.1-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Enter file name: words.txt\n", 15 | "WRITING PROGRAMS OR PROGRAMMING IS A VERY CREATIVE\n", 16 | "AND REWARDING ACTIVITY YOU CAN WRITE PROGRAMS FOR\n", 17 | "MANY REASONS RANGING FROM MAKING YOUR LIVING TO SOLVING\n", 18 | "A DIFFICULT DATA ANALYSIS PROBLEM TO HAVING FUN TO HELPING\n", 19 | "SOMEONE ELSE SOLVE A PROBLEM THIS BOOK ASSUMES THAT\n", 20 | "{\\EM EVERYONE} NEEDS TO KNOW HOW TO PROGRAM AND THAT ONCE\n", 21 | "YOU KNOW HOW TO PROGRAM, YOU WILL FIGURE OUT WHAT YOU WANT\n", 22 | "TO DO WITH YOUR NEWFOUND SKILLS\n", 23 | "\n", 24 | "WE ARE SURROUNDED IN OUR DAILY LIVES WITH COMPUTERS RANGING\n", 25 | "FROM LAPTOPS TO CELL PHONES WE CAN THINK OF THESE COMPUTERS\n", 26 | "AS OUR PERSONAL ASSISTANTS WHO CAN TAKE CARE OF MANY THINGS\n", 27 | "ON OUR BEHALF THE HARDWARE IN OUR CURRENT-DAY COMPUTERS\n", 28 | "IS ESSENTIALLY BUILT TO CONTINUOUSLY AS US THE QUESTION\n", 29 | "WHAT WOULD YOU LIKE ME TO DO NEXT\n", 30 | "\n", 31 | "OUR COMPUTERS ARE FAST AND HAVE VASTS AMOUNTS OF MEMORY AND\n", 32 | "COULD BE VERY HELPFUL TO US IF WE ONLY KNEW THE LANGUAGE TO\n", 33 | "SPEAK TO EXPLAIN TO THE COMPUTER WHAT WE WOULD LIKE IT TO\n", 34 | "DO NEXT IF WE KNEW THIS LANGUAGE WE COULD TELL THE\n", 35 | "COMPUTER TO DO TASKS ON OUR BEHALF THAT WERE REPTITIVE\n", 36 | "INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST\n", 37 | "ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING\n", 38 | "AND MIND-NUMBING\n" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "# Use words.txt as the file name\n", 44 | "fname = raw_input(\"Enter file name: \")\n", 45 | "fh = open(fname)\n", 46 | "for line in fh.readlines():\n", 47 | " print line.upper().strip()" 48 | ] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 2", 54 | "language": "python", 55 | "name": "python2" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 2 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython2", 67 | "version": "2.7.11" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 0 72 | } 73 | -------------------------------------------------------------------------------- /Assignment 7.2-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Enter file name: mbox-short.txt\n", 15 | "Average spam confidence: 0.750718518519\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "# Use the file name mbox-short.txt as the file name\n", 21 | "fname = raw_input(\"Enter file name: \")\n", 22 | "fh = open(fname)\n", 23 | "rez = []\n", 24 | "for line in fh.readlines():\n", 25 | " if line.startswith(\"X-DSPAM-Confidence:\"):\n", 26 | " line.replace(\" \", \"\")\n", 27 | " ind = line.find(\":\")\n", 28 | " rez.append(float(line[ind+1:]))\n", 29 | "print \"Average spam confidence:\", sum(rez)/len(rez)" 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 2", 36 | "language": "python", 37 | "name": "python2" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 2 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython2", 49 | "version": "2.7.11" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 0 54 | } 55 | -------------------------------------------------------------------------------- /Assignment 8.4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "unesite ime fajla: romeo.txt\n", 15 | "['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "fajl = raw_input(\"unesite ime fajla: \")\n", 21 | "fajlOpen = open(fajl)\n", 22 | "listica = []\n", 23 | "linije = [line.split() for line in fajlOpen]\n", 24 | "for i in linije:\n", 25 | " for j in i:\n", 26 | " if j not in listica:\n", 27 | " listica.append(j)\n", 28 | "listica.sort()\n", 29 | "print listica" 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 2", 36 | "language": "python", 37 | "name": "python2" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 2 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython2", 49 | "version": "2.7.11" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 0 54 | } 55 | -------------------------------------------------------------------------------- /Assignment 8.5-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Unesite tekst\n", 15 | "mbox-short.txt\n", 16 | "stephen.marquard@uct.ac.za\n", 17 | "louis@media.berkeley.edu\n", 18 | "zqian@umich.edu\n", 19 | "rjlowe@iupui.edu\n", 20 | "zqian@umich.edu\n", 21 | "rjlowe@iupui.edu\n", 22 | "cwen@iupui.edu\n", 23 | "cwen@iupui.edu\n", 24 | "gsilver@umich.edu\n", 25 | "gsilver@umich.edu\n", 26 | "zqian@umich.edu\n", 27 | "gsilver@umich.edu\n", 28 | "wagnermr@iupui.edu\n", 29 | "zqian@umich.edu\n", 30 | "antranig@caret.cam.ac.uk\n", 31 | "gopal.ramasammycook@gmail.com\n", 32 | "david.horwitz@uct.ac.za\n", 33 | "david.horwitz@uct.ac.za\n", 34 | "david.horwitz@uct.ac.za\n", 35 | "david.horwitz@uct.ac.za\n", 36 | "stephen.marquard@uct.ac.za\n", 37 | "louis@media.berkeley.edu\n", 38 | "louis@media.berkeley.edu\n", 39 | "ray@media.berkeley.edu\n", 40 | "cwen@iupui.edu\n", 41 | "cwen@iupui.edu\n", 42 | "cwen@iupui.edu\n", 43 | "There were 27 lines in the file with From as the first word\n" 44 | ] 45 | } 46 | ], 47 | "source": [ 48 | "fh = raw_input(\"Unesite tekst\\n\")\n", 49 | "tekst = open(fh)\n", 50 | "zbroj = 0\n", 51 | "for linija in tekst:\n", 52 | " if linija.startswith(\"From \"):\n", 53 | " rijeci = linija.rstrip().split()\n", 54 | " email = rijeci[1]\n", 55 | " print email\n", 56 | " zbroj +=1\n", 57 | " else:\n", 58 | " continue\n", 59 | "\n", 60 | "print \"There were\", zbroj, \"lines in the file with From as the first word\"" 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 2", 67 | "language": "python", 68 | "name": "python2" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 2 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython2", 80 | "version": "2.7.11" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 0 85 | } 86 | -------------------------------------------------------------------------------- /Assignment 9.4-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "Molim unesite ime fajlambox-short.txt\n", 15 | "cwen@iupui.edu 5\n" 16 | ] 17 | } 18 | ], 19 | "source": [ 20 | "ImeFajla = raw_input(\"Molim unesite ime fajla\")\n", 21 | "tekst = open(ImeFajla)\n", 22 | "Rjecnik = {}\n", 23 | "\n", 24 | "for lines in tekst:\n", 25 | " if lines.startswith(\"From \"):\n", 26 | " words = lines.split()\n", 27 | " email = words[1]\n", 28 | " Rjecnik[email] = Rjecnik.get(email, 0)+1\n", 29 | " \n", 30 | "BigRij = None\n", 31 | "BigBroj = None\n", 32 | "\n", 33 | "for k, v in Rjecnik.items():\n", 34 | " if BigBroj is None or BigBroj < v:\n", 35 | " BigBroj = v\n", 36 | " BigRij = k\n", 37 | "print BigRij, BigBroj" 38 | ] 39 | } 40 | ], 41 | "metadata": { 42 | "kernelspec": { 43 | "display_name": "Python 2", 44 | "language": "python", 45 | "name": "python2" 46 | }, 47 | "language_info": { 48 | "codemirror_mode": { 49 | "name": "ipython", 50 | "version": 2 51 | }, 52 | "file_extension": ".py", 53 | "mimetype": "text/x-python", 54 | "name": "python", 55 | "nbconvert_exporter": "python", 56 | "pygments_lexer": "ipython2", 57 | "version": "2.7.11" 58 | } 59 | }, 60 | "nbformat": 4, 61 | "nbformat_minor": 0 62 | } 63 | -------------------------------------------------------------------------------- /Chapter 10 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What is the difference between a Python tuple and Python list?\n", 8 | " - Lists are mutable and tuples are not mutable\n", 9 | "2. Which of the following methods work both in Python lists and Python tuples?\n", 10 | " - index()\n", 11 | "3. What will end up in the variable y after this code is executed? x , y = 3, 4\n", 12 | " - 4\n", 13 | "4. In the following Python code, what will end up in the variable y? x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}; y = x.items()\n", 14 | " - A list of tuples\n", 15 | "5. Which of the following tuples is greater than x in the following Python sequence? x = (5, 1, 3); if ??? > x : ...\n", 16 | " - (6, 0, 0)\n", 17 | "6. What does the following Python code accomplish, assuming the c is a non-empty dictionary? tmp = list(); for k, v in c.items(): tmp.append( (v, k))\n", 18 | " - It creates a list of tuples where each tuple is a value, key pair\n", 19 | "7. If the variable data is a Python list, how do we sort it in reverse order?\n", 20 | " - data.sort(reverse=True)\n", 21 | "8. Using the following tuple, how would you print 'Wed'? days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')\n", 22 | " - print days[2]\n", 23 | "9. In the following Python loop, why are there two iteration variables (k and v)? c = {'a':10, 'b':1, 'c':22}; for k, v in c.items() : ...\n", 24 | " - Because the items() method in dictionaries returns a list of tuples\n", 25 | "10. Given that Python lists and Python tuples are quite similar - when might you prefer to use a tuple over a list?\n", 26 | " - For a temporary variable that you will use and discard without modifying" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /Chapter 6 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. What does the following Python Program print out? str1 = \"Hello\"; str2 = 'there'; bob = str1 + str2; print bob\n", 8 | " - Hellothere\n", 9 | "2. What does the following Python program print out? x = '40'; y = int(x) + 2; print y\n", 10 | " - 42\n", 11 | "3. How would you use the index operator [] to print out the letter q from the following string? x = 'From marquard@uct.ac.za'\n", 12 | " - print x[8]\n", 13 | "4. How would you use string slicing [:] to print out 'uct' from the following string? x = 'From marquard@uct.ac.za'\n", 14 | " - print x[14:17]\n", 15 | "5. What is the iteration variable in the following Python code? for letter in 'banana' : print letter\n", 16 | " - letter\n", 17 | "6. What does the following Python code print out? print len('banana')*7\n", 18 | " - 42\n", 19 | "7. How would you print out the following variable in all upper case in Python? greet = 'Hello Bob'\n", 20 | " - print greet.upper()\n", 21 | "8. Which of the following is not a valid string method in Python?\n", 22 | " - twist()\n", 23 | "9. What will the following Python code print out? data = 'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'; pos = data.find('.'); print data[pos:pos+3]\n", 24 | " - .ma\n", 25 | "10. Which of the following string methods removes whitespace from both the beginning and end of a string?\n", 26 | " - strip()" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /Chapter 7 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. Given the architecture and terminology we introduced in Chapter 1, where are files stored?\n", 8 | " - Secondary memory\n", 9 | "2. What is stored in a \"file handle\" that is returned from a successful open() call?\n", 10 | " - The handle is a connection to the file's data\n", 11 | "3. What do we use the second parameter of the open() call to indicate?\n", 12 | " - Whether we want to read data from the file or write data to the file\n", 13 | "4. What Python function would you use if you wanted to prompt the user for a file name to open?\n", 14 | " - raw_input()\n", 15 | "5. What is the purpose of the newline character in text files?\n", 16 | " - It indicates the end of one line of text and the beginning of another line of text\n", 17 | "6. If we open a file as follows: xfile = open('mbox.txt'). What statement would we use to read the file one line at a time?\n", 18 | " - for line in xfile:\n", 19 | "7. What is the purpose of the following Python code? fhand = open('mbox.txt'); x = 0; for line in fhand: x = x + 1; print x\n", 20 | " - Count the lines in the file 'mbox.txt'\n", 21 | "8. If you write a Python program to read a text file and you see extra blank lines in the output that are not present in the file input as shown below, what Python string function will likely solve the problem?. From: stephen.marquard@uct.ac.za; From: louis@media.berkeley.edu; From: zqian@umich.edu; From: rjlowe@iupui.edu ...\n", 22 | " - strip()\n", 23 | "9. The following code sequence fails with a traceback when the user enters a file that does not exist. How would you avoid the traceback and make it so you could print out your own error message when a bad file name was entered? fname = raw_input('Enter the file name: '); fhand = open(fname)\n", 24 | " - try / except\n", 25 | "10. What does the following Python code do? fhand = open('mbox-short.txt'); inp = fhand.read()\n", 26 | " - Reads the entire file into the variable inp as a string" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /Chapter 8 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. How are \"collection\" variables different from normal variables?\n", 8 | " - Collection variables can store multiple values in a single variable\n", 9 | "2. What are the Python keywords used to construct a loop to iterate through a list?\n", 10 | " - for/in\n", 11 | "3. For the following list, how would you print out 'Sally'? friends = [ 'Joseph', 'Glenn', 'Sally']\n", 12 | " - print friends[2]\n", 13 | "4. fruit = 'Banana' fruit[0] = 'b'; print fruit\n", 14 | " - Nothing would print the program fails with a traceback\n", 15 | "5. Which of the following Python statements would print out the length of a list stored in the variable data?\n", 16 | " - print len(data)\n", 17 | "6. What type of data is produced when you call the range() function? x = range(5)\n", 18 | " - A list of integers\n", 19 | "7. What does the following Python code print out? a = [1, 2, 3]; b = [4, 5, 6]; c = a + b; print len(c)\n", 20 | " - 6\n", 21 | "8. Which of the following slicing operations will produce the list [12, 3]? t = [9, 41, 12, 3, 74, 15]\n", 22 | " - t[2:4]\n", 23 | "9. What list method adds a new item to the end of an existing list?\n", 24 | " - append()\n", 25 | "10. What will the following Python code print out? friends = [ 'Joseph', 'Glenn', 'Sally' ]; friends.sort(); print friends[0]\n", 26 | " - Glenn" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /Chapter 9 Quiz.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "1. How are Python dictionaries different from Python lists?\n", 8 | " - Python lists are indexed using integers and dictionaries can use strings as indexes\n", 9 | "2. What is a term commonly used to describe the Python dictionary feature in other programming languages?\n", 10 | " - Associative arrays\n", 11 | "3. What would the following Python code print out? stuff = dict(); print stuff['candy']\n", 12 | " - The program would fail with a traceback\n", 13 | "4. What would the following Python code print out? stuff = dict(); print stuff.get('candy',-1)\n", 14 | " - -1\n", 15 | "5. (T/F) When you add items to a dictionary they remain in the order in which you added them.\n", 16 | " - False\n", 17 | "6. What is a common use of Python dictionaries in a program?\n", 18 | " - Building a histogram counting the occurrences of various strings in a file\n", 19 | "7. Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary? if key in counts: counts[key] = counts[key] + 1 else: counts[key] = 1\n", 20 | " - counts[key] = counts.get(key,0) + 1\n", 21 | "8. In the following Python, what does the for loop iterate through? x = dict() ... for y in x : ...\n", 22 | " - It loops through the keys in the dictionary\n", 23 | "9. Which method in a dictionary object gives you a list of the values in the dictionary?\n", 24 | " - values()\n", 25 | "10. What is the purpose of the second parameter of the get() method for Python dictionaries?\n", 26 | " - To provide a default value if the key is not found" 27 | ] 28 | } 29 | ], 30 | "metadata": { 31 | "kernelspec": { 32 | "display_name": "Python 2", 33 | "language": "python", 34 | "name": "python2" 35 | }, 36 | "language_info": { 37 | "codemirror_mode": { 38 | "name": "ipython", 39 | "version": 2 40 | }, 41 | "file_extension": ".py", 42 | "mimetype": "text/x-python", 43 | "name": "python", 44 | "nbconvert_exporter": "python", 45 | "pygments_lexer": "ipython2", 46 | "version": "2.7.11" 47 | } 48 | }, 49 | "nbformat": 4, 50 | "nbformat_minor": 0 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Coursera---Python-Data-Structure-Answers 2 | 3 | this contains all the answers to the quizes and asssignments for "Programming for Everybody (Python- Data Structures)" on Coursera by the University of Michigan. 4 | --------------------------------------------------------------------------------