{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Set and Booleans\n", "\n", "There are two other object types in Python that we should quickly cover. Sets and Booleans. \n", "\n", "##Sets\n", "\n", "Sets are an unordered collection of *unique* elements. We can construct them by using the set() function. Let's go ahead and make a set to see how it works" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = set()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# We add to sets with the add() method\n", "x.add(1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note the curly brackets. This does not indicate a dictionary! Although you can draw analogies as a set being a dictionary with only keys.\n", "\n", "We know that a set has only unique entries. So what happens when we try to add something that is already in a set?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Add a different element\n", "x.add(2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "x" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Try to add the same element\n", "x.add(1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how it won't place another 1 there. That's because a set is only concerned with unique elements! We can cast a list with multiple repeat elements to a set to get the unique elements. For example:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a list with repeats\n", "l = [1,1,2,2,3,4,5,6,1,1]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Cast as set to get unique values\n", "set(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Booleans\n", "\n", "Python comes with Booleans (with predefined True and False displays that are basically just the integers 1 and 0). It also has a placeholder object called None. Let's walk through a few quick examples of Booleans (we will dive deeper into them later in this course)." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Set object to be a boolean\n", "a = True" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use comparison operators to create booleans. We will go over all the comparison operators later on in the course." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Output is boolean\n", "1 > 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use None as a placeholder for an object that we don't want to reassign yet:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# None placeholder\n", "b = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thats it! You should now have a basic understanding of Python objects and data structure types. Next, go ahead and do the assessment test!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }