Sets in Python - Advanced Python 04 - Programming Tutorial

Patrick Loeber · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

The video tutorial covers the concept of sets in Python, including their creation, properties, and operations, using tools such as the set function and methods like union, intersection, and difference.

Full Transcript

hi everybody welcome to a new python tutorial today we're gonna talk about sets in Python a set us a collection data type that is unordered and mutable but unlike lists or tuples it does not allow duplicate elements a set is created with braces just like a dictionary but we don't put key value pairs in it but instead just single elements separated by a comma for example let's put some numbers in here and print this and we will see our set here and if we put for example another one and then add a two here and print this again then we see that only one of each element is kept because a set does not allow duplicates we can also use the set function and use an iterable here for example let's use a list here this will also create a set or we can use a string here for example hello and if we print this first of all we see that the order is arbitrary because asset is unordered and the order is not important and we also see that there's only one L in our set so this is a nice little trick to find out how many different characters are in your word now if you want to create an empty set and you do it like this just with the braces then you have to be careful because now if you have a look at the type of this then we see that this is recognized as a dictionary so if you want to have an empty set you have to do it with the set method a set is mutable so you can change it later on so now we can add elements and we do this with the dot add method so let's put in some numbers here and print this and we can also remove elements again with the remove method so let's remove the three and if we want to remove an element that is not inside our set then this will raise a key error so be careful here so there's another method that's called at this card method that that's the same thing so it also removes the element and if it does not find the element then nothing will happen so no arrow here we can also use the clear method of course this will empty our set or we can use the pop method so this will return an arbitrary value of our set and also removes it so if we print this then we see in this case it returned the one and also remove the one from our set now we can iterate over our set very easily within for in loop so for I in my set and then do something in this case just print this so this will iterate over each element and print it and we don't have to call this I we can also call this for example X or whatever we want now we want to check if an element is inside our set we can do this with an if in statement so if one in my set and then we print yes so the one is an our set the two is an our our set and if we check for example for the four then nothing gets printed now let's talk about Union and intersection and for this case first of all let's create three different sets one with odd numbers one with Eve numbers and one with prime numbers and now we can calculate the Union so the Union combines elements from both from two sets without duplication so let's calculate the union of odds and we do this with dot Union and then as an argument the second set so evens and print this then we see that now we have all the numbers from zero to nine so the Union will combine elements from both from two sets without duplication we can also calculate the intersection of two sets so the intersection will only take elements that are found in both sets so if we say the intersection equals odds dot intersection evens and if we print this then we will get an empty set because odds and evens don't have the same elements now if we calculate calculate the intersection of odds and Prime's we will get all the prime numbers that are also also 3 5 7 if you calculate the intersection of evens and Prime's then we will get back only the even prime numbers so in this case only the two now we can also calculate the difference of two sets so let's create two different sets again set a with numbers from 1 to 9 and set B with walk with 1 2 3 10 11 12 now the difference will return a set with all elements from the first set that are not in the second set so let's call the call it if equal set a dot difference set B and print this then we see that we will get back the numbers from 4 to 9 because it takes the elements from our first set but not the ones that are also in the second set so only from 4 to 9 so if we do it the other way around set B dot difference set a then it will take 10 11 12 but not these 3 numbers because they are also here so then there's a second different method a second difference method that's called the symmetric difference method so the symmetric difference method will return a set with all the elements from set a and set B but not the elements that are in both sets so again so it takes 4 5 6 7 8 9 from set a and 10 11 12 from set B but not 1 2 and 3 because they are in both sets so if I use set a symmetric difference set B then this is the same thing now Union and intersection and the difference method that I just showed you they will not modify the original sets they always will return a new set but we can also modify our sets in place so for example we can say set a dot update set B and now print our set a then we will see that this updates the set by adding the elements that are found in another set so without duplication again so it does not add one two and three again but it adds 10 11 and 12 there's also a intersection of an update method so set a dot intersection update set B and what this does it updates the set by keeping only the elements from found in both sets so only 1 2 & 3 are found in both sets so only these numbers remain in our set then there's also the difference update method so set a difference update set B and if we print this we will see the numbers from 4 to 9 because difference update it updates the set by removing elements for found in another set so it also it found it finds 1 2 & 3 in the set B so it removes these numbers from our set a and then there's the symmetric difference update so this updates the set by only keeping the elements found in set a and in set B but not the elements that are found in both so 1 2 & 3 are found in both sets so these are not taken but then it takes all the remaining elements from both sets yeah you have probably have to play around with them yourselves a little bit to make it Kira and yeah let's also talk about subset superset and disjoint methods so for example let's make them a little bit smaller you can calculate the if set a is a subset of set B and if we print this then this will return false because subset means that all the elements of our first set are also in our second set so if we use it the other way around set B is a subset of set a then this will return true because 1 2 & 3 are also in the second set and the opposite is called the superset method so is superset and in this case it returns false because a superset returns true if the first set contains all the numbers or all the elements from the second set so set B does not contain 5 4 6 so it's not a superset but set a is a superset of set B because it contains 1 2 & 3 and we can calculate if two sets are disjoint so this joint returns true if both sets have a null intersection so no same elements so set a is this joint set B and if you print this this will return false because they have same elements and if we create for example a set C and put 7/8 in here and check set a is this joint with set B then this will give us true now let's briefly talk about copying two sets if you have watched the previous episodes about lists for example then you already know this you have to be careful when you want to copy two sets and only do this with a simple assignment so let's say let set B equals 2 set 1 set a first of all if you print this then we see that we have a copy but now if we modify the copy let's say set B add 7 and if we print the copy and also print the original 1 then we see that also the original 1 changed because with this simple assignment both point to the same set so be careful here you only copy the reference now if you want to make an actual copy you have to use the dot copy method so if you run this again and we see that the original set didn't change or you can also use the set method and use the first set as a argument this will also make an actual copy now as a last thing I want to show you the frozen set the frozen set is also a collection data type and this is just an immutable version of a normal set so you create this with the frozen set method and there's an argument you can also put an iterable here for example a list and let's print this then we see our frozen set here so with a frozen set you cannot change it after its creation so if I try to do a dot add two this will give us an error or i can also say try a remove one this will also give us an error or any of the updates update methods I showed you they also don't work but for example Union intersection and difference method they will work so yeah that's it about sets and frozen sets and I hope you enjoyed this tutorial and see you in the next tutorial where we talk about strings in Python

Original Description

In this Python Advanced Tutorial, we will be learning about Sets in Python. A Set is a collection data type that is unordered and mutable, but unlike lists or tuples it does not allow duplicate elements. We will go over how you can use them and some advanced techniques that can be applied to sets. ~~~~~~~~~~~~~~ GREAT PLUGINS FOR YOUR CODE EDITOR ~~~~~~~~~~~~~~ ✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=youtube&utm_campaign=pythonengineer * 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN A written Tutorial can be found here: https://www.python-engineer.com/courses/advancedpython/04-sets/ You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python ---------------------------------------------------------------------------------------------------------- * This is a sponsored or an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 4 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
50 PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This video tutorial teaches the basics of sets in Python, including their creation, properties, and operations, and provides hands-on experience with set methods and functions.

Key Takeaways
  1. Create a set with braces and single elements separated by a comma
  2. Use the set function to create a set from an iterable
  3. Add elements to a set with the dot add method
  4. Remove elements from a set with the remove method
  5. Use the union method to combine elements from two sets
  6. Create two sets and calculate their intersection
  7. Create two sets and calculate their difference
  8. Use the symmetric difference method to combine two sets
💡 Sets in Python are unordered collections of unique elements and can be created using the set() function or the {} syntax, with various methods available for set operations.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →