** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
s = set()
for i in range(10):
s.add(i)
print(s)
result
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
s = {i for i in range(10)}
print(s)
result
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
You can also add an if statement.
s = {i for i in range(10) if i % 2 == 0}
print(s)
result
{0, 2, 4, 6, 8}
Recommended Posts