# Python set() Function — A Simple Guide with Video

Python’s built-in `set()` function creates and returns a new set object. A set is an unordered collection of unique elements. Without an argument, `set()` returns an empty set. With the optional argument, `set(iter)` initializes the new set with the elements in the iterable.

## Usage

Learn by example! Here are some examples of how to use the `set()` built-in function:

You can create an empty set by skipping the argument:

```>>> set()
set()```

If you pass an iterable—such as another list, a tuple, a set, or a dictionary—you obtain a new set object with elements obtained from the iterable:

```>>> set([1, 2, 3])
{1, 2, 3}```

Note that it really creates a new set object that is different from the one passed as an argument:

```>>> x = {1, 2, 3}
>>> y = set(x)
>>> x is y
False
>>> x == y
True```

The new set `y` has the same elements as the original set `x`. But it’s still a different object as you can see from the check `x is y` that returns `False`.

## Syntax set()

You can use the `set()` method with or without the optional `iterable` argument.

```Syntax: There are two ways of using the constructor:
```set() -> new empty set
set(iterable) -> new set initialized with elements in iterable``````

## Interactive Shell Exercise: Understanding set()

Consider the following interactive code:

Exercise: Guess the output before running the code.

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

## Summary

Python’s built-in `set()` function creates and returns a new set object.

• When used without an argument, it returns an empty set.
• When used with the optional `iterable` argument, it initializes the new set with the elements in the iterable.
```>>> set()
set()
>>> set([1, 2, 3])
{1, 2, 3}```

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!