Python Sets Tutorial #2 - Methods

Tech With Tim · Beginner ·📐 ML Fundamentals ·7y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches various set methods in Python, including intersection, union, difference, symmetric difference, and copying sets, which can save time and improve code cleanliness in linear algebra and mathematical sets applications.

Key Takeaways
  1. Create two sets s and T
  2. Use the intersection method to find common elements
  3. Use the union method to combine elements
  4. Use the difference method to find unique elements
  5. Use the symmetric difference method to find non-common elements
  6. Copy a set using the copy method
💡 Python's set methods can significantly simplify and speed up tasks involving set operations, making them a valuable tool in linear algebra and mathematical sets applications.

Related Reads

📰
I Don’t Want to Build Another App. I Want to Find Out Where the Models Break.
Learn to identify and fix breaking points in machine learning models, a crucial skill for advancing AI capabilities
Medium · Machine Learning
📰
A Graph Neural Network Model for Real-Time Gesture Recognition Based on sEMG Signals
Learn to build a graph neural network for real-time gesture recognition using sEMG signals for advanced hand prostheses control
ArXiv cs.AI
📰
Evaluating the Effect of Frame Rate in Sequence-Based Classification of Autism-Related Self-Stimulatory Hand Idiosyncrasies
Learn how frame rate affects sequence-based classification of autism-related self-stimulatory hand behaviors and improve your skills in ML for healthcare applications
ArXiv cs.AI
📰
Measure, Don't Estimate: Labeling Speakers Without a Gated Model
Learn to label speakers in audio recordings without relying on complex gated models by using simpler pitch clustering techniques
Dev.to · Dima Statz
Up next
Part 2 | MLOps On GitHub | Deploy and Automate ML Workflow |Using GitHub Actions and CML for CI & CD
Abonia Sojasingarayar
Watch →