{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 align=\"center\">Sets</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S1 = {'A', 'B', 'C'}\n",
    "S2 = set(['A', 'B', 'C'])\n",
    "S3 = set(('A', 'B', 'C'))\n",
    "S4 = set({'A', 'B', 'C'})\n",
    "S5 = set({'A': 1, 'B': 2, 'C': 3})\n",
    "S6 = set('ABC')\n",
    "S7 = S1.copy()\n",
    "S1 == S2 == S3 == S4 == S5 == S6 == S7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S = {1, 3, 5, 7, 3, 5, 5}\n",
    "print(S)\n",
    "print(len(S))\n",
    "print(0 not in S)\n",
    "print(5 in S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2}\n",
      "{0, 1, 2, 3, 4}\n",
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}\n",
      "{0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11}\n",
      "{0, 1, 2, 4, 5, 6, 7, 9, 10, 11}\n",
      "{0, 1, 2, 4, 5, 6, 7, 9, 10, 11}\n",
      "{1, 2, 4, 5, 6, 7, 9, 10, 11}\n",
      "{2, 4, 5, 6, 7, 9, 10, 11}\n",
      "set()\n"
     ]
    }
   ],
   "source": [
    "S = set()\n",
    "S.add(0)\n",
    "S.add(1)\n",
    "S.add(2)\n",
    "S.add(2)\n",
    "print(S)\n",
    "S.update({3, 4})\n",
    "print(S)\n",
    "S.update({5}, {6, 7, 8, 9}, {10, 11})\n",
    "print(S)\n",
    "S.remove(8)\n",
    "print(S)\n",
    "S.discard(3)\n",
    "print(S)\n",
    "S.discard(-1)\n",
    "print(S)\n",
    "# Removes an arbitrary element\n",
    "S.pop()\n",
    "print(S)\n",
    "S.pop()\n",
    "print(S)\n",
    "S.clear()\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 2, 4, 6, 7, 8, 9}\n",
      "{0, 2, 4, 6, 8}\n",
      "{0, 2, 4, 6, 8}\n",
      "\n",
      "{1, 3, 5, 6, 8}\n",
      "{1, 3, 5, 6, 8}\n",
      "\n",
      "{1, 3, 5}\n",
      "{1, 3}\n",
      "{1, 3}\n",
      "\n",
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -2}\n",
      "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -5, -4, -3, -1, -2}\n",
      "\n",
      "{0, 1}\n",
      "{0, 1, 4}\n",
      "{2, 3}\n",
      "{0, 1, 2, 3, 4}\n"
     ]
    }
   ],
   "source": [
    "S = set(range(10))\n",
    "print(S.difference({1, 3, 5}))\n",
    "print(S.difference({1, 3, 5}, {7}, {1, 3, 7}, {3, 9}))\n",
    "S.difference_update({1, 3, 5}, {7}, {1, 3, 7}, {3, 9})\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = set(range(6))\n",
    "print(S.symmetric_difference({0, 2, 4, 6, 8}))\n",
    "S.symmetric_difference_update({0, 2, 4, 6, 8})\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = set(range(10))\n",
    "print(S.intersection({-3, -3, -1, 1, 3, 5}))\n",
    "print(S.intersection({-3, -2, -1, 1, 3, 5}, {-3, 1, 3}, {-3, -2, 1, 3}))\n",
    "S.intersection_update({-3, -2, -1, 1, 3, 5}, {-3, 1, 3}, {-3, -2, 1, 3})\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = set(range(10))\n",
    "print(S.union({-2, -1, 1, 3, 5}))\n",
    "print(S.union({-3, -2, -1, 1, 3, 5}, {-5, -4, -1, 1, 3}, {-5, -3, 1, 3}))\n",
    "print()\n",
    "\n",
    "print({0, 1, 2, 3} - {2, 3, 4})\n",
    "print({0, 1, 2, 3} ^ {2, 3, 4})\n",
    "print({0, 1, 2, 3} & {2, 3, 4})\n",
    "print({0, 1, 2, 3} | {2, 3, 4})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S1 = frozenset(['A', 'B', 'C'])\n",
    "S2 = frozenset(('A', 'B', 'C'))\n",
    "S3 = frozenset({'A', 'B', 'C'})\n",
    "S4 = frozenset({'A': 1, 'B': 2, 'C': 3})\n",
    "S5 = frozenset('ABC')\n",
    "S6 = S1.copy()\n",
    "S1 == S2 == S3 == S4 == S5 == S6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S = frozenset({1, 3, 5, 7, 3, 5, 5})\n",
    "print(S)\n",
    "print(len(S))\n",
    "print(0 not in S)\n",
    "print(5 in S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "S = frozenset(range(10))\n",
    "print(S.difference({1, 3, 5}))\n",
    "print(S.difference({1, 3, 5}, {7}, {1, 3, 7}, {3, 9}))\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = frozenset(range(10))\n",
    "print(S.symmetric_difference({0, 2, 4, 6, 8}))\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = frozenset(range(10))\n",
    "print(S.intersection({-3, -3, -1, 1, 3, 5}))\n",
    "print(S.intersection({-3, -2, -1, 1, 3, 5}, {-3, 1, 3}, {-3, -2, 1, 3}))\n",
    "print(S)\n",
    "print()\n",
    "\n",
    "S = frozenset(range(10))\n",
    "print(S.union({-2, -1, 1, 3, 5}))\n",
    "print(S.union({-3, -2, -1, 1, 3, 5}, {-5, -4, -1, 1, 3}, {-5, -3, 1, 3}))\n",
    "print(S)"
   ]
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}

Resource created Wednesday 26 August 2015, 10:25:56 AM, last modified Wednesday 26 August 2015, 03:29:45 PM.

file: sets.ipynb


Back to top

COMP9021 15s2 (Principles of Programming) is powered by WebCMS3
CRICOS Provider No. 00098G