From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Create a set in Python

Create a set in Python

- [Instructor] How do sets work in Python? Let's take a look. Consider the case where we want to keep track of the primary colors: red, blue, and yellow. To create a set, we use the keyword set and in between parentheses and square brackets we put in our values. That's red, blue, and yellow. Each value is separated with a comma. Let's say we wanted to find out if green was a primary color. Or rather, in the set of primary colors. We can check for membership with an IF statement. If the color is in the set, the if block will be run. And we can print out it's a primary color. If it's not in the set, the color is not a primary color. Let's run it. Green is not a primary color. Let's create another set. This time we'll have letters. We'll create a set with A and B. We can add another letter to it with the add function. We'll access the set and use add. We'll add C. Then, we'll print out the set. Let's run it. In…

Contents