Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4824b91

Browse files
added set & frozen_set program
1 parent f989893 commit 4824b91

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

‎Beginner_Level/Frozen Set.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>> # Frozen Set
4+
>>> numbers = [1,2,3,4,1,5,6,4,2,3]
5+
>>> fs = frozenset(numbers)
6+
>>> fs
7+
frozenset({1, 2, 3, 4, 5, 6})
8+
>>> fs.add(7)
9+
Traceback (most recent call last):
10+
File "<pyshell#4>", line 1, in <module>
11+
fs.add(7)
12+
AttributeError: 'frozenset' object has no attribute 'add'
13+
>>>
14+
>>>
15+
>>>
16+
>>> # some basic operation with set
17+
>>>
18+
>>> x ={"a","b","c"}
19+
>>>
20+
>>> "a" in x
21+
True
22+
>>> "g" in x
23+
False
24+
>>> for i in x:
25+
print(i)
26+
27+
28+
a
29+
c
30+
b
31+
>>> x
32+
{'a', 'c', 'b'}
33+
>>> y = {"a","h",g"}
34+
35+
SyntaxError: EOL while scanning string literal
36+
>>> y = {"a","h","g"}
37+
>>> x
38+
{'a', 'c', 'b'}
39+
>>> y
40+
{'g', 'h', 'a'}
41+
>>> # to find union
42+
>>> x|y
43+
{'g', 'c', 'a', 'b', 'h'}
44+
>>> # to find intersection
45+
>>> x&y
46+
{'a'}
47+
>>> # to find difference
48+
>>> x-y
49+
{'c', 'b'}
50+
>>> # to find subset
51+
>>> x<y
52+
False
53+
>>> x={"h","g"}
54+
>>> x<y
55+
True
56+
>>>

‎Beginner_Level/Set.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] on win32
2+
Type "help", "copyright", "credits" or "license()" for more information.
3+
>>> basket = {"apple","mango","orange","pear","banana","apple"}
4+
>>> basket
5+
{'apple', 'pear', 'banana', 'orange', 'mango'}
6+
>>> a = set()
7+
>>> a.add(11)
8+
>>> a.add(22)
9+
>>> a.add(33)
10+
>>> a.add(44)
11+
>>> a
12+
{33, 11, 44, 22}
13+
>>> # don't initialize empty set bcoz python consider it set dictionary
14+
>>> b = {}
15+
>>> type(b)
16+
<class 'dict'>
17+
>>> b = {'somrthing'}
18+
>>> b
19+
{'somrthing'}
20+
>>> type(b)
21+
<class 'set'>
22+
>>> # since set is unordered that means you can't access using index
23+
>>> basket[0]
24+
Traceback (most recent call last):
25+
File "<pyshell#15>", line 1, in <module>
26+
basket[0]
27+
TypeError: 'set' object is not subscriptable
28+
>>>
29+
>>>
30+
>>>
31+
>>>
32+
>>> # creating set from list by passing list as argument in constructor of set
33+
>>> numbers = [1,2,3,4,5,2,1,3,5]
34+
>>> set_numbers = set(numbers)
35+
>>> set_numbers
36+
{1, 2, 3, 4, 5}
37+
>>> set_numbers.add(6)
38+
>>> set_numbers
39+
{1, 2, 3, 4, 5, 6}
40+
>>>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /