{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'yellow', 'red', 'blue'}\n", "{'yellow', 'red', 'blue'}\n", "{'yellow', 'red', 'blue'}\n" ] } ], "source": [ "A = {'red', 'yellow', 'blue'}\n", "B = {'yellow', 'blue', 'red'}\n", "C = {'yellow', 'blue', 'red', 'red'}\n", "\n", "print(A)\n", "print(B)\n", "print(C)\n", "\n", "# These are all the same set! No order, no duplicates" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yellow\n", "red\n", "blue\n" ] } ], "source": [ "for a in A:\n", " print(a)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "print( 'red' in A )\n", "print( 'green' in A )" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "A = {4, 5, 6}\n", "print(sum(A))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "ename": "TypeError", "evalue": "'set' object does not support indexing", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mA\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# can't access specific index of set\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'set' object does not support indexing" ] } ], "source": [ "print(A[0]) # can't access specific index of set" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['blue', 'red', 'yellow']\n", "blue\n" ] } ], "source": [ "A = {'red', 'yellow', 'blue'}\n", "A = list(A) # convert set to list, then you can access a specific index\n", "A = sorted(A) # order alphabetically \n", "print(A)\n", "print(A[0])" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "def subset(A, B): # return True if A is a subset of B, False if A is not a subset of B\n", " for a in A:\n", " if a not in B:\n", " return False\n", " return True\n", "\n", "A = {'red', 'yellow', 'blue'}\n", "B = {'red', 'yellow', 'blue', 'orange'}\n", "\n", "print(subset(A, B))\n", "print(subset(B, A))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.5.1" } }, "nbformat": 4, "nbformat_minor": 2 }