{ "cells": [ { "cell_type": "markdown", "id": "6240fbf0-a271-4fce-8783-712cd2a44bdb", "metadata": { "id": "6240fbf0-a271-4fce-8783-712cd2a44bdb" }, "source": [ "# Day 7 : Challenge Questions- Set\n", "\n", "Solve Each of the Below Questions and Submit Your Answers on Linked in Post within 48 Hours. Attach Screenshots Preferably. If you are not able to Solve, comment \"Unable to Solve\" or \"Need More Time\"" ] }, { "cell_type": "markdown", "id": "32d8ec23-5d12-4579-b3c1-d8d9f5298535", "metadata": { "id": "32d8ec23-5d12-4579-b3c1-d8d9f5298535" }, "source": [ "# Question 1: Understanding Set Operations and Methods\n", "\n", "**Creating and Accessing Sets**\n", "\n", "1. Create a set A containing the elements: {2, 4, 6, 8, 10}.\n", "2. Create another set B with the elements: {1, 2, 3, 4, 5}.\n", "3. Verify if the element 6 is in set A and if 7 is not in set B.\n", "\n", "**Set Operations**\n", "\n", "1. Find the union of sets A and B.\n", "2. Determine the intersection of sets A and B.\n", "3. Compute the difference of set A from set B.\n", "\n", "**Modifying Sets**\n", "\n", "1. Add the elements 12 and 14 to set A.\n", "2. Remove the element 3 from set B.\n", "3. Clear all elements from set A and confirm its emptiness.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "01097859-466f-4c87-96d7-6ffbfa5f43c7", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "01097859-466f-4c87-96d7-6ffbfa5f43c7", "outputId": "e3f1a1e6-1ec0-4d9c-d970-164b1e69b896" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Is 6 in {2, 4, 6, 8, 10} : True\n", "Is 7 not in {1, 2, 3, 4, 5} : True\n" ] } ], "source": [ "# Creating and Accessing Sets\n", "\n", "A_set = set([x for x in range(1,11) if (x % 2 == 0)]) # A_set = {2, 4, 6, 8, 10}\n", "\n", "B_set = set([x for x in range(1,6)]) # B_set = {1, 2, 3, 4, 5}\n", "\n", "is_6_in_A_set = (6 in A_set)\n", "print(f\"Is 6 in {A_set} : {is_6_in_A_set}\")\n", "\n", "is_7_not_in_B_set = (7 not in B_set)\n", "print(f\"Is 7 not in {B_set} : {is_7_not_in_B_set}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "07084cbf-f6dd-4c04-a4e5-3c6c2472a41b", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "07084cbf-f6dd-4c04-a4e5-3c6c2472a41b", "outputId": "d052a670-0573-4b10-c142-4e1c15241082" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{2, 4, 6, 8, 10} union {1, 2, 3, 4, 5} : {1, 2, 3, 4, 5, 6, 8, 10}\n", "{2, 4, 6, 8, 10} intersection {1, 2, 3, 4, 5} : {2, 4}\n", "{2, 4, 6, 8, 10} difference from {1, 2, 3, 4, 5} : {8, 10, 6}\n" ] } ], "source": [ "# Set Operations\n", "\n", "# Union -> A | B\n", "A_union_B = A_set.union(B_set)\n", "print(f\"{A_set} union {B_set} : {A_union_B}\")\n", "\n", "# Intersection -> A & B\n", "A_intersection_B = A_set.intersection(B_set)\n", "print(f\"{A_set} intersection {B_set} : {A_intersection_B}\")\n", "\n", "# Set Difference -> A - B\n", "A_difference_B = A_set.difference(B_set)\n", "print(f\"{A_set} difference from {B_set} : {A_difference_B}\")" ] }, { "cell_type": "code", "source": [ "# Modifying Sets\n", "\n", "# adding elements 12 and 14 to set A\n", "A_set.add(12)\n", "A_set.add(14)\n", "print(f\"Updated set A : {A_set}\")\n", "\n", "# removing element 3 from set B\n", "B_set.discard(3) #B_set.remove(3)\n", "print(f\"Updated set B : {B_set}\")\n", "\n", "# clearing set A\n", "A_set.clear()\n", "print(f\"Updated set A : {A_set}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1edZYvG3jwh_", "outputId": "6199a980-d023-4502-bfc9-371218ca882c" }, "id": "1edZYvG3jwh_", "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Updated set A : {2, 4, 6, 8, 10, 12, 14}\n", "Updated set B : {1, 2, 4, 5}\n", "Updated set A : set()\n" ] } ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.8" }, "colab": { "provenance": [] } }, "nbformat": 4, "nbformat_minor": 5 }