Python Tutorial - 24. Sets and Frozen Sets

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

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 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
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 Reads

📰
How I use python to save hours every week
Learn how to use Python to automate tasks and save hours every week, with practical steps for beginners and experienced users alike.
Dev.to AI
📰
What Are AI Software Solutions and How Can They Transform Your Business?
Discover how AI software solutions can transform your business by automating tasks, enhancing decision-making, and driving innovation
Dev.to · upwork floating infotech
📰
AI Shorts generators you can actually edit (not a black box)
Learn to identify editable AI video generators and when to use black box solutions
Dev.to · Reel Mint
📰
I Wanted To Automate Website Testing Without Writing Hundreds Of Scripts. AI Changed The Approach.
Learn how AI-powered testing is revolutionizing website testing by shifting from script-based automation to goal-based testing, increasing efficiency and reducing manual effort
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
Up next
Microsoft Bot Framework Web Chat Authentication with Microsoft Graph API Call using Auth Token in C#
Dewiride Technologies
Watch →