{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Q&A, Part II\n", "\n", "CSCI 1360: Foundations for Informatics and Analytics" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Reverse the keys and values in a dictionary (make the keys the values, and the values the keys)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "I define a child class. Instead of wholesale overriding parent behavior, however, I merely want to *augment* it. How can I do this without re-implementing code in the parent?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How does Python resolve multiple inheritance collisions? (i.e. methods with the same name in multiple parent classes)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "(Good starting point: http://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Name the two types of variable scope in Python. Name the three [four] specific environments which determine these scopes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What does the following code print:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "class Vehicle():\n", " def __init__(self, name):\n", " self.name = name\n", " print(\"I am a {}.\".format(self.name))\n", " \n", " def drive(self, mileage):\n", " print(\"I drove {} miles.\".format(mileage))\n", "\n", "class Car(Vehicle):\n", " def drive(self, mileage, passengers):\n", " print(\"I have {} passengers.\".format(passengers))\n", " super(Car, self).drive(mileage)\n", " \n", "class BMW(Car):\n", " def __init__(self, model, year):\n", " self.model = model\n", " self.year = year\n", " super(BMW, self).__init__(model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "bmw = BMW(\"Roadster\", 2016)\n", "bmw.drive(10, 3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Write a single loop that simultaneously prints three lists which may or may not be the same length." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Flip the type of loop you used in the previous question (if you used a `for` loop, use a `while` loop here to answer the same thing, and vice versa)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What is \"unpacking\"? Give an example." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How can you test if a specific value `x` is in a list? How can you test if a specific value `x` is NOT in a list?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Explain the resulting data structure from the code below. Why would it be useful?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import numpy as np\n", "\n", "list1 = np.random.random(10).tolist()\n", "list2 = np.random.randint(100, 10).tolist()\n", "\n", "data_structure = enumerate(zip(list1, list2)) # What is this?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Draw and describe a list-of-lists that each contain lists (a 3D \"cube\")." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What happens in the following code?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "x = 2\n", "\n", "def f(x):\n", " y = x ** 2\n", " x = x ** 2\n", " return y\n", "\n", "y = f(x)\n", "x = f(y)\n", "\n", "# What is x?\n", "# What is y?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What happens in the following code?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def g(x, y):\n", " x[0] *= x[1]\n", " z = y.copy()\n", " z[0] *= x[1]\n", " return z\n", "\n", "x = [1, 2]\n", "y = [3, 4]\n", "x = g(y, x)\n", "\n", "# What are x and y?" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }