Python Tutorial - 24. Sets and Frozen Sets
Skills:
Python for Data90%
Key Takeaways
Sets and frozen sets in Python
Full Transcript
hello and welcome to core Basics coding tutorial 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 learned 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 a set in Python so this is how I do it you should use C brackets right and then you should add all your elements in the sent uh in this set so I have bunch of fruits that I have placed in my basket I love I like I love mango by the way it's summer and India uh there are a lot of mangoes coming out and they taste so good and then I have some repeated element so I have two apples 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 set now let's print the content of basket when I do that you notice that uh orange was present two times in my basket but this one is just showing one time so that's what unique element aspect means uh a set doesn't allow duplicate elements so it will remove duplicates automatically so set could be useful when you want to have only unique elements uh for doing anything okay there is another way to initialize set which is by doing this so you can just initialize set like this and then you can add the elements into it so for example I'm going to add whatever number B do add so a do add okay when I print a I get one and two all right now when you initialize the set using the first syntax which is cly brackets remember that you don't use um empty cly brackets because when you do that it is going to initialize it as dictionary you see this uh and if I have a is equal to something if I have as long as I have some content into it it will always always make it a set okay so remember not to use this another interesting thing about set is um they are unordered so you cannot use the index so for example in basket I want to access orange I cannot do this basket zero this is not as allowed so that's the basic difference between a list and a set so list allows index operation set doesn't allow it list allows duplicate elements set doesn't allow it and that's one of the primary use case of set that 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 three and four are repeated so they are duplicate now when you're are doing programming you will most frequently encounter this need of REM removing duplicate elements from a list whenever you uh face that kind of situation you should always use set and set supports taking list as an input in the Constructor so I'm creating a set object here and this is my Constructor and I'm passing list as an input and it is allowed so when you print uh unique numbers here you will see all the unique numbers in the list okay now as we saw previously if you want to add any new element to that uh s you can always add it and if you print it it's going to uh 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 uh you have to use a frozen set so to initialize a frozen set you can use frozen set and you can say numbers okay so when you print Frozen set is going to print again it did the same thing so Frozen set and set are exactly same they are unorder list they removed duplicate as you see here they removed duplicate from this list uh but the only difference between set and 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 of uh content of uh your Frozen set okay now let's cover some basic operations that uh a set covers uh here I have a set called X which has elements a b c uh it covers it supports um in operator so you can say a in X will be true but uh 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 4 I in X so it supports iterable iterator you can iterate over all the elements like this now if you have another set uh let's say here I have another set called Y is having these three elements AG and H right so X has this y has this I want to find out a union of these two right in in in in basic set theory there are certain common operations such as Union intersection subset Etc so in Python all of those things are supported uh to find out Union you can use our operator X or Y so as you see a was common so is just present one time and then it has all the elements from both the sets if you want to find out an inter intersection you use end Operation X and Y and you see only element common in both the sets is a hence it printed a you can also find out a difference so you can say x - Y and that's going to print B and C because from X if you subtract y a 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 elements from the first set so here I'm going to change X set and I'm going to say okay it has H and G two elements okay and my y already has H and G now I do X less than y it's going to say true okay so that was all about set operations thank you for watching this video
Original Description
Video without background music: https://youtu.be/RD6JionMlXM
This tutorial will explain about “sets and frozen sets”. The major topics highlighted in this video are what is set, examples of set, operations of set, what is “frozen set”, the difference between “list and set”, what is “in operator”, what is union, intersect, suprect and subset operation of set.
Topics that are covered in this Python Video:
0:00 Introduction
0:08 Definition of Set and example of set
3:11 Set is an Unordered
3:28 List vs set basics difference
5:04 What is frozen set and its example
5:59 implement basics operation of set
6:12 "in" operator
7:06 Union, intersect, suprect and subset operation of set
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:
Python Tutorial - 25. Command line argument processing using argparse: https://www.youtube.com/watch?v=XYUXFR5FSxI&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=27
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 · 37 of 60
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
▶
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 Reads
📰
📰
📰
📰
How I use python to save hours every week
Dev.to AI
What Are AI Software Solutions and How Can They Transform Your Business?
Dev.to · upwork floating infotech
AI Shorts generators you can actually edit (not a black box)
Dev.to · Reel Mint
I Wanted To Automate Website Testing Without Writing Hundreds Of Scripts. AI Changed The Approach.
Medium · AI
Chapters (8)
Introduction
0:08
Definition of Set and example of set
3:11
Set is an Unordered
3:28
List vs set basics difference
5:04
What is frozen set and its example
5:59
implement basics operation of set
6:12
"in" operator
7:06
Union, intersect, suprect and subset operation of set
🎓
Tutor Explanation
DeepCamp AI