From the course: Python Data Structures: Sets and Frozen Sets

Unlock this course with a free trial

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

Add an element to a set

Add an element to a set

- [Instructor] You can add an element to the original set. So how would you add an element to an empty set? Let keywords be our empty set. And then inside keywords, let us add some keywords. So I add keywords.add, import is a keyword. And then again, we can do keywords.add. Class is a keyword and then we can simply print keywords. As we have already written how to add elements in the set, we can remove the todo comment and then add program. You can see now the keyword set consists of import and class. Initially keywords was an empty set. Now it consists of import and class. How to add elements to a pre-populated set. Lets let numbers be set and it be 1, 2, 3, 4, 5. And now I'm interested in adding 10 to numbers. So numbers.add, you can simply write 10 and then do print numbers. Here also there is no need of using f string. We can just have keywords and then we can hear also just print numbers. We have already returned the example so we can remove comment on line 11. Let us run the…

Contents