Skip to content
Snippets Groups Projects
Mistakes.ipynb 5.83 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(None, None, 1)\n"
     ]
    }
   ],
   "source": [
    "# this can be a shortcut to assign None to multiple variables\n",
    "x = y = z = None\n",
    "z = 1\n",
    "\n",
    "print(x,y,z)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "b wasn't in there!",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-a791a4bf8f4c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m## assert\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mmylist\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'c'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'f'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;34m'b'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mmylist\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"b wasn't in there!\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m: b wasn't in there!"
     ]
    }
   ],
   "source": [
    "## assert\n",
    "mylist = ['a', 'c', 'f']\n",
    "assert 'b' in mylist, \"b wasn't in there!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(['x'], ['x'], ['x'])\n"
     ]
    }
   ],
   "source": [
    "# BAD: this is probably not what you wanted\n",
    "u, i, o = []\n",
    "o.append(\"x\")\n",
    "\n",
    "print(u,i,o)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(True, ['a', 'x'], ['a', 'x'])\n"
     ]
    }
   ],
   "source": [
    "# BAD: the original list you pass to the method will get updated!\n",
    "def my_method(mutable_list=[]):\n",
    "    mutable_list.append('x')\n",
    "    return mutable_list\n",
    "\n",
    "# a list out of the my_method scope\n",
    "a_list = ['a']\n",
    "\n",
    "b_list = my_method(mutable_list=a_list)\n",
    "\n",
    "# a_list get modified!\n",
    "print( a_list == b_list, a_list, b_list )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "Whats wrong here... \n",
    "```bash\n",
    "\n",
    "conda create -n giphy python=3\n",
    "conda install Flask\n",
    "  (..)\n",
    "  Package plan for installation in environment /Users/scholtal/anaconda:\n",
    "  (..)\n",
    "  The following packages will be UPDATED:\n",
    "  (..)\n",
    "  The following packages will be DOWNGRADED due to dependency conflicts:\n",
    "  (..)\n",
    "\n",
    "```\n",
    "\n",
    "Spot the missing ``source activate giphy``...."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'list'>, {'a': [1, 2]})\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "d = defaultdict(list)\n",
    "d[\"a\"] = [1]\n",
    "d[\"a\"].append(2)\n",
    "print(d)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "defaultdict(<class 'int'>, {'a': 3})\n"
     ]
    }
   ],
   "source": [
    "d = defaultdict(int)\n",
    "d[\"a\"] += 1\n",
    "d[\"a\"] += 2\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'newkey': [1], 'newkey1': [2]}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## without using deafaultdict\n",
    "e = {}\n",
    "e.setdefault(\"newkey\", []).append(1)\n",
    "e.setdefault(\"newkey1\", []).append(2)\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'newkey': [1, 2]}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = dict(newkey=[1])\n",
    "f[\"newkey\"].append(2)\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}