<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# List and sets\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- creating lists and sets -----------\n",
      "[1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]\n",
      "set([0, 1, 2, 3, 4, 5])\n",
      "set([3, 4, 5, 6, 7])\n",
      "\n",
      "2. ---------- size of things -----------\n",
      "11\n",
      "6\n",
      "13\n",
      "\n",
      "3. ---------- get by index -----------\n",
      "1\n",
      "0\n",
      "0\n",
      "1\n",
      "sets are not sorted structures, so you can't use [brackets on them]\n",
      "[0, 1, 2, 3, 4, 5]\n",
      "\n",
      "4. ---------- set/list union and difference -----------\n",
      "set([0, 1, 2])\n",
      "set([0, 1, 2, 3, 4, 5, 6, 7])\n",
      "[1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 10, 20, -30]\n",
      "[]\n",
      "\n",
      "5. ---------- list slicing -----------\n",
      "('1:5 -->', [2, 3, 4, 5])\n",
      "('5: -->', [5, 4, 3, 2, 1, 0])\n",
      "('5:-2 -->', [5, 4, 3, 2])\n",
      "(':-2 -->', [1, 2, 3, 4, 5, 5, 4, 3, 2])\n",
      "('-2: -->', [1, 0])\n",
      "(': -->', [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0])\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]  # this is a list\n",
    "\n",
    "sa = set(a)  # this is a set, as in math class\n",
    "anotherset = {3, 4, 5, 6, 7}  # syntactic sugar for declaring sets (Python is a very \"sweet\" language)\n",
    "\n",
    "print('---------- creating lists and sets -----------')\n",
    "print(a)\n",
    "print(sa)\n",
    "print(anotherset)\n",
    "\n",
    "print('\\n2. ---------- size of things -----------')\n",
    "print(len(a))  # len is a function that knows the size of THINGS\n",
    "print(len(sa))\n",
    "print(len('I am a string'))\n",
    "\n",
    "print('\\n3. ---------- get by index -----------')\n",
    "print(a[0])  # OK, you expected this\n",
    "print(a[10])\n",
    "print(a[-1])  # but maybe not this\n",
    "print(a[-11])\n",
    "try:\n",
    "    sa[0]\n",
    "except TypeError as e:\n",
    "    print(\"sets are not sorted structures, so you can't use [brackets on them]\")\n",
    "print(list(sa))  # but you can convert a set to a list (don't assume any particular order)\n",
    "\n",
    "print('\\n4. ---------- set/list union and difference -----------')\n",
    "print(sa - anotherset)  # You can subtract sets\n",
    "print(sa.union(anotherset))\n",
    "print(a + [10, 20, -30])  # You can add lists\n",
    "print([] + [])  # You gotta try this in javascript... :-)\n",
    "\n",
    "print('\\n5. ---------- list slicing -----------')\n",
    "print('1:5 -->', a[1:5])  # last index is not included\n",
    "print('5: -->', a[5:])\n",
    "print('5:-2 -->', a[5:-2])\n",
    "print(':-2 -->', a[:-2])\n",
    "print('-2: -->', a[-2:])\n",
    "print(': -->', a[:])  # this is usually used as a hack to create a shallow copy of a list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n",
      "['a', 'c', 'd']\n"
     ]
    }
   ],
   "source": [
    "d = {'a': 1, 'b': 'two', 'c': 30}  # this is a dictionary\n",
    "d['d'] = 400  # this is how you add something to it\n",
    "del d['b']  # And this is how you remove stuff\n",
    "\n",
    "print(len(d))  # Like I told you, len knows the size of THINGS\n",
    "print(d['a'])\n",
    "print(d.keys())  # If it were me I would return a set, but this guy resturns a list. Works too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "1\n",
      "(1,)\n",
      "('len(t) = ', 3)\n",
      "('len(t1) = ', 1)\n",
      "{'a': 1, 'c': 30, 'd': 400, (1, 2, 3): 'really?'}\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "t = (1, 2, 3)  # This is a tuple. Think of a tuple as an IMMUTABLE list.\n",
    "notatuple = (1)  # This is just a number\n",
    "t1 = (1,)  # This is a tuple\n",
    "\n",
    "print(t)\n",
    "print(notatuple)\n",
    "print(t1)\n",
    "\n",
    "print('len(t) = ', len(t))\n",
    "print('len(t1) = ', len(t1))  # but you already knew that\n",
    "\n",
    "d[t] = 'really?'  # But did you know tuples could be dictionary keys? Crazy shit!\n",
    "\n",
    "print(d)\n",
    "\n",
    "x1, x2, x3 = t  # triple assignment, yay! (This also works with lists)\n",
    "print(x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef\n",
      "defabc\n",
      "def\n",
      "fabc\n",
      "one and then two and then three and then four\n",
      "['one', 'two', 'three', 'four']\n",
      "Hello Tony \n",
      "Hello Tony, are you cold?\n"
     ]
    }
   ],
   "source": [
    "s1 = 'abc'  # It works\n",
    "s2 = \"def\"  # Either way\n",
    "s3 = \"\"\" This is \n",
    "fine too.\"\"\"\n",
    "# There are four string delimiters: ['], [\"], ['''], [\"\"\"]\n",
    "\n",
    "print(s1 + s2)  # sum = concatenation\n",
    "\n",
    "s3 = s2\n",
    "s2 += s1\n",
    "print(s2)  # Awwwnnnn <3\n",
    "print(s3)  # Strings are immutable!\n",
    "print(s2[-4:])  # Strings are indexable the same as lists (This is profound. Stop and think about this) \n",
    "\n",
    "s4 = ' and then '.join(['one', 'two', 'three', 'four'])  # list elements can be joined together with a string separator\n",
    "print(s4)\n",
    "print(s4.split(' and then '))  # and split back\n",
    "\n",
    "tony = 'Tony'\n",
    "print('Hello %s ' % tony)  # % is a replacement operator. The left hand is a string, the right hand can be a lot of things\n",
    "what = 'cold'\n",
    "print('Hello %s, are you %s?' % (tony, what))  # When making multiple replacements, use a TUPLE in the right hand"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------- list loop (elements)\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n",
      "\n",
      "-------- tuple loop (elements)\n",
      "1\n",
      "2\n",
      "3\n",
      "\n",
      "-------- set loop (elements)\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "\n",
      "-------- dictionary loop (keys)\n",
      "a\n",
      "c\n",
      "d\n",
      "(1, 2, 3)\n",
      "\n",
      "-------- string loop (letters, which make sense since you can use [] on strings)\n",
      "r\n",
      "e\n",
      "a\n",
      "l\n",
      "l\n",
      "y\n",
      "?\n",
      "[(1, 'a'), (2, 'c'), (3, 'd'), (4, (1, 2, 3))]\n",
      "i1 = 1, i2 = a\n",
      "i1 = 2, i2 = c\n",
      "i1 = 3, i2 = d\n",
      "i1 = 4, i2 = (1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "print('--------- list loop (elements)')\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "print('\\n-------- tuple loop (elements)')\n",
    "for i in t:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- set loop (elements)')\n",
    "for i in sa:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- dictionary loop (keys)')\n",
    "for i in d:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- string loop (letters, which make sense since you can use [] on strings)')\n",
    "for i in 'really?':\n",
    "    print(i)\n",
    "    \n",
    "print(zip(a, d))  # zip produces a LIST of TUPLEs from two or more COLLECTIONS. The resulting size is TRUNCATED.\n",
    "for i1, i2 in zip(a, d):\n",
    "    print ('i1 = %s, i2 = %s' % (i1, i2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list comprehensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16]\n",
      "[0, 1, 4, 9, 16]\n",
      "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n"
     ]
    }
   ],
   "source": [
    "lis = [0, 1, 2, 3, 4]\n",
    "\n",
    "lis_squared = [x * x for x in lis]  # list comprehension\n",
    "print(lis_squared)\n",
    "\n",
    "set_squared = {x * x for x in lis}  # set comprehension\n",
    "print(lis_squared)\n",
    "\n",
    "dic_squared = {x: x * x for x in lis}  # dictionary comprehension (if even tuples can be dictionary keys, why numbers wouldn't?)\n",
    "print(dic_squared)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# functions and parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mysum(1, 1)\n",
      "2\n",
      "mysum(2, 1)\n",
      "3\n",
      "mysum(1, 2)\n",
      "3\n",
      "mysum2([1, 2, 3, 4, 5])\n",
      "15\n",
      "mysum2([10, 20, 30])\n",
      "60\n",
      "concatsum([1], {})\n",
      "('', 1)\n",
      "concatsum([1, 2], {})\n",
      "('', 3)\n",
      "concatsum([1, 2], {'vixe': 10, 'maria': 20})\n",
      "vixemaria\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "def mysum(a, b):  # We don't want to override Python's native sum function, do we?\n",
    "    print('mysum(%s, %s)' % (a, b))\n",
    "    return a + b\n",
    "\n",
    "print(mysum(1, 1))  # Good the sky is still blue \n",
    "print(mysum(b=1, a=2))  # You can choose your parameter passing strategy. Positional (above), or named. \n",
    "print(mysum(1, b=2))  # Or you can mix it a little bit\n",
    "\n",
    "\n",
    "def mysum2(*args):  # One * means: a list of POSITIONAL parameters that will be converted to a TUPLE.\n",
    "    print('mysum2(%s)' % list(args))  # If we leave args as a tuple, \"%\" would try to make 5 replacements but we only have one \"%s\"\n",
    "    return sum(args)  # Python's native sum function takes in a collection, so this is fine\n",
    "\n",
    "print(mysum2(1, 2, 3, 4, 5))\n",
    "params = [10, 20, 30]\n",
    "print(mysum2(*params))  # You can also do this. Here the * will \"unfold\" the params list into a list of parameters\n",
    "\n",
    "\n",
    "def concatsum(*args, **kwargs):  # Two ** means a list of named (KEYWORD) parameters that will be converted to a DICTIONARY\n",
    "    print('concatsum(%s, %s)' % (list(args), kwargs))\n",
    "    numbersum1 = sum(args)\n",
    "    stringsum = ''.join(kwargs.keys())\n",
    "    numbersum2 = sum([kwargs[k] for k in kwargs])  # assuming the named parameters contain numbers\n",
    "    return stringsum, numbersum1 + numbersum2  # Returning a tuple. Don't need () because of the return keyword! \n",
    "    \n",
    "    \n",
    "print(concatsum(1))\n",
    "print(concatsum(1, 2))\n",
    "letters, number = concatsum(1, 2, vixe=10, maria=20)\n",
    "print(letters)\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# operator overloading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO\n",
    "pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
<commit_msg>set union sugar<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# List and sets\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------- creating lists and sets -----------\n",
      "[1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]\n",
      "set([0, 1, 2, 3, 4, 5])\n",
      "set([3, 4, 5, 6, 7])\n",
      "\n",
      "2. ---------- size of things -----------\n",
      "11\n",
      "6\n",
      "13\n",
      "\n",
      "3. ---------- get by index -----------\n",
      "1\n",
      "0\n",
      "0\n",
      "1\n",
      "sets are not sorted structures, so you can't use [brackets on them]\n",
      "[0, 1, 2, 3, 4, 5]\n",
      "\n",
      "4. ---------- set/list union and difference -----------\n",
      "set([0, 1, 2])\n",
      "set([0, 1, 2, 3, 4, 5, 6, 7])\n",
      "[1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 10, 20, -30]\n",
      "[]\n",
      "\n",
      "5. ---------- list slicing -----------\n",
      "('1:5 -->', [2, 3, 4, 5])\n",
      "('5: -->', [5, 4, 3, 2, 1, 0])\n",
      "('5:-2 -->', [5, 4, 3, 2])\n",
      "(':-2 -->', [1, 2, 3, 4, 5, 5, 4, 3, 2])\n",
      "('-2: -->', [1, 0])\n",
      "(': -->', [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0])\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]  # this is a list\n",
    "\n",
    "sa = set(a)  # this is a set, as in math class\n",
    "anotherset = {3, 4, 5, 6, 7}  # syntactic sugar for declaring sets (Python is a very \"sweet\" language)\n",
    "\n",
    "print('---------- creating lists and sets -----------')\n",
    "print(a)\n",
    "print(sa)\n",
    "print(anotherset)\n",
    "\n",
    "print('\\n2. ---------- size of things -----------')\n",
    "print(len(a))  # len is a function that knows the size of THINGS\n",
    "print(len(sa))\n",
    "print(len('I am a string'))\n",
    "\n",
    "print('\\n3. ---------- get by index -----------')\n",
    "print(a[0])  # OK, you expected this\n",
    "print(a[10])\n",
    "print(a[-1])  # but maybe not this\n",
    "print(a[-11])\n",
    "try:\n",
    "    sa[0]\n",
    "except TypeError as e:\n",
    "    print(\"sets are not sorted structures, so you can't use [brackets on them]\")\n",
    "print(list(sa))  # but you can convert a set to a list (don't assume any particular order)\n",
    "\n",
    "print('\\n4. ---------- set/list union and difference -----------')\n",
    "print(sa - anotherset)  # You can subtract sets\n",
    "print(sa.union(anotherset))\n",
    "print(sa | anotherset) # you can do this way to",
    "print(a + [10, 20, -30])  # You can add lists\n",
    "print([] + [])  # You gotta try this in javascript... :-)\n",
    "\n",
    "print('\\n5. ---------- list slicing -----------')\n",
    "print('1:5 -->', a[1:5])  # last index is not included\n",
    "print('5: -->', a[5:])\n",
    "print('5:-2 -->', a[5:-2])\n",
    "print(':-2 -->', a[:-2])\n",
    "print('-2: -->', a[-2:])\n",
    "print(': -->', a[:])  # this is usually used as a hack to create a shallow copy of a list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dictionaries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n",
      "['a', 'c', 'd']\n"
     ]
    }
   ],
   "source": [
    "d = {'a': 1, 'b': 'two', 'c': 30}  # this is a dictionary\n",
    "d['d'] = 400  # this is how you add something to it\n",
    "del d['b']  # And this is how you remove stuff\n",
    "\n",
    "print(len(d))  # Like I told you, len knows the size of THINGS\n",
    "print(d['a'])\n",
    "print(d.keys())  # If it were me I would return a set, but this guy resturns a list. Works too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tuples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "1\n",
      "(1,)\n",
      "('len(t) = ', 3)\n",
      "('len(t1) = ', 1)\n",
      "{'a': 1, 'c': 30, 'd': 400, (1, 2, 3): 'really?'}\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "t = (1, 2, 3)  # This is a tuple. Think of a tuple as an IMMUTABLE list.\n",
    "notatuple = (1)  # This is just a number\n",
    "t1 = (1,)  # This is a tuple\n",
    "\n",
    "print(t)\n",
    "print(notatuple)\n",
    "print(t1)\n",
    "\n",
    "print('len(t) = ', len(t))\n",
    "print('len(t1) = ', len(t1))  # but you already knew that\n",
    "\n",
    "d[t] = 'really?'  # But did you know tuples could be dictionary keys? Crazy shit!\n",
    "\n",
    "print(d)\n",
    "\n",
    "x1, x2, x3 = t  # triple assignment, yay! (This also works with lists)\n",
    "print(x2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcdef\n",
      "defabc\n",
      "def\n",
      "fabc\n",
      "one and then two and then three and then four\n",
      "['one', 'two', 'three', 'four']\n",
      "Hello Tony \n",
      "Hello Tony, are you cold?\n"
     ]
    }
   ],
   "source": [
    "s1 = 'abc'  # It works\n",
    "s2 = \"def\"  # Either way\n",
    "s3 = \"\"\" This is \n",
    "fine too.\"\"\"\n",
    "# There are four string delimiters: ['], [\"], ['''], [\"\"\"]\n",
    "\n",
    "print(s1 + s2)  # sum = concatenation\n",
    "\n",
    "s3 = s2\n",
    "s2 += s1\n",
    "print(s2)  # Awwwnnnn <3\n",
    "print(s3)  # Strings are immutable!\n",
    "print(s2[-4:])  # Strings are indexable the same as lists (This is profound. Stop and think about this) \n",
    "\n",
    "s4 = ' and then '.join(['one', 'two', 'three', 'four'])  # list elements can be joined together with a string separator\n",
    "print(s4)\n",
    "print(s4.split(' and then '))  # and split back\n",
    "\n",
    "tony = 'Tony'\n",
    "print('Hello %s ' % tony)  # % is a replacement operator. The left hand is a string, the right hand can be a lot of things\n",
    "what = 'cold'\n",
    "print('Hello %s, are you %s?' % (tony, what))  # When making multiple replacements, use a TUPLE in the right hand"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------- list loop (elements)\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "5\n",
      "4\n",
      "3\n",
      "2\n",
      "1\n",
      "0\n",
      "\n",
      "-------- tuple loop (elements)\n",
      "1\n",
      "2\n",
      "3\n",
      "\n",
      "-------- set loop (elements)\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "\n",
      "-------- dictionary loop (keys)\n",
      "a\n",
      "c\n",
      "d\n",
      "(1, 2, 3)\n",
      "\n",
      "-------- string loop (letters, which make sense since you can use [] on strings)\n",
      "r\n",
      "e\n",
      "a\n",
      "l\n",
      "l\n",
      "y\n",
      "?\n",
      "[(1, 'a'), (2, 'c'), (3, 'd'), (4, (1, 2, 3))]\n",
      "i1 = 1, i2 = a\n",
      "i1 = 2, i2 = c\n",
      "i1 = 3, i2 = d\n",
      "i1 = 4, i2 = (1, 2, 3)\n"
     ]
    }
   ],
   "source": [
    "print('--------- list loop (elements)')\n",
    "for i in a:\n",
    "    print(i)\n",
    "\n",
    "print('\\n-------- tuple loop (elements)')\n",
    "for i in t:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- set loop (elements)')\n",
    "for i in sa:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- dictionary loop (keys)')\n",
    "for i in d:\n",
    "    print(i)\n",
    "    \n",
    "print('\\n-------- string loop (letters, which make sense since you can use [] on strings)')\n",
    "for i in 'really?':\n",
    "    print(i)\n",
    "    \n",
    "print(zip(a, d))  # zip produces a LIST of TUPLEs from two or more COLLECTIONS. The resulting size is TRUNCATED.\n",
    "for i1, i2 in zip(a, d):\n",
    "    print ('i1 = %s, i2 = %s' % (i1, i2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list comprehensions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16]\n",
      "[0, 1, 4, 9, 16]\n",
      "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}\n"
     ]
    }
   ],
   "source": [
    "lis = [0, 1, 2, 3, 4]\n",
    "\n",
    "lis_squared = [x * x for x in lis]  # list comprehension\n",
    "print(lis_squared)\n",
    "\n",
    "set_squared = {x * x for x in lis}  # set comprehension\n",
    "print(lis_squared)\n",
    "\n",
    "dic_squared = {x: x * x for x in lis}  # dictionary comprehension (if even tuples can be dictionary keys, why numbers wouldn't?)\n",
    "print(dic_squared)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# functions and parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mysum(1, 1)\n",
      "2\n",
      "mysum(2, 1)\n",
      "3\n",
      "mysum(1, 2)\n",
      "3\n",
      "mysum2([1, 2, 3, 4, 5])\n",
      "15\n",
      "mysum2([10, 20, 30])\n",
      "60\n",
      "concatsum([1], {})\n",
      "('', 1)\n",
      "concatsum([1, 2], {})\n",
      "('', 3)\n",
      "concatsum([1, 2], {'vixe': 10, 'maria': 20})\n",
      "vixemaria\n",
      "33\n"
     ]
    }
   ],
   "source": [
    "def mysum(a, b):  # We don't want to override Python's native sum function, do we?\n",
    "    print('mysum(%s, %s)' % (a, b))\n",
    "    return a + b\n",
    "\n",
    "print(mysum(1, 1))  # Good the sky is still blue \n",
    "print(mysum(b=1, a=2))  # You can choose your parameter passing strategy. Positional (above), or named. \n",
    "print(mysum(1, b=2))  # Or you can mix it a little bit\n",
    "\n",
    "\n",
    "def mysum2(*args):  # One * means: a list of POSITIONAL parameters that will be converted to a TUPLE.\n",
    "    print('mysum2(%s)' % list(args))  # If we leave args as a tuple, \"%\" would try to make 5 replacements but we only have one \"%s\"\n",
    "    return sum(args)  # Python's native sum function takes in a collection, so this is fine\n",
    "\n",
    "print(mysum2(1, 2, 3, 4, 5))\n",
    "params = [10, 20, 30]\n",
    "print(mysum2(*params))  # You can also do this. Here the * will \"unfold\" the params list into a list of parameters\n",
    "\n",
    "\n",
    "def concatsum(*args, **kwargs):  # Two ** means a list of named (KEYWORD) parameters that will be converted to a DICTIONARY\n",
    "    print('concatsum(%s, %s)' % (list(args), kwargs))\n",
    "    numbersum1 = sum(args)\n",
    "    stringsum = ''.join(kwargs.keys())\n",
    "    numbersum2 = sum([kwargs[k] for k in kwargs])  # assuming the named parameters contain numbers\n",
    "    return stringsum, numbersum1 + numbersum2  # Returning a tuple. Don't need () because of the return keyword! \n",
    "    \n",
    "    \n",
    "print(concatsum(1))\n",
    "print(concatsum(1, 2))\n",
    "letters, number = concatsum(1, 2, vixe=10, maria=20)\n",
    "print(letters)\n",
    "print(number)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# classes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO\n",
    "pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# operator overloading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# TODO\n",
    "pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

{'fraction_non_alphanumeric': 0.25468586285847333, 'fraction_numerical': 0.030734603593465016, 'mean_word_length': 1.8706694886933275, 'pattern_counts': {'":': 240, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239408', 'n_tokens_mistral': 10944, 'n_tokens_neox': 10318, 'n_words': 2690}