23. Sets and Frozen Sets [Python 3 Programming Tutorials]

codebasics · Beginner ·🛠️ AI Tools & Apps ·7y ago

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 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →