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 a4abc86

Browse files
committed
Sets in Python Scripts added
1 parent ee2aa3c commit a4abc86

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎Sets_in_python.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Sets is a collection of non-repetitive contents
2+
# Unordered
3+
# Unindexed
4+
# Immutable
5+
6+
a = {1, 2, 3, 4, 1}
7+
print(a) # Output: {1, 2, 3, 4} - It will not include the last '1' in sets because sets do not allow duplicates
8+
print(type(a)) # Output: <class 'set'>
9+
10+
b = {} # This syntax will create an empty dictionary, not an empty set
11+
print(b) # Output: {} - An empty dictionary
12+
print(type(b)) # Output: <class 'dict'>
13+
14+
# To create an empty set, use this syntax
15+
c = set()
16+
print(c) # Output: set()
17+
print(type(c)) # Output: <class 'set'>
18+
19+
# Set methods
20+
c.add(4) # Adds content to a set
21+
c.add(3)
22+
c.add(4) # Adding '4' again, but sets do not allow duplicates, so it will only display '4' once
23+
# c.add([1, 2]) # We cannot add a list or dictionary into a set as they are mutable objects
24+
c.add((6, 7)) # We can add a tuple to a set
25+
print(c) # Output: {3, 4, (6, 7)} - The set after addition
26+
27+
# Length of a set
28+
print(len(c)) # Prints the length of the set
29+
print(c) # Output: {3, 4, (6, 7)} - Current set
30+
31+
# To remove an item from the set
32+
c.remove(3) # Removes '3' from set 'c'
33+
print(c) # Output: {4, (6, 7)} - Set after removing '3'

0 commit comments

Comments
(0)

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