Sets in Python || Python Tutorial || Learn Python Programming

Sets in Python  ||  Python Tutorial  ||  Learn Python Programming

Python comes equipped with different objects
to help you organize your data. These data structures include lists, tuples, sets and
dictionaries. Today we talk about sets. Sets are useful when you are working with data
and the order or frequency of the values do not matter. Get ready to become an element
of the set of people who understand sets… We will begin by creating an empty set called
“example”. To see a list of methods you can use on a
set, use the directory function on our set. We want to add things to our set, and the
“add” function looks promising. To see how to use this method, use the help function. The help text provides a reminder of the definition
of a set. Duplicates are not stored. If you try to add the same element twice, the set
will store it the first time, and ignore it the second time. We will use this method to add several objects
to this set. The integer 42… The boolean value False… The number Pi as a float… The string “thorium”. Notice that you can add data of different
types to the same set. If you enter the name of the set and press
enter, Python will show you the items inside the set. Each item inside a set is called
an “element”. When you try this, the elements may appear
in a different order for you than what is displayed here. Do not panic. For sets, the
order does not matter. This is different from lists and tuples, where the order DOES matter. Now look what happens if you try to add the number 42 to the set a second time… The
set still contains just one copy of the number 42. Sets do not contain duplicate elements. To see the number of elements in a set, use
the length function, which is shortened to L-E-N… Our set does indeed have 4 elements. To remove an element from this set, use the
remove method. Before we test this method, let’s look at the help text. Python gives a stern warning. If you attempt
to remove an element that is not in the set, you will get an error. To test this method, let’s remove the number
‘42’. We can check that it worked either by looking
at the number of elements… Or displaying all the elements inside the
set… Look what happens if we try to remove ‘50’,
which is not in the set… We get an error, just as Python warned us. To avoid the possibility of an error, there
is a second way to remove an element: the discard method. Here is the help text: With the discard method, if you try to remove
an element which is not in the set, the method does nothing – it quietly returns without
making a change. Watch what happens when we discard the integer 50, which is not in the
set: Nothing… Peace and quiet… The choice is yours. If you want to be alerted
when your code tries to remove an element not in the set, use “remove”. Otherwise,
discard provides a convenient alternative. There is a second way to create a set which
can be faster in some instances. When creating the set, you can pre-populate the set with
a collection of elements. You can see this set contains 4 elements. There is also a faster way to remove elements.
To empty out the set and remove all elements, use the “clear” method. The set now contains
no elements – it has become the empty set. We can move along; there is nothing to see
here. Now that we know how to create and modify
a set, let’s learn how to evaluate the union and intersection of TWO sets. If you have
two sets A and B, then the union is the combination of all elements from the two sets and is denoted
with a U-like symbol… The intersection is the set of elements inside both A and B, and
the symbol for this operation is the flip of the union symbol. To see these in action, let’s look at the
integers from 1 through 10. The odd integers are 1, 3, 5, 7 and 9.
The even integers are 2, 4, 6, 8 and 10. The prime numbers between 1 and 10 are 2,
3, 5 and 7. And finally, the composite integers – the
integers which can be factored – are 4, 6, 8, 9 and 10. The union of the odd and even integers are
all the integers from 1 to 10. You get the same answer if you reverse everything. Notice how the set of odds… and the set
of evens are unchanged. We can find the set of odd prime numbers by
computing the intersection of the sets of odds and primes. There are 3 in the range
1 to 10. And there is only one even prime number … 2. Which integers are both even and odd?
There are none. The intersection of these two sets is the empty set. The union of the prime numbers and composite
numbers are the integers from 2 through 10. Notice 1 is missing – this is because 1 is
neither prime nor composite. Another common operation is testing to see
if one element is inside a set. To do this in Python, use the “in” operator. Is 2
in the set of prime numbers? Yes. This is a true statement. Is 6 an odd integer?
No. This is a false statement. You can also test to see if an element is
NOT in a set. Is this statement true or false?
True. 9 is NOT an even integer, so this is a true statement.
There are many more methods and operations you can perform with sets… Take a moment
to explore these methods. You will not regret it. Sets are a built-in data type in Python. They
come equipped with all the luxury features – unions, intersections, adding elements,
removing elements, and much more. Everything you will ever need for your data hungry code…
provided your sets are finite.

100 thoughts on “Sets in Python || Python Tutorial || Learn Python Programming”

  1. Great lesson. I like the combining of why it's important to use the functions and objects the way Python set them up. Helps me to understand how I will use it in the future and hopefully remember the differences in sets, tuples, list, and dictionaries.

  2. Hi, as I said, great job but can we take a lill more time to go through all the functions sets or any other class has? Thanx

  3. Hi, i'm quite confused with something. Objects in a set always need to be immutable , then why does the set in example 2 contain a list which is a mutable object?
    example2 = set([28, True, 2.71828, "Helium"])
    Why does this work?

  4. Started at video 1 and i subscribed instant. This is what i needed. short to the point video's. basically spoonfeeding me bit by bit. Already i'm starting to see the immense power of this programming language, i have no previous coding experience, only HTML, CSS and a bit of CISCO CCNA.

    This is by far the best method to get started if you do not have much or none experience at all. learning coding is hard and can be frightening at times. the humour really helps relax and focus better. i can't get enough of this and plan to watch out the whole playlist in a binge.

  5. Very Useful And Informative. Thank you….. (oh, and great humour too, the style is excellent and I can imagine it might lure in many young minds who will find the series both fun and very useful).

  6. at 3:00…. "Nothing… peace and quiet" lol… I know that moment all to well when I run a new program for the first time. Great work Socratica!

  7. You explained wonderfully well. I like your videos for many reasons and one of the reasons is that you explain to the point without giving any novice descriptions.

  8. I still find the help information really hard to understand. Especially when some are hidden behind the modules tab and need the extra help(math.sin) vs help(sin) etc. Just I need you to explain it first then I can actually understand what the help stuff is trying to tell me….which isn't good.

  9. I keep remember C programming I did a long time ago. always printf scanf….read. And then just masses of text at the start of the program displaying all my variables and functions and everyhting for even teh simplest of programs. Now all I have to do is print(), and just make the variables inside the code as I go instead of loading them in at the start. I totally understand why I was so frustrated and hated learning C now. If they hada taught python or something more intuitive I maybe would be a programmer today.

    I hate how things were done growing up, I feel like I got screwed over in many ways for thinking too much about how stupid it was. I thought they would teach us how computers work. instead they taught us C programming language….telling us this was how computers work. The whole time I'm sitting there scratching my head going… does the shit I type make the computer do stuff though? How was C created? How can teh computer print hello world when I type a text command? how does it know to read a text command? I mean cmon…at least cover the basics of how the commands are converted to binary and how the computer can read 1's and 0's and all that. I

    nstead we had to buy this giant book by the guy who made the course and sit there learning programming theory half the time instead of actually programming or learning how computers work…..PLAN OUT UR PROGRAM BY THINKING WHAT YOU WANT IT TO DO, WRITE DOWN THE VARIOUS STEPS AND THE DESIRED OUTPUT. Christ….No wonder so many people complained about the teacher. I was stuck complaining about the fact it was programming language we were learning, and not how computers work. I never got into programming because of that horrible experience with C. Infact I avoided all computer science just to never deal with c programming again. I barely remember learning any coding in that course at all, pretty sure i could write all the shit I had to do just from this far in the python series. The testing was done on paper via multiple choice exams….like that should tell you enough right there on what a shit programming course it was.

    Makes me angry that schools do this to people. There's just soo many of us, they have to herd us like sheeple…..and yet still take our money away in such stupidly large amounts. So many schools are a scam. Students need a way to be proactive and involved in their learning, online stuff like this offers this avenue of success. I'm tired of paying money to learn about shit I can't apply to anything irl.

  10. Another great video in a great series, thank you! I love the pace of these videos – nice balance of detailed explanation and keeping it moving.

  11. Mahoney Technologies

    I have become a real fan of: e.g., Socratica, 3Blue1Brown, and Titains of CNC, yes all very different but all very well done!

    Is there any chance that you can include MicroPython? Sure Hope So!

  12. In her class~
    Student: *raises hands* umm.. ma'am…… ah…

    Her: *eyes turns black and starts transforming into a…… Human*

    Student: *pisses in his pants*

  13. live for humanity

    Can i get a reply without any reason?? Reason i am a student of urs, and a great fan of ur teaching, video has been shared with our college group, thanks

  14. when are you going to upload on hash pyhton?????????????????????????????????????????????????????????????????????

  15. Day of creativity :
    Algorithm for prime numbers from the integers from 1 to 100 :

    integers_1_100 = set(range(1, 100)) # to set integers from 1 to 100
    print("integers_1_100 = ", integers_1_100)
    integer = 1 # initialize the integers which to compare
    while integer <= 100 : # a loop which analize each integer from 1 to 100 included
    if integer/1 == 1: # 1 is a composite number not a prime number, a composite
    # number is a positive integer that can be formed by multiplying two
    # smaller positive integers.
    print("integer/1 = ", integer/1)
    integers_1_100.discard(integer) # discard 1 from the set of
    l = 2 # initialize the divisor
    while True and (l < integer): #
    rest = integer % l # rest value is fill of the rest of the
    # integer/divisor
    if rest == 0 : # if rest is 0 => the integer is divisible by more
    #than two positive divisors
    integers_1_100.discard(integer) # so we discard the integer
    #inside the set
    primes = integers_1_100 # updating the set : prime
    break # to this while loop, then go to below to l108
    else :
    if l < integer – 1: # we need to verify that the divisor is
    # less than the integer
    l = l + 1 # if it is the case, we add 1 to the divisor and
    # we restart this loop
    else :
    break # else it means that we have tested all possibles
    # divisors to check if the integer is a prime number so
    # we stop le loop, go to l108
    integer = integer + 1 # add 1 to the integer, restart the loop
    print("list of prime numbers from 1 to 100 :", primes)

  16. I kind of thought this would be cheesy, but these are some very well put together tutorials! Short, to the point and a very good job of explaining the content. Thank you very much for these

Leave a Reply

Your email address will not be published. Required fields are marked *