Instantly share code, notes, and snippets.

Created Feb 8, 2019
Created on Cognitive Class Labs
 { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", " \n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", " \n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Sets in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Welcome! This notebook will teach you about the sets in the Python Programming Language. By the end of this lab, you'll know the basics set operations in Python, including what it is, operations and logic operations.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", "
\n", "
\n", "

\n", " Estimated time needed: 20 min\n", "

\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Sets

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Set Content

Set Operations

Sets Logic Operations

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Quiz on Sets

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert the list ['rap','house','electronic music', 'rap'] to a set:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'electronic music', 'house', 'rap'}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "set1=set(['rap','house','electronic music','rap'])\n", "set1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider the list A = [1, 2, 2, 1] and set B = set([1, 2, 2, 1]), does sum(A) = sum(B) " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the sum of A is 6\n", "the sum of A is 6\n" ] } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "A=[1,2,2,1]\n", "B=[1,2,2,1]\n", "print(\"the sum of A is \", sum(A))\n", "print(\"the sum of A is \",sum(B))\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a new set album_set3 that is the union of album_set1 and album_set2:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "\n", "album_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\n", "album_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])\n", "album_set3=album_set1.union(album_set2)\n", "album_set3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Find out if album_set1 is a subset of album_set3:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Write your code below and press Shift+Enter to execute\n", "album_set1.issubset(album_set3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Double-click __here__ for the solution.\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The last exercise!

\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Get IBM Watson Studio free of charge!

\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "