Skip to content

Commit 42d403e

Browse files
set and loops
1 parent ac2c98c commit 42d403e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

collections/sets.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Sets
2+
nums = {1,2,3,4,5}
3+
nums2 = set((1,2,3,4,5))
4+
5+
print(nums)
6+
print(nums2)
7+
8+
print(type(nums))
9+
print(type(nums2))
10+
11+
print(len(nums))
12+
13+
# no duplicates alllowed
14+
nums = {1,2,2,2,3,4,5}
15+
print(nums)
16+
17+
# True ia a dupe of 1, False is dupe of zero
18+
nums = {1, True, 2, 3, False, 4, 0, 5}
19+
print(nums)
20+
21+
# check if a value is in a set
22+
print(2 in nums)
23+
24+
# but we cannot refer to an element in the set with an index position or key
25+
# add a new element to a set
26+
nums.add(8)
27+
print(nums)
28+
29+
# add an elements from one set to another
30+
morenums = {6,7,9}
31+
nums.update(morenums)
32+
33+
print(nums)
34+
35+
# we can use update with list, tuples, and dictionaries, too.
36+
37+
# merge two sets to create a new set
38+
one = {1,2,3}
39+
two = {4,5,6}
40+
41+
mynewSet = one.union(two)
42+
print(mynewSet)
43+
44+
# keep only duplicates
45+
one = {1,2,3}
46+
two = {2,3,6}
47+
48+
one.intersection_update(two)
49+
print(one)
50+
51+
# keep everything except the duplicates
52+
one = {1,2,3}
53+
two = {2,3,4}
54+
55+
one.symmetric_difference_update(two)
56+
print(one)

loops/whileloop.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,14 @@
4848
while i <= 10:
4949
print(i)
5050
i += 2
51+
52+
print(" while loop demo")
53+
value = 0
54+
while value <=10:
55+
value += 1
56+
if value == 5:
57+
continue
58+
print(value)
59+
else:
60+
print("Value is now equals to ",value)
61+

0 commit comments

Comments
 (0)