Python Sets Tutorial #2 - Methods
Skills:
ML Maths Basics90%
Key Takeaways
This video tutorial covers various set methods in Python, including intersection, union, difference, symmetric difference, and copying sets, which are useful in linear algebra and mathematical sets.
Full Transcript
in this video I'm gonna be going over some useful set methods and continuing to talk about sets more detail than showing you some more advanced things that you can do with them if you haven't seen my previous video just talking about how a set works it's an unordered unique data set or collection of elements sorry I recommend you go watch that if you already know about sets then just stay here and I'm just gonna go through some really useful methods and pretty much wanted me to talk about are here right in this little box so feel free to just read through them and kind of experiment with them yourself but if you want a more like in-depth kind of tutorial on them then watch through the whole video I'm going to go through what all of these do and how they work so sets are really useful in Python because they come with a lot of methods and if any of you guys are in linear algebra then you know that there's a lot of things you want to do with sets like subsets super sets differences intersections unions and those can be annoying to have to code yourself so yes you can code them yourself just using typical lists but these sets allow you just to type like one method and have it all done for you and it saves you a lot of time it also just looks cleaner in your code so I'm going to start by creating two sets so I'm just going to say s is equal to like 1 2 3 7 & 8 and why not just a little string in here we'll do a test I'm going to do a set T I'm gonna set this equal to 3 4 5 8 9 tests like this okay now one of the most useful methods that you can use on sets is I would want to say intersection and intersection is gonna give you the most or any elements that are in both of the sets and this is really useful there's a lot of applications and so I'll show you how you can do it you can do s dot intersection of T like this and what this is gonna do is it's gonna return a new set containing all of the elements that are common within set so when I do that you see we get 8 test and 3 and those are because they occur in both sets another one that's really useful is Union and what this does is it simply just combines all of the elements in the set together so if I do s dot Union T then you can see we get all of the elements combined together and notice because it's a set we don't have any duplicate elements it just finds ones just like bushes these two sets together another one that's useful is is subset so I'm gonna just create a small set anesthetic SS is equal to like 1 2 3 that's not a set is it it's a list of my bad because we're not gonna be able to use this on list so SS equals that and we'll just do like TT equals and let's do 1 2 now if we want to determine if TT is a subset of SS there's a way that we can do this we can say is is subset is actually the method so say SS or TT DUT is subset of SS and that gives us a true value and the way that this works is it pretty much finds and our it looks at TT and says are all the elements in TT also contained in SS and if they are then that means this is indeed a subset of the set SS ok so let's do this again except let's just change this around so SS is SS a subset of TT well I'll let you guess by having a look and the answer is no and that is because if we look at SS is every element SS in TT no it's not so this is going to give us a false value now another one that we can use is is superset and it's kind of just like a reverse of this so if I do ss is superset of TT now it's going to check and see if all the elements in TT are in SS so it's doing the same thing as is subset just kind of in Reverse so it's giving us a true value now so that means that this contains all the elements that are in like the lower bound set kind of so again if we flip this around we do TT SS we get false ok so another one that's really useful is difference and what difference is gonna do it's gonna give us all the elements that are in one set but are not in the other so if I say s dot difference I'm going to go back up to these tops that's now and use these I start difference of T then we get set of one two and seven so these are the elements that are in s that are not in T and same thing if we do TDOT different s then we get the elements that are in T but our 9s and again that's useful as well you want to find elements that aren't common to both sets now we can also do something called a symmetric difference and with some symmetric difference does is it's going to find elements that aren't in either of these sets okay so if we do symmetric difference of T then we get one two four five seven nine and the way that symmetric works is it finds the elements set like aren't in common with the set so you can see one does not exist in this set two does not exist in this set three does we don't get three and four well that doesn't exist in the other set so we add four five doesn't exist in the other sets we had five seven eight does exist so nice so it finds all the elements that are not common in both of the sets and combines them into a new set now it's good to notice here what these are doing is they're returning us a new set and we can see that because they're printing it out to the screen so it's not actually changing s and it's not changing T and I can press NT it's just returning a new set now the last one I'm going to show you this is pretty straightforward since sets are mutable sometimes you may want to make a copy of them and you can't actually RL test but I'm pretty sure you can't do something like this so set H equals s : yeah we get an error so that would be how you copy lists so how do you copy a set the way to copy a sent is through H is equal to s con copy now for any of you guys that are a bit more advanced this is creating a shallow copy of s you don't have to know what that means but I'm just giving it to you guys in case that means something to you so that's how you would make a copy so now if you change something in H sources like each dot remove what's in s let's see like one and we print H and then we print s we can see we did not end up changing s because we made a copy anyway so that's been it for some of these useful set methods if any of you guys are in linear algebra I hope you appreciate these because coding these can be kind of difficult and also just take a lot of time so these methods save you a ton of time and just help you out when programming anyways that's been it for this video if you guys enjoyed please make sure you leave a like and subscribe and I'll see you again in another one [Music]
Original Description
In this video I will talking about and showing different set methods in python. These are very useful methods and relate heavily to linear algebra and mathematical sets. These will save you much time in the future.
Showcased methods:
- .intersection()
- .union()
- .difference()
- .symmetric_difference()
- .issubset()
- .issuperset()
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Set methods in python
- Python sets tutorial
- Sets in python
- Sets python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: ML Maths Basics
View skill →Related Reads
📰
📰
📰
📰
I Don’t Want to Build Another App. I Want to Find Out Where the Models Break.
Medium · Machine Learning
A Graph Neural Network Model for Real-Time Gesture Recognition Based on sEMG Signals
ArXiv cs.AI
Evaluating the Effect of Frame Rate in Sequence-Based Classification of Autism-Related Self-Stimulatory Hand Idiosyncrasies
ArXiv cs.AI
Measure, Don't Estimate: Labeling Speakers Without a Gated Model
Dev.to · Dima Statz
🎓
Tutor Explanation
DeepCamp AI