Skip to content

Commit eb1b800

Browse files
committed
Sample code for the article on set comprehension
1 parent 2487b88 commit eb1b800

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed

python-set-comprehension/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Set Comprehensions: How and When to Use Them
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Set Comprehensions: How and When to Use Them](https://realpython.com/python-set-comprehension/).

python-set-comprehension/colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
colors = {"blue", "red", "green", "orange"}
2+
print(colors)
3+
colors.add("purple")
4+
print(colors)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2+
3+
print({number**2 if number % 2 == 0 else number**3 for number in numbers})
4+
5+
result_set = set()
6+
for number in numbers:
7+
if number % 2 == 0:
8+
value = number**2
9+
else:
10+
value = number**3
11+
result_set.add(value)
12+
13+
print(result_set)

python-set-comprehension/emails.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
emails = [
2+
3+
4+
5+
6+
7+
8+
9+
]
10+
11+
print({email.strip().lower() for email in emails})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
emails_set = {
2+
3+
4+
5+
6+
7+
}
8+
9+
print({email for email in emails_set if email.endswith(".com")})

python-set-comprehension/matrix.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
matrix = [
2+
[9, 3, 8, 3],
3+
[4, 5, 2, 8],
4+
[6, 4, 3, 1],
5+
[1, 0, 4, 5],
6+
]
7+
8+
print({value**2 for row in matrix for value in row})

python-set-comprehension/text.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
unique_words = set()
2+
text = """
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
"""
8+
9+
for word in text.split():
10+
unique_words.add(word)
11+
12+
print(unique_words)
13+
14+
print(set(text.split()))
15+
16+
unique_words = {word for word in text.split()}
17+
18+
print(unique_words)

python-set-comprehension/tools.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tools = ["Python", "Django", "Flask", "pandas", "NumPy"]
2+
tools_set = {tool.lower() for tool in tools}
3+
4+
print(tools_set)
5+
print("python".lower() in tools_set)
6+
print("Pandas".lower() in tools_set)
7+
print("Numpy".lower() in tools_set)
8+
print("Numpy".lower() in tools_set)

0 commit comments

Comments
 (0)