{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set in Python\n",
    "\n",
    "Trainer: Md. Jalal Uddin, Founder and director of Research Society\n",
    "    \n",
    "email: dmjalal90@gmail.com, 20205103002@nuist.edu.cn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set (store data): \n",
    "    \n",
    "    # curly brackets\n",
    "    # unordered (item does not have a defined order)  \n",
    "    # unindex\n",
    "    # unchangeable \n",
    "    # No duplicate members\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# string, int, boolean and tuple data types\n",
    "\n",
    "# list and dictionary are not applicable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset = {True, 'favourite_colors', ('white', 'green', 'red'), 1990, 'good man', 'year of birth', 'Jalal', 'Name'}\n"
     ]
    }
   ],
   "source": [
    "myset = {\"Name\",\"Jalal\",\"year of birth\", 1990, \"good man\",True,\"favourite_colors\", (\"white\", \"green\", \"red\")}\n",
    "print(\"myset =\", myset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# create a set by using set() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset = {True, 'favourite_colors', ('white', 'green', 'red'), 1990, 'good man', 'year of birth', 'Jalal', 'Name'}\n"
     ]
    }
   ],
   "source": [
    "myset = set({\"Name\",\"Jalal\",\"year of birth\", 1990, \"good man\",True,\"favourite_colors\", (\"white\", \"green\", \"red\")})\n",
    "print(\"myset =\", myset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# duplicates are not allowed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'Ripa', 'Jalal'}\n"
     ]
    }
   ],
   "source": [
    "myset = {\"Jalal\",\"Ripa\",\"Jalal\"}\n",
    "print(myset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# join sets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset = {'good man', 'favourite_colors', 'year of birth', 'Jalal', 1990, 'Name'}\n",
      "myset = {'good man', 'favourite_colors', 'year of birth', 'Jalal', 1990, 'Name'}\n"
     ]
    }
   ],
   "source": [
    "myset1 = {\"Name\",\"Jalal\",\"year of birth\"}\n",
    "myset2 = {1990, \"good man\",\"favourite_colors\"}\n",
    "\n",
    "myset3 = myset1.union(myset2)     # union() method returns a new set containing all items from both sets\n",
    "print(\"myset =\", myset3)\n",
    "\n",
    "\n",
    "myset1.update(myset2)            # update() method inserts the items in set2 into set1\n",
    "print(\"myset =\", myset1)\n",
    "\n",
    "# Note: union() and update(): No duplicate items"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keep only duplicates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset = {'Ripa', 'Jalal'}\n"
     ]
    }
   ],
   "source": [
    "myset3 = {\"Jalal\",\"Ripa\",\"year of birth\"}\n",
    "myset4 = {\"Jalal\", \"Ripa\",\"favourite_colors\"}\n",
    "\n",
    "myset5 = myset3.intersection(myset4)     # intersection () method return a set contains the items that present in both set\n",
    "print(\"myset =\", myset5)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# keep all items, no Duplicates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset = {'year of birth', 'favourite_colors'}\n"
     ]
    }
   ],
   "source": [
    "myset5 = myset3.symmetric_difference(myset4)     # symmetric_difference() method returns a new set remove the duplicates\n",
    "print(\"myset =\", myset5)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# add new items by using add() function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "myset =  {'year of birth', 'Jara', 'Ripa', 'Jalal'}\n"
     ]
    }
   ],
   "source": [
    "myset3 = {\"Jalal\",\"Ripa\",\"year of birth\"}\n",
    "\n",
    "myset3.add(\"Jara\")\n",
    "print(\"myset = \",myset3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# remove set items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove Jalal =  {'year of birth', 'Ripa'}\n",
      "discard Ripa =  {'year of birth'}\n"
     ]
    }
   ],
   "source": [
    "myset3 = {\"Jalal\",\"Ripa\",\"year of birth\"}\n",
    "\n",
    "myset3.remove(\"Jalal\")     \n",
    "print(\"remove Jalal = \",myset3)\n",
    "\n",
    "myset3.discard(\"Ripa\")     \n",
    "print(\"discard Ripa = \",myset3)\n",
    "\n",
    "# Note: remove() or  discard() method remove an item in a set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove the last item =  {'Ripa', 'Jalal', 'Jimi'}\n"
     ]
    }
   ],
   "source": [
    "myset4 = {\"Jalal\",\"Ripa\",\"year of birth\",\"Jimi\"}\n",
    "\n",
    "myset4.pop()            \n",
    "print(\"remove the last item = \",myset4) \n",
    "\n",
    "# Note: pop() method removes the last item. However, we do not know which item gets removed because sets are unordered.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "removes all the elements =  set()\n"
     ]
    }
   ],
   "source": [
    "myset4 = {\"Jalal\",\"Ripa\",\"year of birth\"}\n",
    "\n",
    "myset4.clear()                               # clear() method removes all items from the set\n",
    "print(\"removes all the elements = \",myset4) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'myset4' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-11-b5b0310a1888>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mmyset4\u001b[0m                               \u001b[1;31m# delete the set completely\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"delete the set = \"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmyset4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'myset4' is not defined"
     ]
    }
   ],
   "source": [
    "myset4 = {\"Jalal\",\"Ripa\",\"year of birth\"}\n",
    "\n",
    "del myset4                               # delete the set completely\n",
    "print(\"delete the set = \",myset4) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
