Skip to content

Commit 13168fe

Browse files
authored
Add files via upload
1 parent 5a307b7 commit 13168fe

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

Sets.ipynb

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Sets"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"* Set are mutable unordered data structures.\n",
15+
"* They only consist of unique elements. no duplicates are present.\n",
16+
"* Sets are also defined using {} parenthesis like dictionary but there are no key value pairs only elements seprated by comma\n",
17+
"* **set_name={,,,}**"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"sets={1,2,4,5,6,6,6,6,6,7,8,8,2,1,}"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {},
33+
"outputs": [
34+
{
35+
"name": "stdout",
36+
"output_type": "stream",
37+
"text": [
38+
"{1, 2, 4, 5, 6, 7, 8}\n"
39+
]
40+
}
41+
],
42+
"source": [
43+
"print(sets)"
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
49+
"source": [
50+
"As you can see only unique values are present."
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 3,
56+
"metadata": {},
57+
"outputs": [
58+
{
59+
"name": "stdout",
60+
"output_type": "stream",
61+
"text": [
62+
"{1, 2, 4, 5, 6, 7, 8, 9}\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"#add element\n",
68+
"sets.add(9)\n",
69+
"print(sets)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 4,
75+
"metadata": {},
76+
"outputs": [
77+
{
78+
"name": "stdout",
79+
"output_type": "stream",
80+
"text": [
81+
"{1, 2, 5, 6, 7, 8, 9}\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"#remove element\n",
87+
"sets.remove(4)\n",
88+
"print(sets)"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 7,
94+
"metadata": {},
95+
"outputs": [
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"{1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 21}\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"#union of sets - merging data from both the sets\n",
106+
"set2= {4,5,10,11,21}\n",
107+
"print(sets.union(set2))"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 8,
113+
"metadata": {},
114+
"outputs": [
115+
{
116+
"name": "stdout",
117+
"output_type": "stream",
118+
"text": [
119+
"{5}\n"
120+
]
121+
}
122+
],
123+
"source": [
124+
"#intersection - gives common element from both the sets\n",
125+
"print(sets.intersection(set2))"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 9,
131+
"metadata": {},
132+
"outputs": [
133+
{
134+
"name": "stdout",
135+
"output_type": "stream",
136+
"text": [
137+
"False\n"
138+
]
139+
}
140+
],
141+
"source": [
142+
"#to check if the set is a subset of the other\n",
143+
"print(sets.issubset(set2))"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 11,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"{1, 2, 6, 7, 8, 9}\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"#check differnece between two set. displayed all the different value from the other set when compared\n",
161+
"print(sets.difference(set2))"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": []
170+
}
171+
],
172+
"metadata": {
173+
"kernelspec": {
174+
"display_name": "Python 3",
175+
"language": "python",
176+
"name": "python3"
177+
},
178+
"language_info": {
179+
"codemirror_mode": {
180+
"name": "ipython",
181+
"version": 3
182+
},
183+
"file_extension": ".py",
184+
"mimetype": "text/x-python",
185+
"name": "python",
186+
"nbconvert_exporter": "python",
187+
"pygments_lexer": "ipython3",
188+
"version": "3.6.4"
189+
}
190+
},
191+
"nbformat": 4,
192+
"nbformat_minor": 2
193+
}

0 commit comments

Comments
 (0)