23. Sets and Frozen Sets [Python 3 Programming Tutorials]
Key Takeaways
Utilizes Python 3 to demonstrate sets and frozen sets
Full Transcript
today we have another fun topic called sets and frozen sets let's begin with the formal definition of set a set is an unordered collection of unique elements so this is something that you already learnt in your mathematics class while you were in school this is exactly the same thing so I'm going to initialize my first set so let's say I have a basket of fruits and I want to represent that using the set in Python so this is how I do it you should use curly brackets right and then you should add all your elements in this set so I have a bunch of fruits that I have placed in my basket I love mango by the way it's summer and in India the lot of mangoes coming out and they taste so good and then I have some repeated elements I had to april's in my basket two oranges and one mango so I just created a set when I do type of basket it is telling me that the type is sad now let's print the content of basket when I do that you notice that orange was praising two times in my basket but this one is just showing one time so that's what unique element aspect means a set doesn't allow duplicate elements so it will remove duplicates automatically so set could be useful when you want to have on the unique elements for doing anything okay there is another way to initialize set which is by doing this so I can just in his last act like this and then you can add the elements into it so for example I am going to add whatever number be doc so I a dot okay when I print a I get one and two all right now when you initialize the set using the first index which is curly brackets remember that you don't use empty curly brackets because when you do that it is gonna initialize it as dictionary you see this and if I have a is equal to something if I have as long as I have some content into it it will always make it as set okay so remember not to use this another interesting thing about set is they are unordered so you cannot use the index so for example in basket I want to access orange I cannot do this basket 0 this is not allowed so that's the basic difference between a list and a set so list allows index operation set doesn't allow a list allows duplicate elements set doesn't allow it and that's one of the primary use case of said that if whenever you want to remove duplicate elements from a list you use set so for example if you have a list of numbers right so I have list of numbers and as you see 2 3 & 4 are repeated so they're duplicate now when you are doing programming you will most frequently encounter this need of removing duplicate elements from a list whenever you face that kind of situation you should always use set and set supports taking lists as an input in the constructor so I am creating a set object here and this is my constructor and I am passing list as an input and it is allowed so when you print unique numbers here you see all the unique numbers in the list okay now as we saw previously if you want to add any new element to that said you can always add it and if you print it it's gonna work fine now often there are needs where you want your set to be frozen frozen meaning you should not be able to change the content of your set in that case you have to use our frozen set so to initialize a frozen set you can use frozen set and you can say in numbers okay so when you print frozen set is gonna print again it did the same thing so frozen set and set are exactly same they are on or the list they remove duplicate as you see here the remove duplicate from this list but the only difference between certain frozen set is it doesn't allow to add a new element so you saw it's not allowing so you cannot change the context or content of your frozen set okay now let's cover some basic operation that asset covers here I have a set called X which has elements ABC it covers it supports in operator you can say a in X will be true but act whatever G in X will be false also if you want to iterate over all the elements then you can use syntax similar to a list so you can say for I in X so it supports I travel I traitor you can alter it over all the elements like this now if you have another set let's say here I have another circle why is having these three elements a G and H right so access this why has this I want to find out a union of these two in basic set theory there are certain common operations such as Union intersection subset etcetera so in Python all of those things are supported to find out Union you can use or operator X or Y so as you see a was common so it's just present one time and then it has all the elements from both sets if you want to find out an intern intersection you use end operation x and y and you see only element common in both the sides is a hence and it printed a you can also find out a difference so you can say X minus y and that's gonna print B and C because from X if you subtract Y is the common element so it will subtract a and what is left is B and C it also supports subset so if you want to check whether X is a subset of Y you can say X less than Y now here of course X is not a subset of Y if a set is a subset of another set it should have the second set should have all the element from the first set so here I'm going to change X set and I'm gonna say okay it has H and G two elements okay and my wife already has H and G now I do X less than Y it's gonna say true okay so that was all about set operations thank you for watching this week
Original Description
Learn how to use sets and frozen sets in python. Set is basically mathematical set and could be really useful in programming. Frozen sets are same as set except you can not change them (immutable).
Next Video:
24. Command line argument processing using argparse [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=OxpBMNalsDM&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=26
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Website: http://codebasics.io/
Facebook: https://www.facebook.com/codebasicshub
Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
More on: Python for Data
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI